XML parsing in java with xpath

2020-02-16 03:35发布

问题:

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
..
..

回答1:

Try something like this:

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

}