Note that there are some explanatory texts on larger screens.

plurals
  1. POissue parsing xml with namespaces using jdk xpath
    primarykey
    data
    text
    <p>I am having difficulty trying to parse an XML content with Xpath. Xml includes namespace information. I have tried to create a NameSpaceContextImp (apache WS commons implementations of NameSpaceContext interface in jdk) to map the namaspace prefixes to URIs, however couldn't successfully queried the xml document. When I use online xpath testing tool at <a href="http://chris.photobooks.com/xml/default.htm" rel="nofollow">http://chris.photobooks.com/xml/default.htm</a>, the xpath query I use comes up with my expected nodes/elements. So I am trying to figure out what it is that I am doing wrong. I am providing the xml document and the sample code snippet. I would appreciate any feedback. As a note, I have tried xpath queries both with and without the namespace prefixes. </p> <pre><code>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 that is 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() &gt; 0) { System.out.println(evaluation); } &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy"&gt; &lt;wsp:All wsp:Usage="Required"&gt; &lt;L7p:SetVariable&gt; &lt;L7p:AssertionComment assertionComment="included"&gt; &lt;L7p:Properties mapValue="included"&gt; &lt;L7p:entry&gt; &lt;L7p:key stringValue="RIGHT.COMMENT"/&gt; &lt;L7p:value stringValue="Used to enable message logging, Null (allow MSGDEBUG Header to set level), 0 - default,1 - Medium,2 - Full"/&gt; &lt;/L7p:entry&gt; &lt;/L7p:Properties&gt; &lt;/L7p:AssertionComment&gt; &lt;L7p:Base64Expression stringValue=""/&gt; &lt;L7p:VariableToSet stringValue="LOCAL_POLICY_DEBUG_LEVEL"/&gt; &lt;/L7p:SetVariable&gt; &lt;L7p:Include&gt; &lt;L7p:PolicyGuid stringValue="ec1f4166-4299-4e44-bf9d-c5c2a9f0c894"/&gt; &lt;/L7p:Include&gt; &lt;L7p:SslAssertion&gt; &lt;L7p:Option optionValue="Optional"/&gt; &lt;/L7p:SslAssertion&gt; &lt;wsp:OneOrMore L7p:Enabled="false" wsp:Usage="Required"&gt; &lt;L7p:SpecificUser&gt; &lt;L7p:Enabled booleanValue="false"/&gt; &lt;L7p:IdentityProviderOid longValue="-2"/&gt; &lt;L7p:UserLogin stringValue="test"/&gt; &lt;L7p:UserName stringValue="test"/&gt; &lt;L7p:UserUid stringValue="58916874"/&gt; &lt;/L7p:SpecificUser&gt; &lt;L7p:SpecificUser&gt; &lt;L7p:Enabled booleanValue="false"/&gt; &lt;L7p:IdentityProviderOid longValue="-2"/&gt; &lt;L7p:UserLogin stringValue="test"/&gt; &lt;L7p:UserName stringValue="test"/&gt; &lt;L7p:UserUid stringValue="58916873"/&gt; &lt;/L7p:SpecificUser&gt; &lt;L7p:SpecificUser&gt; &lt;L7p:Enabled booleanValue="false"/&gt; &lt;L7p:IdentityProviderOid longValue="-2"/&gt; &lt;L7p:UserLogin stringValue="test"/&gt; &lt;L7p:UserName stringValue="test"/&gt; &lt;L7p:UserUid stringValue="58916876"/&gt; &lt;/L7p:SpecificUser&gt; &lt;L7p:SpecificUser&gt; &lt;L7p:Enabled booleanValue="false"/&gt; &lt;L7p:IdentityProviderOid longValue="-2"/&gt; &lt;L7p:UserLogin stringValue="test"/&gt; &lt;L7p:UserName stringValue="test"/&gt; &lt;L7p:UserUid stringValue="58916875"/&gt; &lt;/L7p:SpecificUser&gt; &lt;L7p:SpecificUser&gt; &lt;L7p:Enabled booleanValue="false"/&gt; &lt;L7p:IdentityProviderOid longValue="-2"/&gt; &lt;L7p:UserLogin stringValue="testengineering-user"/&gt; &lt;L7p:UserName stringValue="testengineering-user"/&gt; &lt;L7p:UserUid stringValue="48201728"/&gt; &lt;/L7p:SpecificUser&gt; &lt;/wsp:OneOrMore&gt; &lt;wsp:OneOrMore wsp:Usage="Required"&gt; &lt;L7p:HttpRoutingAssertion&gt; &lt;L7p:ProtectedServiceUrl stringValue="http://localhost:13000/Services/Finance/v1"/&gt; &lt;L7p:RequestHeaderRules httpPassthroughRuleSet="included"&gt; &lt;L7p:Rules httpPassthroughRules="included"&gt; &lt;L7p:item httpPassthroughRule="included"&gt; &lt;L7p:Name stringValue="Cookie"/&gt; &lt;/L7p:item&gt; &lt;L7p:item httpPassthroughRule="included"&gt; &lt;L7p:Name stringValue="SOAPAction"/&gt; &lt;/L7p:item&gt; &lt;/L7p:Rules&gt; &lt;/L7p:RequestHeaderRules&gt; &lt;L7p:RequestParamRules httpPassthroughRuleSet="included"&gt; &lt;L7p:ForwardAll booleanValue="true"/&gt; &lt;L7p:Rules httpPassthroughRules="included"/&gt; &lt;/L7p:RequestParamRules&gt; &lt;L7p:ResponseHeaderRules httpPassthroughRuleSet="included"&gt; &lt;L7p:Rules httpPassthroughRules="included"&gt; &lt;L7p:item httpPassthroughRule="included"&gt; &lt;L7p:Name stringValue="Set-Cookie"/&gt; &lt;/L7p:item&gt; &lt;/L7p:Rules&gt; &lt;/L7p:ResponseHeaderRules&gt; &lt;/L7p:HttpRoutingAssertion&gt; &lt;L7p:Include&gt; &lt;L7p:PolicyGuid stringValue="b438384e-eeb0-45c5-8a7e-d30da78f07ee"/&gt; &lt;/L7p:Include&gt; &lt;/wsp:OneOrMore&gt; &lt;/wsp:All&gt; &lt;/wsp:Policy&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload