簡體   English   中英

使用xpath在java中解析XML

[英]XML parsing in java with 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

我正在使用的網址是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
..
..

嘗試這樣的事情:

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("==============");
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM