2011-12-09 43 views
25

Tôi đang cố gắng phân tích cú pháp tệp XML từ URL bằng cách lấy tất cả các phần tử "<Type>" trong đó tham số type_id = "4218" ??Cách lấy các phần tử XML cụ thể với giá trị thuộc tính cụ thể?

tài liệu

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> 

Đây là mã Java của tôi:

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); 
    } 
} 

Mã này mang lại cho tôi tất cả các yếu tố, tôi không muốn điều đó, tôi muốn tất cả các yếu tố mà các thuộc tính type_id = "4218"!

Trả lời

23

XPath là sự lựa chọn phù hợp với bạn:

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

Và lặp qua nl

+0

Làm thế nào tôi tìm kiếm giá trị trong type_id với nhà điều hành như –

4

Bạn có thể sử dụng XPath.XPath được sử dụng để điều hướng qua các phần tử và thuộc tính trong tài liệu XML. Có một số triển khai tốt của Xpath trong Java.

Đối với bạn ví dụ

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

Bạn đang thiếu một điều kiện bên trong vòng lặp của bạn:

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); 
     } 
    } 
} 

Ngoài ra, bạn không cần phải kiểm tra xem NodeList trả về bởi getElementsByTagName là null, do đó bạn có thể loại bỏ nếu trước khi vòng lặp.

Trong trường hợp chung, bạn có thể sử dụng XPath tốt hơn.

2

XPath sau đây sẽ cung cấp cho bạn các yếu tố Loại bạn đang sau:

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

Vì vậy, bạn có thể sử dụng mã Java sau đây để có được một NodeList chỉ bao gồm các:

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

theo dõi câu trả lời @soulcheck bên dưới và đặt câu lệnh ngắt nếu có thể ... có thể cải thiện tìm kiếm của bạn.

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; 

    } 
} 

}

Các vấn đề liên quan