使用XPath解析XML字符串数据的问题

时间:2021-08-03 16:55:57

I'm trying to use XPath to parse an XML string but I am getting only null values back. Does anyone have an idea where I might be going wrong in the code shown below?

我正在尝试使用XPath来解析XML字符串,但我只返回null值。有没有人知道下面的代码我可能会出错?

public static void main(String[] args) {
    String content = "<imagesXML><Images><Image><ImageUID Scope='Public' Type='Guid' Value='{7f2535d0-9a41-4997-9694-0a4de569e6d9}'/><CorbisID Scope='Public' Type='String' Value='42-15534232'/><Title Scope='Public' Type='String' Value='Animal'/><CreditLine Scope='Public' Type='String' Value='© Robert Llewellyn/Corbis'/><IsRoyaltyFree Scope='Public' Type='Boolean' Value='False'/><AspectRatio Scope='Public' Type='String' Value='1.500000'/><URL128 Scope='Public' Type='String' Value='http://cachens.corbis.com/CorbisImage/thumb/15/53/42/15534232/42-15534232.jpg'/></Image></Images></imagesXML>";
    InputSource source = new InputSource(new StringReader(content));
    XPath xPath = XPathFactory.newInstance().newXPath();
    NodeList list = null;
    try {
        list = (NodeList) xPath.evaluate("//URL128[@Value]", source,
                XPathConstants.NODESET);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
    for (int i = 0; i < list.getLength(); i++) {
        System.out.println(list.item(i));
    }
}

The output from System.out is 'value=[URL128: null]', but it should be the URL I am trying to extract: http://cachens.corbis.com/CorbisImage/thumb/15/53/42/15534232/42-15534232.jpg.

System.out的输出是'value = [URL128:null]',但它应该是我想要提取的URL:http://cachens.corbis.com/CorbisImage/thumb/15/53/42/15534232 /42-15534232.jpg。

Any help appreciated, thanks.

任何帮助表示感谢,谢谢。

2 个解决方案

#1


5  

What if you try changing your XPath evaluation statement from this:

如果您尝试更改此XPath评估语句,该怎么办:

list = (NodeList)xPath.evaluate("//URL128[@Value]", 
    source, XPathConstants.NODESET);

to this:

list = (NodeList) xPath.evaluate("//URL128/@Value", 
    source, XPathConstants.NODESET);

#2


1  

Note:

  • The expression //URL128[@Value] returns a list of all URL123 elements having a Value attribute
  • 表达式// URL128 [@Value]返回具有Value属性的所有URL123元素的列表

  • The expression //URL128/@Value returns a list of the Value attributes from each URL128 element in the source
  • 表达式// URL128 / @ Value返回源中每个URL128元素的Value属性列表

Neither of these lists contain strings; they contain DOM types. You've got only one URL128 element in your source, yet you're asking for a NodeList. You could simplify by using the following:

这些列表都不包含字符串;它们包含DOM类型。您的源中只有一个URL128元素,但您要求的是NodeList。您可以使用以下内容进行简化:

String url = xPath.evaluate("//URL128/@Value", source);

#1


5  

What if you try changing your XPath evaluation statement from this:

如果您尝试更改此XPath评估语句,该怎么办:

list = (NodeList)xPath.evaluate("//URL128[@Value]", 
    source, XPathConstants.NODESET);

to this:

list = (NodeList) xPath.evaluate("//URL128/@Value", 
    source, XPathConstants.NODESET);

#2


1  

Note:

  • The expression //URL128[@Value] returns a list of all URL123 elements having a Value attribute
  • 表达式// URL128 [@Value]返回具有Value属性的所有URL123元素的列表

  • The expression //URL128/@Value returns a list of the Value attributes from each URL128 element in the source
  • 表达式// URL128 / @ Value返回源中每个URL128元素的Value属性列表

Neither of these lists contain strings; they contain DOM types. You've got only one URL128 element in your source, yet you're asking for a NodeList. You could simplify by using the following:

这些列表都不包含字符串;它们包含DOM类型。您的源中只有一个URL128元素,但您要求的是NodeList。您可以使用以下内容进行简化:

String url = xPath.evaluate("//URL128/@Value", source);