java - issue parsing xml with namespaces using jdk xpath -


i having difficulty trying parse xml content xpath. xml includes namespace information. have tried create namespacecontextimp (apache ws commons implementations of namespacecontext interface in jdk) map namaspace prefixes uris, couldn't queried xml document. when use online xpath testing tool @ http://chris.photobooks.com/xml/default.htm, xpath query use comes expected nodes/elements. trying figure out doing wrong. providing xml document , sample code snippet. appreciate feedback. note, have tried xpath queries both , without namespace prefixes.

namespacecontextimpl namespacecontext = new namespacecontextimpl(); namespacecontext.startprefixmapping("wsp", "http://schemas.xmlsoap.org/ws/2002/12/policy"); namespacecontext.startprefixmapping("l7p", "http://www.layer7tech.com/ws/policy"); string policyxml = "xml content pasted below" inputsource inputsource = new inputsource(new stringreader(policyxml)); xpathfactory xpathfactory = xpathfactory.newinstance(); xpath xpath = xpathfactory.newxpath(); xpath.setnamespacecontext(namespacecontext); xpathexpression xpathexpression = xpath.compile("/wsp:policy/wsp:all"); string evaluation = xpathexpression.evaluate(inputsource); if (evaluation.trim().length() > 0) {     system.out.println(evaluation); }       <?xml version="1.0" encoding="utf-8"?> <wsp:policy xmlns:l7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">     <wsp:all wsp:usage="required">         <l7p:setvariable>             <l7p:assertioncomment assertioncomment="included">                 <l7p:properties mapvalue="included">                     <l7p:entry>                         <l7p:key stringvalue="right.comment"/>                         <l7p:value stringvalue="used enable message logging, null (allow msgdebug header set level), 0 - default,1 - medium,2 - full"/>                     </l7p:entry>                 </l7p:properties>             </l7p:assertioncomment>             <l7p:base64expression stringvalue=""/>             <l7p:variabletoset stringvalue="local_policy_debug_level"/>         </l7p:setvariable>         <l7p:include>             <l7p:policyguid stringvalue="ec1f4166-4299-4e44-bf9d-c5c2a9f0c894"/>         </l7p:include>         <l7p:sslassertion>             <l7p:option optionvalue="optional"/>         </l7p:sslassertion>         <wsp:oneormore l7p:enabled="false" wsp:usage="required">             <l7p:specificuser>                 <l7p:enabled booleanvalue="false"/>                 <l7p:identityprovideroid longvalue="-2"/>                 <l7p:userlogin stringvalue="test"/>                 <l7p:username stringvalue="test"/>                 <l7p:useruid stringvalue="58916874"/>             </l7p:specificuser>             <l7p:specificuser>                 <l7p:enabled booleanvalue="false"/>                 <l7p:identityprovideroid longvalue="-2"/>                 <l7p:userlogin stringvalue="test"/>                 <l7p:username stringvalue="test"/>                 <l7p:useruid stringvalue="58916873"/>             </l7p:specificuser>             <l7p:specificuser>                 <l7p:enabled booleanvalue="false"/>                 <l7p:identityprovideroid longvalue="-2"/>                 <l7p:userlogin stringvalue="test"/>                 <l7p:username stringvalue="test"/>                 <l7p:useruid stringvalue="58916876"/>             </l7p:specificuser>             <l7p:specificuser>                 <l7p:enabled booleanvalue="false"/>                 <l7p:identityprovideroid longvalue="-2"/>                 <l7p:userlogin stringvalue="test"/>                 <l7p:username stringvalue="test"/>                 <l7p:useruid stringvalue="58916875"/>             </l7p:specificuser>             <l7p:specificuser>                 <l7p:enabled booleanvalue="false"/>                 <l7p:identityprovideroid longvalue="-2"/>                 <l7p:userlogin stringvalue="testengineering-user"/>                 <l7p:username stringvalue="testengineering-user"/>                 <l7p:useruid stringvalue="48201728"/>             </l7p:specificuser>         </wsp:oneormore>         <wsp:oneormore wsp:usage="required">             <l7p:httproutingassertion>                 <l7p:protectedserviceurl stringvalue="http://localhost:13000/services/finance/v1"/>                 <l7p:requestheaderrules httppassthroughruleset="included">                     <l7p:rules httppassthroughrules="included">                         <l7p:item httppassthroughrule="included">                             <l7p:name stringvalue="cookie"/>                         </l7p:item>                         <l7p:item httppassthroughrule="included">                             <l7p:name stringvalue="soapaction"/>                         </l7p:item>                     </l7p:rules>                 </l7p:requestheaderrules>                 <l7p:requestparamrules httppassthroughruleset="included">                     <l7p:forwardall booleanvalue="true"/>                     <l7p:rules httppassthroughrules="included"/>                 </l7p:requestparamrules>                 <l7p:responseheaderrules httppassthroughruleset="included">                     <l7p:rules httppassthroughrules="included">                         <l7p:item httppassthroughrule="included">                             <l7p:name stringvalue="set-cookie"/>                         </l7p:item>                     </l7p:rules>                 </l7p:responseheaderrules>             </l7p:httproutingassertion>             <l7p:include>                 <l7p:policyguid stringvalue="b438384e-eeb0-45c5-8a7e-d30da78f07ee"/>             </l7p:include>         </wsp:oneormore>     </wsp:all> </wsp:policy> 

the cdata under all element whitespace, trim. if want dom elements, use node or nodeset (nodelist) options.

string xml = "<foo><bar baz='hello' /></foo>"; inputsource src = new inputsource(new stringreader(xml)); xpathexpression expr = xpathfactory.newinstance().newxpath()     .compile("/foo/bar"); node node = (node) expr.evaluate(src, xpathconstants.node);  transformer trans = transformerfactory.newinstance().newtransformer(); trans.setoutputproperty(outputkeys.omit_xml_declaration, "yes"); trans.transform(new domsource(node), new streamresult(system.out)); 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -