解析xml中嵌套标签的问题

发布于 2024-11-19 05:58:37 字数 1123 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情癖 2024-11-26 05:58:37

您没有使用有效的 XML - 标签名称中不能有空格。

XML 名称不能包含空格,请参阅此处了解有效值。


更新(以下评论表明发布的示例代表了实际的 XML):

您通过节点列表索引器的访问不正确:

String subSlide_title = subSlides.item(i).getFirstChild().getNodeValue();

请尝试这样做(使用 j 而不是 i,作为内部循环变量被调用):

String subSlide_title = subSlides.item(j).getFirstChild().getNodeValue();

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:

String subSlide_title = subSlides.item(i).getFirstChild().getNodeValue();

Try this instead (using j instead of i, as the inner loop variable is called):

String subSlide_title = subSlides.item(j).getFirstChild().getNodeValue();
尤怨 2024-11-26 05:58:37
NodeList nodes = doc.getElementsByTagName("item");
 for (int i = 0; i < nodes.getLength(); i++) {
     Element element = (Element) nodes.item(i);
     NodeList nodesimg = element.getElementsByTagName("name");
     for (int j = 0; j < nodesimg.getLength(); j++) {
        Element line = (Element) nodesimg.item(j);
        String value=getCharacterDataFromElement(line);
     }
}
public static String getCharacterDataFromElement(Element e) {
  Node child = e.getFirstChild();
  if (child instanceof CharacterData) {
     CharacterData cd = (CharacterData) child;
     return cd.getData();
  }
  return "?";
}

我认为上面的代码将帮助您解析 xml 文件。

NodeList nodes = doc.getElementsByTagName("item");
 for (int i = 0; i < nodes.getLength(); i++) {
     Element element = (Element) nodes.item(i);
     NodeList nodesimg = element.getElementsByTagName("name");
     for (int j = 0; j < nodesimg.getLength(); j++) {
        Element line = (Element) nodesimg.item(j);
        String value=getCharacterDataFromElement(line);
     }
}
public static String getCharacterDataFromElement(Element e) {
  Node child = e.getFirstChild();
  if (child instanceof CharacterData) {
     CharacterData cd = (CharacterData) child;
     return cd.getData();
  }
  return "?";
}

I think above code will help you in parsing xml file.

遇见了你 2024-11-26 05:58:37

XML 元素都乱了。
实际上有两行没有错误。

例如,

<subItem 4> 

它在语法上是错误的,我不知道你能从中得出什么逻辑意义。

您的意思是

<subItem4> 

像第四个子项目中那样还是

<subItem someAttribute="4">

我建议学习 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

<subItem 4> 

is syntactically wrong and I don't see what logical sense you could make out of it.

Do you mean

<subItem4> 

as in the fourth sub item or

<subItem someAttribute="4">

I'd recommend learning XML, it's very simple... http://www.w3schools.com/xml/default.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文