splatter_fadli
splatter_fadli

Reputation: 781

How to read xml using XPATH in Java

I want to read XML data using XPath in java, so for the information I have gathered I am not able to parse xml according to my requirement,

I just want to take the value of bankId

this the example of the xml

I want to read XML data using XPath in java, so for the information I have gathered I am not able to parse xml according to my requirement,

I just want to take the value of 'bankId'

this part of the example of the 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>

this is my java code

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

only this that appear when I run the code

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

hup hup 2

any help will be pleasure :)

Upvotes: 1

Views: 597

Answers (2)

Madhan
Madhan

Reputation: 5818

Try the following xpaths

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

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

 //bankId or

 //bankId[1]

I've tested the xpaths for your xml in this online Xml Xpath tester

Upvotes: 2

cvesters
cvesters

Reputation: 696

The XPath expression is wrong.

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

would select the element at:

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

There is no such element in the XML. You want

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

To select the 3rd boList.

Upvotes: 2

Related Questions