繁体   English   中英

如何使用Xpath解析复杂的XML

[英]How to parse a complex XML using Xpath

我有一个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>

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

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

如何为上述代码编写准确而完整的Java代码? 我只想提取<InquiryType>getParty</InquiryType>

尝试您的代码,对我来说似乎很好。 这是我所做的

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));
}

与test.xml完全包含您正在使用的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>

我正在使用以下方法:

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;

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM