如何在Java中使用XPATH读取xml

时间:2015-07-01 18:51:30

标签: java xml xpath

我想在java中使用XPath读取XML数据,因此对于我收集的信息,我无法根据我的要求解析xml,

我只想取bankId的值

这是xml的例子

我想在java中使用XPath读取XML数据,因此对于我收集的信息,我无法根据我的要求解析xml,

我只想取'bankId'的价值

这部分xml的例子

<?xml version="1.0" encoding="UTF-8"?>
<p:RawData xsi:type="p:RawData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ba="http://service.bni.co.id/bancslink"   xmlns:bo="http://service.bni.co.id/core/bo" xmlns:core="http://service.bni.co.id/core" xmlns:mhp="http://service.bni.co.id/mhp" xmlns:mpnG2="http://service.bni.co.id/mhp/mpn_g2" xmlns:mpnG2_1="http://service.bni.co.id/bancslink/mpn_g2" xmlns:p="http://BP_MultiHostPayment">
  <boList>
  <name>McpRequest</name>
  <bo xsi:type="bo:CommonBillPaymentReq">
  <billerCode>0128</billerCode>
  <regionCode>0001</regionCode>
  <billerName>MPN G2 IDR</billerName>
  <billingNum>820150629548121</billingNum>
  <customerName>EVA RAJAGUKGUK  SH.  M.KN                         </customerName>
  <paymentMethod>2</paymentMethod>
  <accountNum>0373163437</accountNum>
  <trxAmount>50000</trxAmount>
  <naration>820150629548121</naration>
  <invoiceNum>820150629548121</invoiceNum>
  <billInvoiceNum1>INVNUM1</billInvoiceNum1>
  <billAmount1>0</billAmount1>
  <billInvoiceNum2>INVNUM2</billInvoiceNum2>
  <billAmount2>0</billAmount2>
  <billInvoiceNum3>INVNUM3</billInvoiceNum3>
  <billAmount3>0</billAmount3>
  <isDecAmount>false</isDecAmount>
  <trxDecAmount>50000</trxDecAmount>
</bo>
 </boList>
 <boList>
   <name>McpTellerHeader</name>
   <bo xsi:type="core:TellerHeader">
     <tellerId>00004</tellerId>
     <branchCode>0997</branchCode>
     <overrideFlag>I</overrideFlag>
   </bo>
 </boList>
 <boList>
   <name>HostRequest</name>
   <bo xsi:type="mhp:Request">
     <header>
       <hostId>MPN_G2</hostId>
       <channelId>ATM</channelId>
       <branchId></branchId>
       <terminalId></terminalId>
       <locationId></locationId>
       <transDateTime>2015-06-30T22:26:33</transDateTime>
       <transId>20150630T222633N042462J0000001969R840SLEXI0</transId>
      </header>
      <content xsi:type="mpnG2:PaymentReq">
        <bankId>520009000990</bankId>
        <billingInfo1>013</billingInfo1>
        <billingInfo2>03</billingInfo2>
        <billingInfo3>409257</billingInfo3>
        <branchCode>0997</branchCode>
        <channelType>7010</channelType>
        <currency>IDR</currency>
        <gmt>2015-06-30T15:26:33.942</gmt>
        <localDatetime>2015-06-30T22:26:33.943</localDatetime>
        <settlementDate>0701</settlementDate>
        <switcherCode>001</switcherCode>
    <terminalId>S1HKWGA032</terminalId>
    <terminalLocation>0997</terminalLocation>
    <transactionId>013978</transactionId>
    <amount>50000</amount>
    <billerAccountNumber>3010194605</billerAccountNumber>
    <customerName>EVA RAJAGUKGUK  SH.  M.KN                         </customerName>
    <ntb>000000058111</ntb>
    <paymentCode>820150629548121</paymentCode>
      </content>
    </bo>
  </boList>
   </p:RawData>

这是我的java代码

try {

        FileInputStream file = new FileInputStream(new File("C:/Log/contoh.xml"));
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document xmlDocument = builder.parse(file);

        XPath xPath = XPathFactory.newInstance().newXPath();

        System.out.println("hup hup");
        String expression = "/p:RawData/boList/boList/boList/bo/content[@xsi:type='mpnG2:PaymentReq']/bankId";
        System.out.println(expression);
        String bankId = xPath.compile(expression).evaluate(xmlDocument);
        System.out.println(bankId);

        System.out.println("hup hup 2");
        expression = "/p:RawData/boList/boList/boList/bo/content[@xsi:type='mpnG2:PaymentReq']/bankId";
        NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
        for (int i=0; i < nodeList.getLength(); i++){
            System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
        }

仅在我运行代码

时出现

hup hup / P:RAWDATA / boList / boList / boList / BO /内容[@xsi:类型= 'mpnG2:PaymentReq'] / bankId

hup hup 2

任何帮助都会很愉快:)

2 个答案:

答案 0 :(得分:2)

XPath表达式错误。

/p:RawData/boList/boList/boList/bo/content[@xsi:type='mpnG2:PaymentReq']/bankId

将选择以下元素:

<p:RawData>
  <boList>
    <boList>
      <boList>
        <bo>
          <content xsi:type="mpnG2:PaymentReq" />
        </bo>
      </boList>
    </boList>
  </boList>
</RawData>

XML中没有这样的元素。 你想要

/p:RawData/boList[3]/bo/content[@xsi:type='mpnG2:PaymentReq']/bankId

选择第3个boList。

答案 1 :(得分:2)

尝试以下xpaths

 //content[@xsi:type='mpnG2:PaymentReq']/bankId or  

 //content[@xsi:type='mpnG2:PaymentReq'][1]/bankId or

 //bankId or

 //bankId[1]

我已在此在线Xml Xpath tester

测试了xml的xpath