java中使用xpath解析XML
我正在尝试打印一些数据 所以我的代码是
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
所以我想做的就是让每个人都独自一人,就像这
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
..
..
I am trying to print out some data
so my code is
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()));
}
}
so it will print me like this for the first loop
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
the url I am using is http://ws.audioscrobbler.com/2.0/?method=track.search&track=Believe&api_key=b25b959554ed76058ac220b7b2e0a026
so what I am trying to do is to get eveyone alone , like this
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
..
..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样的事情:
Try something like this: