2011-10-04 106 views
0

我試圖打印出一些數據 所以我的代碼是XML在Java解析使用XPath

private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance(); 
     private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance(); 
     public void parseXml(String urlPath) throws Exception { 
      URL url = new URL(urlPath); 
      URLConnection connection = url.openConnection(); 
      DocumentBuilder db = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); 

      final Document document = db.parse(connection.getInputStream()); 
      XPath xPathEvaluator = XPATH_FACTORY.newXPath(); 
      XPathExpression nameExpr = xPathEvaluator.compile("lfm/results/trackmatches/track"); 
      NodeList tracksinfoNodes = (NodeList) nameExpr.evaluate(document, XPathConstants.NODESET); 
      for (int i = 0; i < tracksinfoNodes.getLength(); i++) { 
       Node trackNameNode = tracksinfoNodes.item(i); 
       System.out.println(String.format("Track Name: %s" , trackNameNode.getTextContent())); 
       } 

     } 

所以它將打印我喜歡本作的第一循環

Track Name: 
    I Believe in You 
    Neil Young 
    http://www.last.fm/music/Neil+Young/_/I+Believe+in+You 
    0 
    90540 
      http://userserve-ak.last.fm/serve/34s/65285990.png 
    http://userserve-ak.last.fm/serve/64s/65285990.png 
    http://userserve-ak.last.fm/serve/126/65285990.png 
    http://userserve-ak.last.fm/serve/300x300/65285990.png 

的URL我我正在使用的是http://ws.audioscrobbler.com/2.0/?method=track.search&track=Believe&api_key=b25b959554ed76058ac220b7b2e0a026

所以我想要做的就是讓eveyone獨自一人,像這樣

song : I Believe in You 
artist :Neil Young 
link : http://www.last.fm/music/Neil+Young/_/I+Believe+in+You 
something: 0 
views: 90540 
linkimg : http://userserve-ak.last.fm/serve/34s/65285990.png 
.. 
.. 

回答

2

嘗試這樣:

private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance(); 
private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance(); 

public static void parseXml(String urlPath) throws Exception { 
    URL url = new URL(urlPath); 
    URLConnection connection = url.openConnection(); 
    DocumentBuilder db = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder(); 

    final Document document = db.parse(connection.getInputStream()); 
    XPath xPathEvaluator = XPATH_FACTORY.newXPath(); 

    NodeList tracksinfoNodes = (NodeList) xPathEvaluator.compile("lfm/results/trackmatches/track").evaluate(
      document, XPathConstants.NODESET); 
    for (int i = 0; i < tracksinfoNodes.getLength(); i++) { 
     Node trackNameNode = tracksinfoNodes.item(i); 

     NodeList childs = trackNameNode.getChildNodes(); 

     for (int j = 0; j < childs.getLength(); j++) { 
      Node n = childs.item(j); 

      if (!n.getNodeName().equals("#text")) { 
       System.out.println(String.format("%s: %s", n.getNodeName(), n.getTextContent())); 
      } 
     } 

     System.out.println("=============="); 
    } 

} 
+0

是那裏有反正我可以舉例來說,它的節點名稱得到的值,如果我想用歌曲名來替換標題例如 感謝 – Peril

+0

出現答案在códgio我提供的是等於你所需要的。我不明白評論中解釋的困難。 –