<?xml version="1.0" encoding="gb2312" ?>
<results>
<item>
<title>陈家洛传奇</title>
<price>34</price>
<summary>中国共产党</summary>
</item>
<result>
<name>陈龙传</name>
<price>34999</price>
<digest>在此基础上</digest>
</result>
</results>
我理想的情况是获取<results></results>下子节点的个数为2。[即一个item,一个result].我自己的部分代码如下:
NodeList nodes = onlinedoc.getElementsByTagName("results");
System.out.println("返回结果共有" + nodes.getLength() + "条记录");//在这儿的话,返回结果是1。
for (int i = 0; i < nodes.getLength(); i++) {
Node node=nodes.item(i);
NodeList childs = node.getChildNodes();//又想到在这儿取childs.getLength.但是结果是5,而且我打//印结果出来发现。有三个是空的。
请问有什么办法获取这个值呢?我不要硬编码的实现。因为最先贴出来的xml也可能用其他的节点,如:<?xml version="1.0" encoding="gb2312" ?>
<results>
<item>
<title>陈家洛传奇</title>
<price>34</price>
<summary>中国共产党</summary>
</item>
<result>
<name>陈龙传</name>
<price>34999</price>
<digest>在此基础上</digest>
</result>
<abc>...</abc>
<rrr>...</rrr>
</results>
理想情况下,这个值就是四。请高手指点一下,我自己用的是DOM, JAVA编程。万分感激。
<results>
<item>
<title>陈家洛传奇</title>
<price>34</price>
<summary>中国共产党</summary>
</item>
<result>
<name>陈龙传</name>
<price>34999</price>
<digest>在此基础上</digest>
</result>
</results>
我理想的情况是获取<results></results>下子节点的个数为2。[即一个item,一个result].我自己的部分代码如下:
NodeList nodes = onlinedoc.getElementsByTagName("results");
System.out.println("返回结果共有" + nodes.getLength() + "条记录");//在这儿的话,返回结果是1。
for (int i = 0; i < nodes.getLength(); i++) {
Node node=nodes.item(i);
NodeList childs = node.getChildNodes();//又想到在这儿取childs.getLength.但是结果是5,而且我打//印结果出来发现。有三个是空的。
请问有什么办法获取这个值呢?我不要硬编码的实现。因为最先贴出来的xml也可能用其他的节点,如:<?xml version="1.0" encoding="gb2312" ?>
<results>
<item>
<title>陈家洛传奇</title>
<price>34</price>
<summary>中国共产党</summary>
</item>
<result>
<name>陈龙传</name>
<price>34999</price>
<digest>在此基础上</digest>
</result>
<abc>...</abc>
<rrr>...</rrr>
</results>
理想情况下,这个值就是四。请高手指点一下,我自己用的是DOM, JAVA编程。万分感激。
doc.getDocumentElement().appendChild(doc.importNode(childs.item(j), true));
System.out.print("j: "+j+" ");
}
非常清楚。