xml - Simple way to get execute an Xpath against a String in Java? -


i know similar question has been asked (and answered) number of times, , i've tried many of suggestions, reason, cannot get working.

i have xml document (in "compositebodyline") in string variable, , want perform xpath search on xml document , result(s) xpath search.

how can that?

here's 1 example of i've tried (or merging several different things i've found:

 // from: http://javarevisited.blogspot.com/2012/12/create-and-evaluate-xpath-java-example-tutorial-program.html    //create documentbuilderfactory reading xml file   documentbuilderfactory factory = documentbuilderfactory.newinstance();   documentbuilder builder = factory.newdocumentbuilder();    inputstream inputstream = new    bytearrayinputstream(compositebodyline.getbytes());   org.w3c.dom.document doc = builder.parse(inputstream);     system.out.println("doc.getparentnode()=[" + doc.getparentnode().tostring() + "]");    // create xpathfactory creating xpath object   xpathfactory xpathfactory = xpathfactory.newinstance();    // create xpath object xpathfactory   xpath xpath = xpathfactory.newxpath();    // compile xpath expression getting brands   xpathexpression xpathexpr = xpath.compile("/soapenv:envelope");    // xpath text example : executing xpath expression in java   object result = xpathexpr.evaluate(doc, xpathconstants.nodeset);   system.out.println("java xpath text example: brands of popular smartphones ");    printxpathresult(result);  . . . . public static org.w3c.dom.document loadxmlfromstring(string xml) throws exception {     documentbuilderfactory factory = documentbuilderfactory.newinstance();     documentbuilder builder = factory.newdocumentbuilder();     inputsource = new inputsource(new stringreader(xml));     return builder.parse(is); } 

when try above code, null returning. think of examples i've looked @ take file input, whereas in case, have xml document in string varible, if had guess, guess having problems bringing xml input.

can provide easy way accomplish this?

thanks, jim

edit: here's example of input xml:

<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">     <soapenv:body>         <request xmlns:xacml-context="urn:oasis:names:tc:xacml:2.0:context:schema:os" xmlns:ns9="urn:oasis:xacml:2.0:saml:assertion:schema:os" xmlns:ns8="urn:oasis:xacml:2.0:saml:protocol:schema:os" xmlns:ns7="http://security.bea.com/ssmws/ssm-soap-types-1.0.xsd" xmlns:ns6="http://www.w3.org/2001/04/xmlenc#" xmlns:ns5="urn:oasis:names:tc:xacml:2.0:policy:schema:os" xmlns:ns4="urn:oasis:names:tc:xacml:2.0:context:schema:os" xmlns:ns3="urn:oasis:names:tc:saml:2.0:protocol" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#" xmlns="urn:oasis:names:tc:xacml:2.0:context:schema:os">             <subject>                 <attribute attributeid="urn:oasis:names:tc:xacml:1.0:subject:subject-id" datatype="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#oesprincipalinfo">                      <attributevalue>{name=jimxxxx1234}+(class=weblogic.security.principal.wlsuserimpl)</attributevalue>                 </attribute> <!-- following **the** way , work oes role --> <attribute attributeid="http://oracle.com/symbols/oes/attribute/group-assertion" datatype="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#oesprincipalinfo" xsi:type="ns1:attributetype">  <attributevalue xsi:type="ns1:attributevaluetype">{name=operators}+(class=weblogic.security.principal.wlsgroupimpl)</attributevalue>  </attribute>              </subject>             <resource>                 <attribute attributeid="urn:oasis:names:tc:xacml:2.0:resource:resource-id" datatype="http://www.w3.org/2001/xmlschema#string">                     <attributevalue>foo/foo1/foo2</attributevalue>                 </attribute>             </resource>             <action>                 <attribute attributeid="urn:oasis:names:tc:xacml:1.0:action:action-id" datatype="http://www.w3.org/2001/xmlschema#string">                     <attributevalue>get</attributevalue>                 </attribute>             </action> <ns4:environment xsi:type="ns4:environmenttype"       xmlns:ns4="urn:oasis:names:tc:xacml:2.0:context:schema:os"       xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">   <ns4:attribute attributeid="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#registeredattribute"       datatype="http://www.w3.org/2001/xmlschema#string" xsi:type="ns4:attributetype">     <ns4:attributevalue xsi:type="ns4:attributevaluetype">4444444444yes</ns4:attributevalue>    </ns4:attribute>   <ns4:attribute attributeid="http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#numberofborrowedbooksattribute"         datatype="http://www.w3.org/2001/xmlschema#string" xsi:type="ns4:attributetype">         <ns4:attributevalue xsi:type="ns4:attributevaluetype">abc</ns4:attributevalue>    </ns4:attribute> </ns4:environment>                   </request>     </soapenv:body> </soapenv:envelope> 

steve , reason can namespaces have on xml. 1 way overcome registering namespaces. simpler approach use localnames. shorter version of program below , return me node set expected. created minimum test program here , please let me know if works other xpath may have

import java.io.bytearrayinputstream;  import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.xpath.xpath; import javax.xml.xpath.xpathconstants; import javax.xml.xpath.xpathexpression; import javax.xml.xpath.xpathfactory;  import org.w3c.dom.document;  public class xpathclass {     public static void main(string[] args) throws exception {         string soapxml = "<soapenv:envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\" xmlns:xsd=\"http://www.w3.org/2001/xmlschema\">    <soapenv:body>        <request xmlns:xacml-context=\"urn:oasis:names:tc:xacml:2.0:context:schema:os\" xmlns:ns9=\"urn:oasis:xacml:2.0:saml:assertion:schema:os\" xmlns:ns8=\"urn:oasis:xacml:2.0:saml:protocol:schema:os\" xmlns:ns7=\"http://security.bea.com/ssmws/ssm-soap-types-1.0.xsd\" xmlns:ns6=\"http://www.w3.org/2001/04/xmlenc#\" xmlns:ns5=\"urn:oasis:names:tc:xacml:2.0:policy:schema:os\" xmlns:ns4=\"urn:oasis:names:tc:xacml:2.0:context:schema:os\" xmlns:ns3=\"urn:oasis:names:tc:saml:2.0:protocol\" xmlns:ns2=\"http://www.w3.org/2000/09/xmldsig#\" xmlns=\"urn:oasis:names:tc:xacml:2.0:context:schema:os\">            <subject>                <attribute attributeid=\"urn:oasis:names:tc:xacml:1.0:subject:subject-id\" datatype=\"http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#oesprincipalinfo\">                    <attributevalue>{name=jimlum1234}+(class=weblogic.security.principal.wlsuserimpl)</attributevalue>                </attribute><!-- following **the** way , work oes role --><attribute attributeid=\"http://oracle.com/symbols/oes/attribute/group-assertion\" datatype=\"http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#oesprincipalinfo\" xsi:type=\"ns1:attributetype\"> <attributevalue xsi:type=\"ns1:attributevaluetype\">{name=operators}+(class=weblogic.security.principal.wlsgroupimpl)</attributevalue> </attribute>             </subject>            <resource>                <attribute attributeid=\"urn:oasis:names:tc:xacml:2.0:resource:resource-id\" datatype=\"http://www.w3.org/2001/xmlschema#string\">                    <attributevalue>foo/foo1/foo2</attributevalue>                </attribute>            </resource>            <action>                <attribute attributeid=\"urn:oasis:names:tc:xacml:1.0:action:action-id\" datatype=\"http://www.w3.org/2001/xmlschema#string\">                    <attributevalue>get</attributevalue>                </attribute>            </action><ns4:environment xsi:type=\"ns4:environmenttype\"      xmlns:ns4=\"urn:oasis:names:tc:xacml:2.0:context:schema:os\"      xmlns:xsi=\"http://www.w3.org/2001/xmlschema-instance\">  <ns4:attribute attributeid=\"http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#registeredattribute\"      datatype=\"http://www.w3.org/2001/xmlschema#string\" xsi:type=\"ns4:attributetype\">    <ns4:attributevalue xsi:type=\"ns4:attributevaluetype\">4444444444yes</ns4:attributevalue>   </ns4:attribute>  <ns4:attribute attributeid=\"http://security.bea.com/ssmws/ssm-ws-1.0.wsdl#numberofborrowedbooksattribute\"        datatype=\"http://www.w3.org/2001/xmlschema#string\" xsi:type=\"ns4:attributetype\">        <ns4:attributevalue xsi:type=\"ns4:attributevaluetype\">abc</ns4:attributevalue>   </ns4:attribute></ns4:environment>                  </request>    </soapenv:body></soapenv:envelope>";         documentbuilderfactory factory = documentbuilderfactory.newinstance();         documentbuilder builder = factory.newdocumentbuilder();         document doc = builder.parse(new bytearrayinputstream(soapxml.getbytes()));          xpathfactory xpathfactory = xpathfactory.newinstance();          // create xpath object xpathfactory         xpath xpath = xpathfactory.newxpath();          // compile xpath expression getting brands         xpathexpression xpathenvelopeexpr = xpath.compile("//*[local-name()='envelope']");         object result = xpathenvelopeexpr.evaluate(doc, xpathconstants.nodeset);         system.out.println("java xpath text example: brands of popular smartphones " + result);      }  } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -