如何使用Xpath解析复杂的XML

时间:2017-08-06 19:07:32

标签: java xml xpath saxparser

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

2 个答案:

答案 0 :(得分:1)

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

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

使用包含正在使用的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>

答案 1 :(得分:1)

我使用以下方法:

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;

    }