解析xml中嵌套标签的问题
我有一个 xml,正在通过 DOM 解析器进行解析。 xml 在某种程度上是这样的序列
<root>
<item1> abc </item1>
<item2> def </item2>
<item3> ghi </item3>
<item4>
<subItem4>
<name> xyz </name>
<id> 1 </id>
</subItem4>
<subItem4>
<name> asd </name>
<id> 2 </id>
</subItem4>
</item4>
</root>
根据这个虚拟 xml,我到达了子项 4,但没有到达它的子项。我正在尝试如下获取最里面的项目:
NodeList slip = theElement.getElementsByTagName("item4").item(0).getChildNodes();
for(int i = 0; i<slide.getLength(); i++)
{
NodeList subSlides = theElement.getElementsByTagName("subItem4").item(0).getChildNodes();
for (int j=0; j<subSlides.getLength(); j++)
{
String subSlide_title = subSlides.item(i).getFirstChild().getNodeValue();
}
}
它不工作。请有人能确定我在解析过程中在哪里犯了错误。任何帮助表示赞赏。
I have an xml that i am parsing through DOM parser. the xml is of somewhat this sequence
<root>
<item1> abc </item1>
<item2> def </item2>
<item3> ghi </item3>
<item4>
<subItem4>
<name> xyz </name>
<id> 1 </id>
</subItem4>
<subItem4>
<name> asd </name>
<id> 2 </id>
</subItem4>
</item4>
</root>
According to this dummy xml i am reaching till subItem 4 but not to the childern of it. what i am trying is as follows to get innermost Items is:
NodeList slide = theElement.getElementsByTagName("item4").item(0).getChildNodes();
for(int i = 0; i<slide.getLength(); i++)
{
NodeList subSlides = theElement.getElementsByTagName("subItem4").item(0).getChildNodes();
for (int j=0; j<subSlides.getLength(); j++)
{
String subSlide_title = subSlides.item(i).getFirstChild().getNodeValue();
}
}
its not working. please can someone identify where am i doing the mistake in parsing. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有使用有效的 XML - 标签名称中不能有空格。
XML 名称不能包含空格,请参阅此处了解有效值。
更新(以下评论表明发布的示例代表了实际的 XML):
您通过节点列表索引器的访问不正确:
请尝试这样做(使用
j
而不是i
,作为内部循环变量被调用):You are not using valid XML - you can't have spaces in tag names.
XML Names cannot contain white space, see here for valid values.
Update (following comment that the posted sample is representative of the actual XML):
Your access via the indexer of the node list is incorrect:
Try this instead (using
j
instead ofi
, as the inner loop variable is called):我认为上面的代码将帮助您解析 xml 文件。
I think above code will help you in parsing xml file.
XML 元素都乱了。
实际上有两行没有错误。
例如,
它在语法上是错误的,我不知道你能从中得出什么逻辑意义。
您的意思是
像第四个子项目中那样还是
我建议学习 XML,它非常简单... http://www.w3schools.com/xml/default.asp
The XML elements are all messed up.
There are literally 2 lines that don't have mistakes in them.
For instance
is syntactically wrong and I don't see what logical sense you could make out of it.
Do you mean
as in the fourth sub item or
I'd recommend learning XML, it's very simple... http://www.w3schools.com/xml/default.asp