如何使用Xpath解析复杂的XML

时间:2022-08-01 17:02:48

I have an XML as below :

我有一个XML如下:

<Service xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="someurl" xsi:schemaLocation="someurl Sample.xsd">
    <RequestControl>
        <requestID>100129</requestID>
        <Control>
            <requesterName>Admin</requesterName>
            <requesterLanguage>100</requesterLanguage>
        </Control>
    </RequestControl>
    <Inquiry>
        <InquiryType>getParty</InquiryType>
        <InquiryParam>
            <Param name="PartyId">854850029276139020</Param>
        </InquiryParam>
    </Inquiry>
</Service>

I want to extract the value "getParty" from tag using XPath XML Parser. I am using the below as my expression :

我想使用XPath XML Parser从标记中提取值“getParty”。我使用以下作为我的表达:

expression = xPath.compile("/Service/Inquiry/InquiryType/text()");

How can I write the accurate and complete java code for the above? I just want to extract the value for <InquiryType>getParty</InquiryType>.

如何为上述内容编写准确完整的java代码?我只想提取 getParty 的值。

2 个解决方案

#1


1  

Trying your code, it looks like it is working fine for me. Here is what I did

尝试你的代码,看起来它对我来说很好。这就是我做的

public static void main(String ... args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(System.getProperty("user.dir") + "/src/main/resources/test.xml");
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expression = xpath.compile("/Service/Inquiry/InquiryType/text()");
    NodeList xpathNodeList = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
    System.out.println("InquiryType is : " +xpathNodeList.item(0));
}

With test.xml containing exactly the xml you are using

使用包含您正在使用的xml的test.xml

<Service xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="someurl" xsi:schemaLocation="someurl Sample.xsd">
<RequestControl>
    <requestID>100129</requestID>
    <Control>
        <requesterName>Admin</requesterName>
        <requesterLanguage>100</requesterLanguage>
    </Control>
</RequestControl>
<Inquiry>
    <InquiryType>getParty</InquiryType>
    <InquiryParam>
        <Param name="PartyId">854850029276139020</Param>
    </InquiryParam>
</Inquiry>
</Service>

#2


1  

I am using the below method :

我使用以下方法:

public static String inputXmlXPathParser(String inputXml){


        //==================================================X-Path Parser =============================================================//

        String transactionName = StringUtils.EMPTY;

        try {

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setNamespaceAware(true);
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new InputSource( new StringReader(inputXml)));
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            XPathExpression expression = xpath.compile("/Service/Inquiry/InquiryType/text()");
            NodeList xpathNodeList = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
            System.out.println("InquiryType is : " +xpathNodeList.item(0));

        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    


        return transactionName;

    }

#1


1  

Trying your code, it looks like it is working fine for me. Here is what I did

尝试你的代码,看起来它对我来说很好。这就是我做的

public static void main(String ... args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(System.getProperty("user.dir") + "/src/main/resources/test.xml");
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expression = xpath.compile("/Service/Inquiry/InquiryType/text()");
    NodeList xpathNodeList = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
    System.out.println("InquiryType is : " +xpathNodeList.item(0));
}

With test.xml containing exactly the xml you are using

使用包含您正在使用的xml的test.xml

<Service xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="someurl" xsi:schemaLocation="someurl Sample.xsd">
<RequestControl>
    <requestID>100129</requestID>
    <Control>
        <requesterName>Admin</requesterName>
        <requesterLanguage>100</requesterLanguage>
    </Control>
</RequestControl>
<Inquiry>
    <InquiryType>getParty</InquiryType>
    <InquiryParam>
        <Param name="PartyId">854850029276139020</Param>
    </InquiryParam>
</Inquiry>
</Service>

#2


1  

I am using the below method :

我使用以下方法:

public static String inputXmlXPathParser(String inputXml){


        //==================================================X-Path Parser =============================================================//

        String transactionName = StringUtils.EMPTY;

        try {

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setNamespaceAware(true);
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new InputSource( new StringReader(inputXml)));
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

            XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            XPathExpression expression = xpath.compile("/Service/Inquiry/InquiryType/text()");
            NodeList xpathNodeList = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);
            System.out.println("InquiryType is : " +xpathNodeList.item(0));

        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    


        return transactionName;

    }