2011-12-09 160 views
25

我想通过采取参数type_id =“4218”的所有“<Type>”元素从URL解析XML文件?如何获取具有特定属性值的特定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”!

回答

23

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

+0

我如何寻找价值TYPE_ID与运营商一样 –

4

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

你错过了你的循环内的条件:

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为空,所以你可以删除如果在循环之前。

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

2

以下XPath会给你的类型元素,你是后:

/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); 
2

请按照下面的@soulcheck答案,如果可能的话放一个break语句...以增强您的搜索。

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; 

    } 
} 

}

相关问题