java - XML Parsing with XPath -
so pretty new xml parsing, looked around , found info of value me. came across called xpath , thought might work. unfortunately, having trouble it.
currently trying string using line of code:
string summaryfedex = (string) xpath.evaluate("/soap-env:envelope/soap-env:body/v4:trackreply/v4:highestseverity", dom, xpathconstants.string);
before ask, dom
fine. leaves me believe expression wrong.
below have xml trying parse. maybe give assistance?
i should note: trying "v4:highestseverity" data.
xml:
<?xml version="1.0" encoding="utf-8"?> <soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> <soap-env:header /> <soap-env:body> <v4:trackreply xmlns:v4="http://fedex.com/ws/track/v4"> <v4:highestseverity>error</v4:highestseverity> <v4:notifications> <v4:severity>error</v4:severity> <v4:source>prof</v4:source> <v4:code>1000</v4:code> <v4:message>authentication failed</v4:message> </v4:notifications> <v4:transactiondetail> <v4:customertransactionid>tc030_wsvc_track_v4 _pos</v4:customertransactionid> <v4:localization> <v4:languagecode>en</v4:languagecode> </v4:localization> </v4:transactiondetail> <v4:version> <v4:serviceid>trck</v4:serviceid> <v4:major>4</v4:major> <v4:intermediate>0</v4:intermediate> <v4:minor>0</v4:minor> </v4:version> </v4:trackreply> </soap-env:body> </soap-env:envelope>
if want read data , won't have conflicts ignoring namespace, can use xpath expression ignores namespaces, this:
"/*[local-name()='envelope']/*[local-name()='body']/*[local-name()='trackreply']/*[local-name()='highestseverity']"
it's same xpath selecting *
element, , adding predicate [...]
limit elements ones have matching local name.
if need preserve namespaces, have register prefixes. can implementing namespacecontext
, setting xpath
object reference before performing search:
xpath.setnamespacecontext(new namespacecontext() { public string getnamespaceuri(string prefix) { if (prefix.equals("soap-env")) { return "http://schemas.xmlsoap.org/soap/envelope/"; } else if (prefix.equals("v4")) { return "http://fedex.com/ws/track/v4"; } else { return xmlconstants.null_ns_uri; } } public string getprefix(string namespaceuri) { return null;} public iterator getprefixes(string namespaceuri) { return null;} });
Comments
Post a Comment