在 Java 中用属性解析 XML?

发布于 2024-12-08 21:03:48 字数 2069 浏览 0 评论 0原文

我有一个网址: http://ws.audioscrobbler.com/2.0/?method=chart.getlovedtracks&api_key=b25b959554ed76058ac220b7b2e0a026&page=3&limit=25 显示包含 A 歌曲名称信息的 XML 和艺术家。它还显示 3 个指向不同大小图像的链接,这些图像 url 由以下代码标识:

<image size="small">
<image size="large">
<image size="extralarge">  

这是我目前正在使用的代码,但我不知道如何仅获取中型图像的 url ???

private 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/tracks/track/name");
    NodeList trackNameNodes = (NodeList) nameExpr.evaluate(document, XPathConstants.NODESET);
    for (int i = 0; i < trackNameNodes.getLength(); i++) {
        Node trackNameNode = trackNameNodes.item(i);
        System.out.println(String.format("Track Name: %s" , trackNameNode.getTextContent()));

        XPathExpression artistImageExpr = xPathEvaluator.compile("following-sibling::/image");
        NodeList artistImageNodes = (NodeList) artistImageExpr.evaluate(trackNameNode, XPathConstants.NODESET);
        for (int j=0; j < artistImageNodes.getLength(); j++) {
            System.out.println(String.format(" -Image URL: %s", artistImageNodes.item(j).getTextContent()));
        }

        XPathExpression artistNameExpr = xPathEvaluator.compile("following-sibling::artist/name");
        NodeList artistNameNodes = (NodeList) artistNameExpr.evaluate(trackNameNode, XPathConstants.NODESET);
        for (int j=0; j < artistNameNodes.getLength(); j++) {
            System.out.println(String.format(" - Artist Name: %s", artistNameNodes.item(j).getTextContent()));
        }
    }
}

I have a url: http://ws.audioscrobbler.com/2.0/?method=chart.getlovedtracks&api_key=b25b959554ed76058ac220b7b2e0a026&page=3&limit=25 that display XML with information on A song name and artist. It also display 3 links to different sized images, these image urls are identified by

<image size="small">
<image size="large">
<image size="extralarge">  

Here is the code I am using at the moment however I do not know how to get the url for the medium sized image only ???

private 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/tracks/track/name");
    NodeList trackNameNodes = (NodeList) nameExpr.evaluate(document, XPathConstants.NODESET);
    for (int i = 0; i < trackNameNodes.getLength(); i++) {
        Node trackNameNode = trackNameNodes.item(i);
        System.out.println(String.format("Track Name: %s" , trackNameNode.getTextContent()));

        XPathExpression artistImageExpr = xPathEvaluator.compile("following-sibling::/image");
        NodeList artistImageNodes = (NodeList) artistImageExpr.evaluate(trackNameNode, XPathConstants.NODESET);
        for (int j=0; j < artistImageNodes.getLength(); j++) {
            System.out.println(String.format(" -Image URL: %s", artistImageNodes.item(j).getTextContent()));
        }

        XPathExpression artistNameExpr = xPathEvaluator.compile("following-sibling::artist/name");
        NodeList artistNameNodes = (NodeList) artistNameExpr.evaluate(trackNameNode, XPathConstants.NODESET);
        for (int j=0; j < artistNameNodes.getLength(); j++) {
            System.out.println(String.format(" - Artist Name: %s", artistNameNodes.item(j).getTextContent()));
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文