Xpath로 XML 컨텐트를 파싱하는 데 어려움이 있습니다. Xml에는 네임 스페이스 정보가 포함됩니다. URI에 namaspace 접두사를 매핑하기 위해 NameSpaceContextImp (jdk에서 NameSpaceContext 인터페이스의 apache WS 공유 구현 구현)를 만들려고했지만 XML 문서를 성공적으로 쿼리 할 수 없었습니다. http://chris.photobooks.com/xml/default.htm에서 온라인 xpath 테스트 도구를 사용할 때 xpath 쿼리를 사용하면 예상되는 노드/요소가 생깁니다. 그래서 내가 잘못하고있는 것이 무엇인지 알아 내려고 노력하고 있습니다. xml 문서와 샘플 코드 스 니펫을 제공하고 있습니다. 나는 어떤 의견을 주셔서 감사합니다. 참고로, 나는 네임 스페이스 접두어를 사용하거나 사용하지 않고 xpath 쿼리를 시도했다.xml을 jdk xpath를 사용하여 네임 스페이스로 구문 분석하는 경우
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="included">
<L7p:entry>
<L7p:key stringValue="RIGHT.COMMENT"/>
<L7p:value stringValue="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="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>
감사합니다. CDATA에 대한 귀하의 의견은 저에게 효과적입니다. 너무 많은 것을 시도하는 동안, 나는 또한 xpath 질의를 단순화 시켰고, 내가 원래는 찾고 있던 실제 요소/속성을 찾고 있지 않았다. 나는 당신의 코드를 잘라내는 시도를하지 않았고 왜 내가 변압기가 필요한지 이해하지 못한다. – John
@ 존 - 트랜스포머는'toString()'이'Node' 타입을 많이하지 않기 때문에 데모 코드에서 XML을'System.out'으로 출력하기위한 것입니다 - [here] (http : // stackoverflow .com/questions/1219596). – McDowell