【问题标题】:How to get specific XML elements with specific attribute value?如何获取具有特定属性值的特定 XML 元素?
【发布时间】:2012-01-16 17:46:29
【问题描述】:

我正在尝试通过获取参数 type_id="4218" 所在的所有“<Type>”元素来解析来自 URL 的 XML 文件??

XML 文档:

<BSQCUBS Version="0.04" Date="Fri Dec 9 11:43:29 GMT 2011" MachineDate="Fri, 09 Dec 2011 11:43:29 +0000">
  <Class class_id="385">
    <Title>Football Matches</Title>
    <Type type_id="4264" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="5873" type_minbet="0" type_maxbet="0">
      ...
    </Type>
    <Type type_id="4725" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="4218" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="4221" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="4218" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
    <Type type_id="4299" type_minbet="0.1" type_maxbet="2000.0">
      ...
    </Type>
  </Class>
</BSQCUBS>

这是我的 Java 代码:

 DocumentBuilder db = dbf.newDocumentBuilder();
 Document doc = db.parse(new URL("http://cubs.bluesq.com/cubs/cubs.php?action=getpage&thepage=385.xml").openStream());

 doc.getDocumentElement().normalize();

 NodeList nodeList = doc.getElementsByTagName("Type");
 System.out.println("ukupno:"+nodeList.getLength());
 if (nodeList != null && nodeList.getLength() > 0) {
   for (int j = 0; j < nodeList.getLength(); j++) {
     Element el = (org.w3c.dom.Element) nodeList.item(j);
     type_id = Integer.parseInt(el.getAttribute("type_id"));
     System.out.println("type id:"+type_id);
   }
 }

这段代码给了我所有元素,我不想要那个,我想要属性 type_id = "4218" 的所有元素!

【问题讨论】:

    标签: java xml-parsing


    【解决方案1】:

    XPath 是您的正确选择:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("<Your xml doc uri>");
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile("//Type[@type_id=\"4218\"]");
    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    

    并遍历nl

    【讨论】:

    • 我如何使用 like 运算符在 type_id 中搜索值
    【解决方案2】:

    您的循环中缺少一个条件:

     if(nodeList != null && nodeList.getLength() > 0){
         for (int j = 0; j < nodeList.getLength(); j++) {
             Element el = (org.w3c.dom.Element) nodeList.item(j);
             if (el.hasAttribute("type_id") && el.getAttribute("type_id").equals("4218")) {
                  type_id = Integer.parseInt(el.getAttribute("type_id"));
    
                  System.out.println("type id:"+type_id);
             }
         }
    }
    

    此外,您不需要测试 getElementsByTagName 返回的 NodeList 是否为 null,因此您可以在循环之前删除 if。

    一般情况下,使用 XPath 可能会更好。

    【讨论】:

      【解决方案3】:

      您可以使用 XPath。XPath 用于在 XML 文档中导航元素和属性。Java 中有一些很好的 Xpath 实现。

      举个例子

      XPath xpath = XPathFactory.newInstance().newXPath();
      XPathExpression expr = xpath.compile("//Type[@type_id=\"4218\"]");
      Object exprResult = expr.evaluate(doc, XPathConstants.NODESET);
      NodeList nodeList = (NodeList) exprResult;
      

      【讨论】:

        【解决方案4】:

        按照下面的@soulcheck 回答,如果可能的话,添加一个中断语句......这可以增强您的搜索。

         if(nodeList != null && nodeList.getLength() > 0){
         for (int j = 0; j < nodeList.getLength(); j++) {
             Element el = (org.w3c.dom.Element) nodeList.item(j);
             if (el.hasAttribute("type_id") && el.getAttribute("type_id").equals("4218")) {
                  type_id = Integer.parseInt(el.getAttribute("type_id"));
        
                  System.out.println("type id:"+type_id);
                  break;
        
             }
         }
        

        }

        【讨论】:

          【解决方案5】:

          以下 XPath 将为您提供所需的 Type 元素:

          /BSQCUBS/Class/Type[@type_id=4218]
          

          因此,您可以使用以下 Java 代码来获取仅包含这些的 NodeList:

          XPathExpression expr = xpath.compile("/BSQCUBS/Class/Type[@type_id=4218]");
          NodeList nl = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-03-03
            • 1970-01-01
            • 2021-05-24
            • 1970-01-01
            • 1970-01-01
            • 2023-01-19
            • 1970-01-01
            相关资源
            最近更新 更多