Xpath Java无法获取URL

时间:2019-02-05 13:10:32

标签: java xml xpath

Xpath似乎不起作用。我已经尝试了一些方法,但是似乎没有任何效果。我究竟做错了什么?

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(result)));
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//cm:URL/@value");
String msg = expr.evaluate(doc, XPathConstants.STRING).toString();       
logger.debug(msg);

我拥有的XML如下:

<ItemXML xmlns="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:ns2="http://www.ibm.com/xmlns/db2/cm/api/1.0/schema">
    <DOCUMENTS SCA_DATE="#" SCA_NR="#" cm:PID="#" xmlns:cm="http://www.ibm.com/xmlns/db2/cm/api/1.0/schema" xmlns:ns1="http://www.ibm.com/xmlns/db2/cm/beans/1.0/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <cm:properties type="#">
            <cm:lastChangeUserid value="#"/>
            <cm:lastChangeTime value="#"/>
            <cm:createUserid value="#"/>
            <cm:createTime value="#"/>
            <cm:semanticType value="#"/>
            <cm:ACL name="#"/>
            <cm:lastOperation name="#" value="#"/>
        </cm:properties>
        <ns1:CONTRACTS AM="#"/>
        <ns1:BASE cm:PID="#" cm:partNumber="#">
            <cm:properties type="item" xsi:type="#">
                <cm:lastChangeUserid value="#"/>
                <cm:lastChangeTime value="#"/>
                <cm:createUserid value="#"/>
                <cm:createTime value="#"/>
                <cm:semanticType value="#"/>
                <cm:ACL name="#"/>
                <cm:lastOperation name="#" value="#"/>
            </cm:properties>
            <cm:resourceObject MIMEType="application/pdf" RMName="#" SMSCollName="#" externalObjectName="" originalFileName="#" resourceFlag="#" resourceName="" size="#">
                <cm:URL value="https://testurl.com"/>
            </cm:resourceObject>
        </ns1:BASE>
    </DOCUMENTS>
</ItemXML>

我希望将 cm:URL -> https://testurl.com的值另存为String。 重要提示:无论xml的结构如何,Xpath都应该找到该值。

3 个答案:

答案 0 :(得分:0)

无需使用XPath,您可以执行以下操作:

NodeList elementsByTagName = doc.getDocumentElement().getElementsByTagName("cm:URL");
System.out.println("result: " + elementsByTagName.item(0).getAttributes().getNamedItem("value").getNodeValue());

答案 1 :(得分:0)

以下行中有问题:-

Document doc = builder.parse(new InputSource(new StringReader(result)));

我尝试打印文档,它给我的输出为[#document:null],您能否先尝试在控制台中打印文档,让我知道它是否不为空。

答案 2 :(得分:0)

按照@bangnab的建议使用他的解决方案,我尝试了它的工作

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XpathTester {

    public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
        File inputFile = new File("C:\\Users\\Arvind.Carpenter\\Desktop\\input.txt");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputFile);
        doc.getDocumentElement().normalize();
        NodeList elementsByTagName = doc.getDocumentElement().getElementsByTagName("cm:URL");

        //you can iterate over this node list and get all the URL i am printing first one

        System.out.println("result: " + elementsByTagName.item(0).getAttributes().getNamedItem("value").getNodeValue()); 
    }

}