Xml namspace parsing using JDOM -
i trying read following xml string response using jdom have no idea how parse? can please me? trying following codes parse:
org.jdom.element rootnode = document.getrootelement(); list<?> list = rootnode.getchildren("quotationresponse"); for(int = 1 ; <= list.size() ; i++) { element node = (element) list.get(i); string documentdate = node.getattribute("documentdate"); string transactiontype = node.getattribute("transactiontype"); }
xml:
<?xml version="1.0" encoding="utf-8"?> <s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:body><vtenvelope xmlns="un:vtinc:o-series:tps:6:0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <login><username>user</username> <password>abcd</password> </login> <quotationresponse documentdate="2011-03-24" transactiontype="sale"><customer><destination taxareaid="1230000"><city>dallas</city> <maindivision>tx</maindivision> <subdivision>chester</subdivision> <postalcode>75038</postalcode> <country>usa</country> </destination> </customer> <subtotal>1000.0</subtotal> <total>1060.0</total> <totaltax>60.0</totaltax> <lineitem lineitemid="1" lineitemnumber="1" taxdate="2013-04-25"><product productclass="product class attribute value">product code value</product> <quantity>1.0</quantity> <fairmarketvalue>1000.0</fairmarketvalue> <unitprice>1000.0</unitprice> <extendedprice>1000.0</extendedprice> <taxes taxresult="taxable" taxtype="sales" situs="destination" taxcollectedfromparty="buyer"><jurisdiction jurisdictionlevel="state" jurisdictionid="3051">texas</jurisdiction> <calculatedtax>60.0</calculatedtax> <effectiverate>0.06</effectiverate> <taxable>1000.0</taxable> <imposition impositiontype="general sales , use tax">sales , use tax</imposition> <taxruleid>121</taxruleid> </taxes> <totaltax>60.0</totaltax> </lineitem> </quotationresponse> </vtenvelope></s:body></s:envelope>
you need use namespace-specific getchildren() method. namespace want "un:vtinc:o-series:tps:6:0"
namespace ns = namespace.getnamespace("un:vtinc:o-series:tps:6:0"); list<?> list = rootnode.getchildren("quotationresponse", ns);
if using jdom 2.x, second line be:
namespace ns = namespace.getnamespace("un:vtinc:o-series:tps:6:0"); list<element> list = rootnode.getchildren("quotationresponse", ns);
and whole thing be:
namespace ns = namespace.getnamespace("un:vtinc:o-series:tps:6:0"); for(element node : rootnode.getchildren("quotationresponse", ns)) { string documentdate = node.getattribute("documentdate"); string transactiontype = node.getattribute("transactiontype"); }
edit: ok, still having problems. see number of things wrong now.
you should using jdom 2.0.4. type-casting. somehow putting attribute object in string. should not possible compile!
string documentdate = node.getattributevalue("documentđate")
finally, quotationresponse not child of root element, of s:body.... , vtencelope. need access these right namespaces. need document structure right.