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

getChildNodes 給出意外的結(jié)果

getChildNodes giving unexpected result(getChildNodes 給出意外的結(jié)果)
本文介紹了getChildNodes 給出意外的結(jié)果的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我的 XML 看起來像這樣 -

My XML looks like this-

<collected_objects>
        <object flag="complete" id="objId" version="1">
          <variable_value variable_id="varId">ValueGoesHere</variable_value>
          <reference item_ref="2"/>
        </object>
        <object comment="objComment" flag="complete" id="objId" version="1">
          <reference item_ref="1"/>
        </object>
</collected_objects>

我正在使用下面的代碼處理它-

I am processing it using below code-

Document dom = parser.getDocument();
    NodeList collected_objects = dom.getElementsByTagName("object");
    System.out.println("Number of collected objects are " + collected_objects.getLength());

        for (int i = 0; i < collected_objects.getLength(); i++) {

            Node aNode = collected_objects.item(i);
            //get children of "objects"         
            NodeList refNodes = aNode.getChildNodes();

            System.out.println("# of chidren are " + refNodes.getLength());

            //print attributes of "objects"

            NamedNodeMap attributes = aNode.getAttributes();
            for (int a = 0; a < attributes.getLength(); a++) {
             Node theAttribute = attributes.item(a);
             System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());

        }

}

它輸出為-

Number of collected objects are 2
# of chidren are 5
flag=complete
id=objId
version=1
# of chidren are 3
comment=objComment
flag=complete
id=objId
version=1

我的問題是為什么# of chidren are"分別是 5 和 3?我不應(yīng)該分別期待 2 和 1 嗎?因?yàn)榈谝粋€對象有variable_value"和reference",而第二個對象只有reference"

My question is why "# of chidren are" are 5 and 3 respectively? Shouldn't I be expecting 2 and 1 respectively ? because first object has "variable_value" and "reference" and second object has only "reference"

本質(zhì)上,我的意圖是處理對象"的子級.

Essentially, my intent is to process children of "objects".

推薦答案

那是因?yàn)槊總€子節(jié)點(diǎn)之間有2個TEXT_NODE (#text).

That's because you have 2 TEXT_NODE (#text) between each child nodes.

以下包括文本節(jié)點(diǎn)及其對應(yīng)的值.

The following included the text nodes and their corresponding values.

<object flag="complete" id="objId" version="1">
    <TEXT_NODE />
    <variable_value variable_id="varId">ValueGoesHere</variable_value>
    <reference item_ref="2"/>
    <TEXT_NODE />
</object>

這可以通過修改代碼來驗(yàn)證:

This can be verified by modifying your code:

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document dom = dBuilder.parse(new ByteArrayInputStream(S.getBytes()));
        NodeList collected_objects = dom.getElementsByTagName("object");
        System.out.println("Number of collected objects are "
                + collected_objects.getLength());

        for (int i = 0; i < collected_objects.getLength(); i++) {

            Node aNode = collected_objects.item(i);
            // get children of "objects"
            NodeList refNodes = aNode.getChildNodes();

            System.out.println("# of chidren are " + refNodes.getLength());

            //
            for (int x = 0; x < refNodes.getLength(); x++) {
                Node n = refNodes.item(x);
                System.out.println(n.getNodeType() + " = " + n.getNodeName() + "/" + n.getNodeValue());
            }

            // print attributes of "objects"

            NamedNodeMap attributes = aNode.getAttributes();
            for (int a = 0; a < attributes.getLength(); a++) {
                Node theAttribute = attributes.item(a);
                System.out.println(theAttribute.getNodeName() + "="
                        + theAttribute.getNodeValue());

            }

        }

輸出:

Number of collected objects are 2
# of chidren are 5
3 = #text/          
1 = variable_value/null
3 = #text/          
1 = reference/null
3 = #text/        
flag=complete
id=objId
version=1
# of chidren are 3
3 = #text/          
1 = reference/null
3 = #text/        
comment=objComment
flag=complete
id=objId
version=1

其中,3 = TEXT_NODE 和 1 = ELEMENT_NODE.

Where, 3 = TEXT_NODE and 1 = ELEMENT_NODE.

這篇關(guān)于getChildNodes 給出意外的結(jié)果的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進(jìn)度偵聽器未觸發(fā)(Google 驅(qū)動器 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ū)動api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 日韩视频―中文字幕 | 欧美黑人一级爽快片淫片高清 | 美国十次成人欧美色导视频 | 中文字幕视频一区 | 国产99精品| 久久久亚洲一区 | 一本一道久久a久久精品蜜桃 | 97国产精品视频人人做人人爱 | 国产三级大片 | 欧美综合在线视频 | 国产精品久久久久久久久久久免费看 | 99久久婷婷国产综合精品电影 | 欧美精品网站 | 五月婷婷中文 | 欧美视频一区二区三区 | 一区二区三区中文字幕 | 成人激情免费视频 | 蜜桃毛片 | 黑人中文字幕一区二区三区 | 日本成人午夜影院 | 欧美在线一区二区三区 | 国产高清免费 | 国产精品不卡 | 国产成人免费观看 | 99re在线视频 | 国产特级毛片aaaaaa喷潮 | 日韩欧美一区二区三区 | 91p在线观看| 免费日韩av | 91麻豆精品国产91久久久更新资源速度超快 | 777777777亚洲妇女 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 日韩av中文 | 人人九九精| 浮生影院免费观看中文版 | 91综合网 | 国产粉嫩尤物极品99综合精品 | 精品少妇v888av | 国产福利视频导航 | 精品久久久精品 | 五月激情综合 |