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,
我想在java中使用XPath读取XML数据,因此对于我收集到的信息,我不能按照我的要求解析XML,
I just want to take the value of bankId
我只想取bankId的值
this the example of the xml
这是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,
我想在java中使用XPath读取XML数据,因此对于我收集到的信息,我不能按照我的要求解析XML,
I just want to take the value of 'bankId'
我想取bankId的值
this part of the example of the xml
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
这是我的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());
}
only this that appear when I run the code
只有当我运行代码时才会出现
hup hup /p:RawData/boList/boList/boList/bo/content[@xsi:type='mpnG2:PaymentReq']/bankId
玫瑰玫瑰/ p:RawData / boList / boList / boList /博/内容[@xsi:type = ' mpnG2:PaymentReq ']/ bankId
hup hup 2
玫瑰玫瑰2
any help will be pleasure :)
任何帮助都是愉快的。
2 个解决方案
#1
2
Try the following xpaths
试试以下xpath
//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
我已经在这个在线xml Xpath测试器中测试了xml的Xpath Xpath
#2
2
The XPath expression is wrong.
XPath表达式是错误的。
/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
XML中没有这样的元素。你想要的
/p:RawData/boList[3]/bo/content[@xsi:type='mpnG2:PaymentReq']/bankId
To select the 3rd boList.
选择第三个列表。
#1
2
Try the following xpaths
试试以下xpath
//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
我已经在这个在线xml Xpath测试器中测试了xml的Xpath Xpath
#2
2
The XPath expression is wrong.
XPath表达式是错误的。
/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
XML中没有这样的元素。你想要的
/p:RawData/boList[3]/bo/content[@xsi:type='mpnG2:PaymentReq']/bankId
To select the 3rd boList.
选择第三个列表。