猿问

Xpath Java 无法获取 URL

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:URL --> https://testurl.com的值保存为字符串。重要提示:无论 xml 结构如何,Xpath 都应该找到该值。


holdtom
浏览 114回答 3
3回答

肥皂起泡泡

在不使用 XPath 的情况下,您可以:NodeList elementsByTagName = doc.getDocumentElement().getElementsByTagName("cm:URL");System.out.println("result: " + elementsByTagName.item(0).getAttributes().getNamedItem("value").getNodeValue());

宝慕林4294392

使用他的解决方案,我尝试了它的工作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 {&nbsp; &nbsp; public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; File inputFile = new File("C:\\Users\\Arvind.Carpenter\\Desktop\\input.txt");&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();&nbsp; &nbsp; &nbsp; &nbsp; factory.setNamespaceAware(true);&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilder builder = factory.newDocumentBuilder();&nbsp; &nbsp; &nbsp; &nbsp; Document doc = builder.parse(inputFile);&nbsp; &nbsp; &nbsp; &nbsp; doc.getDocumentElement().normalize();&nbsp; &nbsp; &nbsp; &nbsp; NodeList elementsByTagName = doc.getDocumentElement().getElementsByTagName("cm:URL");&nbsp; &nbsp; &nbsp; &nbsp; //you can iterate over this node list and get all the URL i am printing first one&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("result: " + elementsByTagName.item(0).getAttributes().getNamedItem("value").getNodeValue());&nbsp;&nbsp; &nbsp; }}

繁星淼淼

以下行有问题:-Document doc = builder.parse(new InputSource(new StringReader(result)));我尝试打印文档,它给我的输出为 [#document: null],你可以先尝试在控制台中打印文档并让我知道它是否不为空。
随时随地看视频慕课网APP

相关分类

Java
我要回答