如何获取具有特定属性值的特定XML元素?

我试图通过获取所有“ ”元素来解析URL中的XML文件,其中参数type_id =“4218”??

XML文档:

   Football Matches  ...   ...   ...   ...   ...   ...   ...    

这是我的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”的所有元素!

XPath是您的正确选择:

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(""); 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

你在循环中错过了一个条件:

  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可能会更好。

您可以使用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; 

按照下面的@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; } } 

}

以下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);