久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在java中動(dòng)態(tài)讀取xml元素和值

dynamically read xml element and value in java(在java中動(dòng)態(tài)讀取xml元素和值)
本文介紹了在java中動(dòng)態(tài)讀取xml元素和值的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試讀取 XML(無論如何,我不知道它的結(jié)構(gòu),bcz 我正在從服務(wù)器讀取文件.),我在 SO 上進(jìn)行了很多搜索.但沒有發(fā)現(xiàn)任何有用的東西.

I'm trying to read XML(whatever, i don't know it's structure, bcz i'm reading flie from server.), and i searched a lot on SO. but not found anything helpful.

1> 我想檢索 XML 的節(jié)點(diǎn)名稱,從根目錄到文件末尾、子節(jié)點(diǎn)或子節(jié)點(diǎn)的子節(jié)點(diǎn),直到?jīng)]有子節(jié)點(diǎn)或兄弟節(jié)點(diǎn).

1> i want to retrieve XML's node name, starting from root to end of file, child or child of child untill there's no child or sibling remaining.

2> 在類似的問題中,我發(fā)現(xiàn)他們使用標(biāo)記名來檢索該標(biāo)記的值,所以我想在其中傳遞該節(jié)點(diǎn)名以獲取值...

2> in similar question, i found that they use tagname to retrieve value of that tag, so i want to pass that node name inside this to get value...

是否可以動(dòng)態(tài)進(jìn)行.或者必須遵循與之前問題相同的結(jié)構(gòu),例如靜態(tài)添加標(biāo)記名以檢索值.

is it possible to do dynamically. or have to follow same structure as in previous questions like statically add tagname to retrieve value.

在java中完成后,我想創(chuàng)建可執(zhí)行jar,并想在歡樂中使用它.

after doing this in java, i want to create executable jar, and want to use it in mirth.

編輯我想找到元素,它的值是這樣的,在第一個(gè)條件下,我正在循環(huán)我的數(shù)組列表,我命名為 masterheader,其中包含所有標(biāo)簽名稱,在第二個(gè)循環(huán)中,我將從中查找節(jié)點(diǎn)名稱xml,并且在這兩個(gè) for 循環(huán)中,我指定了一個(gè)條件,如果兩者都匹配,則它獲取節(jié)點(diǎn)名稱并將值存儲(chǔ)為分隔形式,因?yàn)槲蚁雽⑵滢D(zhuǎn)換為 csv.

EDIT i want to find element and it's value such that, in first condition i'm looping my arraylist which i named masterheader which contain all tag name, and in second loop i'm going to find node name from xml, and and inside that two for loop i specified one condition, if both match, then it get node name and it store a value in , separated form, because i want to convert it in csv.

這里,masterheader 和 childheader 是 arraylist.masterheader 默認(rèn)包含所有標(biāo)頭.我們將在 childheader 中添加節(jié)點(diǎn)名,如果兩者都匹配,那么我們?cè)诮Y(jié)果字符串中添加值.例如masterheader = [員工姓名、員工姓名、父親姓名、手機(jī)、工資]而在childheader = [employeename, mobile,salary]//這是基于xml解析的.

here, masterheader and childheader is arraylist. masterheader contain all header by default. and we gonna add nodename in childheader, nd if both match then we add value in result string. for e.g. masterheader = [employeename, employeesurname, fathername, mobile, salary] and in childheader = [employeename, mobile, salary] //this is based on xml parsing.

for(i=0;i<masterheader.size();i++)
{
    for(j=0;j<childheader.size();j++)
    {
         if((masterheader[i]).equals(childheader[j]))
        {
           //get the value of that node, + ","        
        }
        else
         {
                count++
         }
    }
if(count==childheader.size()){
 //add into result string +null+","  
}
}

推薦答案

您可以為此目的使用 StAX 解析器.

