0

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 Answer 1

2

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

is theres anyway I can for example , get the value by its node name , if I want to replace Title with song name for example thanks
The answer appears in códgio I have provided is equal to what you need. I did not understand the difficulties explained in the commentary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.