java - 在 Java 中使用 XPath 获取当前节点值

标签 java parsing xpath last.fm

我正在用 Java 构建一个应用程序,但在获取某些值时遇到一些问题

这个想法是,我在云中有一个 XML 文档(在本例中为 Last.fm API),并且我想检索具有属性的节点的值。这个值是一个字符串,我想使用属性来获取它

Last.Fm XML 的示例如下:

<track>
  <id>1019817</id>
  <name>Believe</name>
  <mbid/>
  <url>http://www.last.fm/music/Cher/_/Believe</url>
  <duration>240000</duration>
  <streamable fulltrack="1">1</streamable>
  <listeners>69572</listeners>
  <playcount>281445</playcount>
  <artist>
    <name>Cher</name>
    <mbid>bfcc6d75-a6a5-4bc6-8282-47aec8531818</mbid>
    <url>http://www.last.fm/music/Cher</url>
  </artist>
  <album position="1">
    <artist>Cher</artist>
    <title>Believe</title>
    <mbid>61bf0388-b8a9-48f4-81d1-7eb02706dfb0</mbid>
    <url>http://www.last.fm/music/Cher/Believe</url>
    <image size="small">http://userserve-ak.last.fm/serve/34/8674593.jpg</image>
    <image size="medium">http://userserve-ak.last.fm/serve/64/8674593.jpg</image>
    <image size="large">http://userserve-ak.last.fm/serve/126/8674593.jpg</image>
  </album>
  <toptags>
    <tag>
      <name>pop</name>
      <url>http://www.last.fm/tag/pop</url>
    </tag>
    ...
  </toptags>
  <wiki>
    <published>Sun, 27 Jul 2008 15:44:58 +0000</published>
    <summary>...</summary>
    <content>...</content>
  </wiki>
</track>

所以我的想法是获取例如属性为“medium”的图像值

我使用 XMLPath 完成了以下代码:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document documento = builder.parse("http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key=" + apikey + "&artist=cher&track=believe");
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//lfm/track/album/image[@size='medium']");
NodeList nlLastFm = (NodeList) expr.evaluate(documento, XPathConstants.NODESET);
Element eLastFm = (Element) nlLastFm.item(0);
Log.i(TAG, "eLastFm: " + nlLastFm.item(0));
coverUrl = parser.getValue(eLastFm, "image");

但问题是它没有正确返回值。我搜索了很多其他相关帖子,但他们没有解决我的问题......

有人可以帮助我吗?

感谢您的帮助!

最佳答案

尝试:

String value = nlLastFm.item(0).getTextContent()

一些测试代码(相应地编辑了较小的 xml 和 xpath 部分)

public static void main(String[] args) throws ParserConfigurationException,
            SAXException, IOException, XPathExpressionException {
        String xml = "<album position=\"1\"><artist>Cher</artist><title>Believe</title><mbid>61bf0388-b8a9-48f4-81d1-7eb02706dfb0</mbid><url>http://www.last.fm/music/Cher/Believe</url><image size=\"small\">http://userserve-ak.last.fm/serve/34/8674593.jpg</image><image size=\"medium\">http://userserve-ak.last.fm/serve/64/8674593.jpg</image><image size=\"large\">http://userserve-ak.last.fm/serve/126/8674593.jpg</image></album>";

        InputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document documento = builder.parse(stream);
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("//album/image[@size='medium']");
        NodeList nlLastFm = (NodeList) expr.evaluate(documento,
                XPathConstants.NODESET);

        String coverUrl = nlLastFm.item(0).getTextContent();
        System.out.println(coverUrl);

    }

输出 http://userserve-ak.last.fm/serve/64/8674593.jpg

关于java - 在 Java 中使用 XPath 获取当前节点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17112512/

相关文章:

java - C++ 中是否有与 Java 中的 TreeMap 对应的类?

php str_getcsv 数组问题

php - 如何删除 DOM 元素标签但保留其内容?

java - 反序列化为使用 Gson 扩展 LinkedHashMap 的类

java - Dagger 2 : How to use injection with a Fragment

iOS PDF 解析 Type 1 字体指标

xpath:我如何查询一个名为 test 且根本没有属性的元素?

xml - XSLT - 如果属性值字符长度大于 10,则为 bool 值 true

java - 如何停止音乐剪辑?

java - Stax vs Sax vs DOM 解析器?