Monday 26 August 2013

extract string from xml java

extract string from xml java

I receive XML String from Client as
<?xml version="1.0" encoding="UTF-8"?>
<Esp:Interface Version="1.0" xmlns:Esp="http://***Confidential****/">
<Esp:Transaction ActionCode="APPROVE" CardNumber="474849********28"
ExpiryDate="0824"
ResponseCode="00" TerminalId="03563001"
Track2="474849********28=08241261234"
TransactionAmount="300" TransactionId="00051" Type="PURCHASE"
LocalDate="0823"
LocalTime="152006" PosOperatorId="1234" AuthorizationNumber="123456"
MessageType="REFERAL">
</Esp:Transaction>
</Esp:Interface>
I want to extract each string like
ActionCode="APPROVE"
CardNumber="474849********28"
ExpiryDate="0824"
ResponseCode="00"
TerminalId="03563001"
Track2="474849********28=08241261234"
TransactionAmount="300"
TransactionId="00051"
Type="PURCHASE"
LocalDate="0823"
LocalTime="152006"
PosOperatorId="1234"
AuthorizationNumber="123456"
MessageType="REFERAL"
I used XPath to read XML String but it gives me error
InputSource source = new InputSource(new
StringReader(sXMLData));//sXMLData Contains XML String
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(source);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
String msg = xpath.evaluate("/Esp:Interface/Esp:Transaction", document);
System.out.println("msg= " + msg);
Error :
msg : /* Coming Blank */
I referred these two links but didnt get help as much Parse XML Simple
String using Java XPath And Simplest way to query XML in Java
How can i get these individual strings from XML ... by using build in XML
or old substring method (inefficient)

1 comment:

  1. BetRivers Casino Hotel in New Orleans, LA - JTM Hub
    Get the best 거제 출장마사지 deals on BetRivers Casino Hotel in 제주도 출장안마 New Orleans, LA. 대전광역 출장샵 The BetRivers 안산 출장마사지 Casino Hotel offers a 충주 출장샵 convenient way to get from the

    ReplyDelete