我的 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?我不应该分别期待 2 和 1 吗?因为第一个对象有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"
本质上,我的意图是处理对象"的子级.
Essentially, my intent is to process children of "objects".
那是因为每个子节点之间有2个TEXT_NODE (#text).
That's because you have 2 TEXT_NODE (#text) between each child nodes.
以下包括文本节点及其对应的值.
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>
这可以通过修改代码来验证:
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.
这篇关于getChildNodes 给出意外的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
上传进度侦听器未触发(Google 驱动器 API)Upload progress listener not fired (Google drive API)(上传进度侦听器未触发(Google 驱动器 API))
使用 Google Drive SDK 将文件保存在特定文件夹中Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 将文件保存在特定文件夹中)
Google Drive Android API - 无效的 DriveId 和 Null ResourcGoogle Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 无效的 DriveId 和 Null ResourceId)
谷歌驱动api服务账户查看上传文件到谷歌驱动使Google drive api services account view uploaded files to google drive using java(谷歌驱动api服务账户查看上传文件到谷歌驱动使用java
Google Drive 服务帐号返回 403 usageLimitsGoogle Drive service account returns 403 usageLimits(Google Drive 服务帐号返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory;Google Drcom.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例