You could use a StAX parser for this purpose.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employees count="4">
    <employee>
        <id>2</id>
        <firstName>Jane</firstName>
        <lastName>Doe</lastName>
        <income>20000.0</income>
    </employee>
    <employee>
        <id>3</id>
        <firstName>Alfred</firstName>
        <lastName>Pennyworth</lastName>
        <income>30000.0</income>
    </employee>
    <employee>
        <id>4</id>
        <firstName>Tony</firstName>
        <lastName>Stark</lastName>
        <income>40000.0</income>
    </employee>
    <employee>
        <id>42</id>
        <firstName>Foo</firstName>
        <lastName>Bar</lastName>
        <income>15.0</income>
    </employee>
</employees>

Java 代碼

try (InputStream stream = new FileInputStream("employees.xml")) {
    XMLInputFactory inputFactory = XMLInputFactory.newFactory();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);

    XMLStreamReader reader = inputFactory.createXMLStreamReader(stream);

    while (reader.hasNext()) {
        switch (reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                System.out.println("Start " + reader.getName());
                for (int i = 0, count = reader.getAttributeCount(); i < count; i++) {
                    System.out.println(reader.getAttributeName(i) + "=" + reader.getAttributeValue(i));
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                System.out.println("End " + reader.getName());
                break;
            case XMLStreamConstants.CHARACTERS:
            case XMLStreamConstants.SPACE:
                String text = reader.getText();
                if (!text.trim().isEmpty()) {
                    System.out.println("text: " + text);
                }
                break;
        }
    }
}

請(qǐng)注意,您可以輕松修改 START_ELEMENT 大小寫以將標(biāo)記名稱與某個(gè)值進(jìn)行比較.(如果您只想檢查這些元素,可能您可以忽略任何其他情況.)

Note that you could easily modify the START_ELEMENT case to compare the tag name to some value. (Probably you could ignore any other cases if you only want to check those elements.)

Start employees
count=4
Start employee
Start id
text: 2
End id
Start firstName
text: Jane
End firstName
Start lastName
text: Doe
End lastName
Start income
text: 20000.0
End income
End employee
Start employee
Start id
text: 3
End id
Start firstName
text: Alfred
End firstName
Start lastName
text: Pennyworth
End lastName
Start income
text: 30000.0
End income
End employee
Start employee
Start id
text: 4
End id
Start firstName
text: Tony
End firstName
Start lastName
text: Stark
End lastName
Start income
text: 40000.0
End income
End employee
Start employee
Start id
text: 42
End id
Start firstName
text: Foo
End firstName
Start lastName
text: Bar
End lastName
Start income
text: 15.0
End income
End employee
End employees

這篇關(guān)于在java中動(dòng)態(tài)讀取xml元素和值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進(jìn)度偵聽器未觸發(fā)(Google 驅(qū)動(dòng)器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動(dòng)api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動(dòng)使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號(hào)返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 久久久亚洲一区 | 在线视频一区二区三区 | 精品综合久久久 | 色综合一区二区三区 | 国产精品成人av | 国产精品福利久久久 | 国产精品不卡一区 | 国产资源一区二区三区 | a级大片 | 国产高清视频 | 欧美激情精品久久久久 | 国产又爽又黄的视频 | 国产综合久久 | 国产精品一区二区三区在线 | 精品久久影院 | 91在线网| 国产精品成人一区二区三区夜夜夜 | 午夜精品福利视频 | 日韩视频国产 | 久久手机在线视频 | 欧美成人精品一区二区男人看 | 久久久久综合 | 亚洲色欲色欲www | 岛国在线免费观看 | 中日av| 日韩在线高清 | 日韩中文字幕在线播放 | 欧美韩一区二区 | 亚洲精品中文字幕在线观看 | 黄色日批视频 | 91久久精品一区二区二区 | 欧美极品在线播放 | 日韩一区二区三区四区五区 | 久久久91精品国产一区二区精品 | 手机av网 | 毛片站 | 国产精品18hdxxxⅹ在线 | 国产欧美精品区一区二区三区 | 中文字幕一区二区视频 | 国产一区二区在线播放 | 一级免费看片 |