issue parsing xml with namespaces using jdk xpath

ben76213

New Member
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 http://chris.photobooks.com/xml/default.htm, 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. \[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() > 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="http://stackoverflow.com/questions/10903942/included"> <L7p:entry> <L7p:key stringValue="http://stackoverflow.com/questions/10903942/RIGHT.COMMENT"/> <L7p:value stringValue="http://stackoverflow.com/questions/10903942/Used to enable message logging, Null (allow MSGDEBUG Header to set level), 0 - default,1 - Medium,2 - Full"/> </L7p:entry> </L7p:Properties> </L7p:AssertionComment> <L7p:Base64Expression stringValue=""/> <L7p:VariableToSet stringValue="http://stackoverflow.com/questions/10903942/LOCAL_POLICY_DEBUG_LEVEL"/> </L7p:SetVariable> <L7p:Include> <L7p:PolicyGuid stringValue="http://stackoverflow.com/questions/10903942/ec1f4166-4299-4e44-bf9d-c5c2a9f0c894"/> </L7p:Include> <L7p:SslAssertion> <L7p:Option optionValue="http://stackoverflow.com/questions/10903942/Optional"/> </L7p:SslAssertion> <wsp:OneOrMore L7p:Enabled="false" wsp:Usage="Required"> <L7p:SpecificUser> <L7p:Enabled booleanValue="http://stackoverflow.com/questions/10903942/false"/> <L7p:IdentityProviderOid longValue="http://stackoverflow.com/questions/10903942/-2"/> <L7p:UserLogin stringValue="http://stackoverflow.com/questions/10903942/test"/> <L7p:UserName stringValue="http://stackoverflow.com/questions/10903942/test"/> <L7p:UserUid stringValue="http://stackoverflow.com/questions/10903942/58916874"/> </L7p:SpecificUser> <L7p:SpecificUser> <L7p:Enabled booleanValue="http://stackoverflow.com/questions/10903942/false"/> <L7p:IdentityProviderOid longValue="http://stackoverflow.com/questions/10903942/-2"/> <L7p:UserLogin stringValue="http://stackoverflow.com/questions/10903942/test"/> <L7p:UserName stringValue="http://stackoverflow.com/questions/10903942/test"/> <L7p:UserUid stringValue="http://stackoverflow.com/questions/10903942/58916873"/> </L7p:SpecificUser> <L7p:SpecificUser> <L7p:Enabled booleanValue="http://stackoverflow.com/questions/10903942/false"/> <L7p:IdentityProviderOid longValue="http://stackoverflow.com/questions/10903942/-2"/> <L7p:UserLogin stringValue="http://stackoverflow.com/questions/10903942/test"/> <L7p:UserName stringValue="http://stackoverflow.com/questions/10903942/test"/> <L7p:UserUid stringValue="http://stackoverflow.com/questions/10903942/58916876"/> </L7p:SpecificUser> <L7p:SpecificUser> <L7p:Enabled booleanValue="http://stackoverflow.com/questions/10903942/false"/> <L7p:IdentityProviderOid longValue="http://stackoverflow.com/questions/10903942/-2"/> <L7p:UserLogin stringValue="http://stackoverflow.com/questions/10903942/test"/> <L7p:UserName stringValue="http://stackoverflow.com/questions/10903942/test"/> <L7p:UserUid stringValue="http://stackoverflow.com/questions/10903942/58916875"/> </L7p:SpecificUser> <L7p:SpecificUser> <L7p:Enabled booleanValue="http://stackoverflow.com/questions/10903942/false"/> <L7p:IdentityProviderOid longValue="http://stackoverflow.com/questions/10903942/-2"/> <L7p:UserLogin stringValue="http://stackoverflow.com/questions/10903942/testengineering-user"/> <L7p:UserName stringValue="http://stackoverflow.com/questions/10903942/testengineering-user"/> <L7p:UserUid stringValue="http://stackoverflow.com/questions/10903942/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="http://stackoverflow.com/questions/10903942/Cookie"/> </L7p:item> <L7p:item httpPassthroughRule="included"> <L7p:Name stringValue="http://stackoverflow.com/questions/10903942/SOAPAction"/> </L7p:item> </L7p:Rules> </L7p:RequestHeaderRules> <L7p:RequestParamRules httpPassthroughRuleSet="included"> <L7p:ForwardAll booleanValue="http://stackoverflow.com/questions/10903942/true"/> <L7p:Rules httpPassthroughRules="included"/> </L7p:RequestParamRules> <L7p:ResponseHeaderRules httpPassthroughRuleSet="included"> <L7p:Rules httpPassthroughRules="included"> <L7p:item httpPassthroughRule="included"> <L7p:Name stringValue="http://stackoverflow.com/questions/10903942/Set-Cookie"/> </L7p:item> </L7p:Rules> </L7p:ResponseHeaderRules> </L7p:HttpRoutingAssertion> <L7p:Include> <L7p:PolicyGuid stringValue="http://stackoverflow.com/questions/10903942/b438384e-eeb0-45c5-8a7e-d30da78f07ee"/> </L7p:Include> </wsp:OneOrMore> </wsp:All></wsp:Policy>\[/code\]
 
Back
Top