java中使用xpath解析XML

发布于 2024-12-08 07:57:32 字数 2042 浏览 0 评论 0原文

我正在尝试打印一些数据 所以我的代码是

 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

所以我想做的就是让每个人都独自一人,就像这

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪淡了忧伤 2024-12-15 07:57:32

尝试这样的事情:

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文