【问题标题】:Parsing an XML file in JDOM for child-child nodes在 JDOM 中为子子节点解析 XML 文件
【发布时间】:2016-01-05 19:48:23
【问题描述】:

我有一个 XML 文件,格式如下:

<head>
                <username>bhnsub</username>
                <error>0</error>
                <account_id>633</account_id>

        <info>
        <mac>address_goes_here<mac>
        <mac>address_goes_here</mac>
        <mac>address_goes_here</mac>
        <mac>address_goes_here</mac>
        <mac>address_goes_here<mac>
    </info>
</head>

我需要使用 Java DOM 解析器对其进行解析并获取相应的值。 我需要将信息下的值放在列表中。

    SAXBuilder builder = new SAXBuilder();
   Document document = (Document) builder.build(new StringReader(content));
            Element rootNode = document.getRootElement();
            if (rootNode.getName().equals("head")) {
                String username = rootNode.getChildText("username");
                String error= rootNode.getChildText("error");
                String account= rootNode.getChildText("account_id");
                Element info= rootNode.getChildren("info");
                        List mac=info.getChildren("mac");

我不知道如何继续使用该列表。

【问题讨论】:

    标签: java xml xml-parsing jdom


    【解决方案1】:

    首先,请确保您使用的是 JDOM 2.0.6(或更高版本,如果您将来阅读本文)。 JDOM 2.x 已经推出了 5 年左右,并且更好,因为它支持 Java 泛型,它有性能改进,如果你需要它,它也有更好的 XPath 支持。

    不过,您的代码可以“轻松”写成:

    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(new StringReader(content));
    Element rootNode = document.getRootElement();
    if ("head".equals(rootNode.getName())) {
        String username = rootNode.getChildText("username");
        String error= rootNode.getChildText("error");
        String account= rootNode.getChildText("account_id");
        List<String> macs = new ArrayList<>();
        for (Element info : rootNode.getChildren("info")) {
            for (Element mac : info.getChildren("mac")) {
                macs.add(mac.getValue());
            }
        }
    }
    

    请注意,我在其中放置了 2 个循环。您的代码有错误,因为它调用:

    Element info = rootNode.getChildren("info");
    

    但是getChildren(...) 返回一个列表,所以这不起作用。在我上面的代码中,我改为遍历列表。如果只有一个“info”元素,那么列表将只有一个成员。

    另请注意,在 JDOM 2.x 中,getChildren(..) 方法返回元素列表:List&lt;Element&gt;,因此无需将结果转换为 Element

    【讨论】:

      【解决方案2】:

      这行得通,使用 javax.xml.parsers 和 org.w3c.dom 中的东西。

      List<String> macvals = new ArrayList<>();
      DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document document = db.parse(new File( "head.xml" ) );
      Element rootNode = document.getDocumentElement();
      if (rootNode.getTagName().equals("head")) {
          NodeList infos = rootNode.getElementsByTagName("info");
          if( infos.getLength() > 0 ){
          Element info = (Element)infos.item(0);
          NodeList macs = info.getElementsByTagName("mac");
          for( int i = 0; i < macs.getLength(); ++i ){
              macvals.add( macs.item( i ).getTextContent() );
          }
          }
      }
      System.out.println( macvals );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多