extract string from xml java -
i receive xml string client
<?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 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
read xml string 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 2 links didnt parse xml simple string using java xpath , simplest way query xml in java
how can these individual strings xml ... using build in xml
or old substring
method (inefficient)
that sounds have forbidden characters before <?xml ...?>
. make sure file correctly saved utf-8 , no whitespace before declaration.
to parse attributes, in simple case, i'd recommend sax parser:
public class mysaxhandler extends defaulthandler { public static void main(string[] args) throws throwable { string sxmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + "<esp:interface version=\"1.0\" xmlns:esp=\"http://***confidential****/\">\r\n" + "<esp:transaction actioncode=\"approve\" cardnumber=\"474849********28\" expirydate=\"0824\" \r\n" + " responsecode=\"00\" terminalid=\"03563001\" track2=\"474849********28=08241261234\" \r\n" + " transactionamount=\"300\" transactionid=\"00051\" type=\"purchase\" localdate=\"0823\" \r\n" + " localtime=\"152006\" posoperatorid=\"1234\" authorizationnumber=\"123456\" messagetype=\"referal\">\r\n" + "</esp:transaction>\r\n" + "</esp:interface>"; inputsource source = new inputsource(new stringreader(sxmldata)); xmlreader reader = xmlreaderfactory.createxmlreader(); reader.setcontenthandler(new mysaxhandler()); reader.parse(source); } @override public void startelement(string uri, string localname, string qname, attributes atts) throws saxexception { if (qname.equals("esp:transaction")) { (int = 0; < atts.getlength(); i++) { system.out.println(atts.getqname(i) + "=\"" + atts.getvalue(i) + "\""); } } } }
gives output:
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"
Comments
Post a Comment