[vwnc] XPath

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[vwnc] XPath

Steffen Märcker
Hi,

today I'd to query an xml document including nested namespaces with xpath  
in Java. I wonder how this can be done in VisualWorks.

xml:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope  
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"  
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
     <SOAP-CHK:Success xmlns:SOAP-CHK="http://soaptest1/soaptest/"  
xmlns="urn:candle-soap:attributes">
       <TABLE name="KNT.NTPROCSSR">
         <OBJECT>NT_Processor</OBJECT>
         <DATA>
           <ROW>
             <Server_Name>Primary:BL3D1971:NT</Server_Name>
             <Processor>0</Processor>
             <Timestamp>1080826190527765</Timestamp>
             <Processor_Time dt="number">15</Processor_Time>
           </ROW>
           <ROW>
             <Server_Name>Primary:BL3D1971:NT</Server_Name>
             <Processor>1</Processor>
             <Timestamp>1080826190527765</Timestamp>
             <Processor_Time dt="number">18</Processor_Time>
           </ROW>
           <ROW>
             <Server_Name>Primary:BL3D1971:NT</Server_Name>
             <Processor>_Total</Processor>
             <Timestamp>1080826190527765</Timestamp>
             <Processor_Time dt="number">17</Processor_Time>
           </ROW>
         </DATA>
       </TABLE>
     </SOAP-CHK:Success>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


Java solution:

xpath =  
XPath.newInstance("/SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-CHK:Success/TEST:TABLE");
xpath.addNamespace("SOAP-ENV",  
"http://schemas.xmlsoap.org/soap/envelope/");
xpath.addNamespace("SOAP-CHK", "http://soaptest1/soaptest/");
xpath.addNamespace("TEST", "urn:candle-soap:attributes");
List<?> list = xpath.selectNodes( document );

Note that the qualifier "TEST" had to be inserted and added  
(addNamespace(...)) because otherwise /TABLE refers to a node not beeing  
defined in a namespace. See also:
http://www.edankert.com/defaultnamespaces.html



Greetings!
Steffen
_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] XPath

Steffen Märcker
To answer the question myself, the following code will do it:


node := XML.Element tag: 'NamespaceElement'.
node
        namespaces:
                (Dictionary withKeysAndValues:
                                #('SOAP-ENV' 'http://schemas.xmlsoap.org/soap/envelope/' 'SOAP-CHK'  
'http://soaptest1/soaptest/' 'TEST' 'urn:candle-soap:attributes' )).
exprString :=  
'/SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-CHK:Success/TEST:TABLE'.
expr := (XML.XPathParser new)
        xmlNode: node;
        parse: exprString as: #expression


Having sleept well helps! ;)





Am 26.08.2008, 23:46 Uhr, schrieb Steffen Märcker <[hidden email]>:

> Hi,
>
> today I'd to query an xml document including nested namespaces with xpath
> in Java. I wonder how this can be done in VisualWorks.
>
> xml:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <SOAP-ENV:Envelope
> xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
> SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
>    <SOAP-ENV:Body>
>      <SOAP-CHK:Success xmlns:SOAP-CHK="http://soaptest1/soaptest/"
> xmlns="urn:candle-soap:attributes">
>        <TABLE name="KNT.NTPROCSSR">
>          <OBJECT>NT_Processor</OBJECT>
>          <DATA>
>            <ROW>
>              <Server_Name>Primary:BL3D1971:NT</Server_Name>
>              <Processor>0</Processor>
>              <Timestamp>1080826190527765</Timestamp>
>              <Processor_Time dt="number">15</Processor_Time>
>            </ROW>
>            <ROW>
>              <Server_Name>Primary:BL3D1971:NT</Server_Name>
>              <Processor>1</Processor>
>              <Timestamp>1080826190527765</Timestamp>
>              <Processor_Time dt="number">18</Processor_Time>
>            </ROW>
>            <ROW>
>              <Server_Name>Primary:BL3D1971:NT</Server_Name>
>              <Processor>_Total</Processor>
>              <Timestamp>1080826190527765</Timestamp>
>              <Processor_Time dt="number">17</Processor_Time>
>            </ROW>
>          </DATA>
>        </TABLE>
>      </SOAP-CHK:Success>
>    </SOAP-ENV:Body>
> </SOAP-ENV:Envelope>
>
>
> Java solution:
>
> xpath =
> XPath.newInstance("/SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-CHK:Success/TEST:TABLE");
> xpath.addNamespace("SOAP-ENV",
> "http://schemas.xmlsoap.org/soap/envelope/");
> xpath.addNamespace("SOAP-CHK", "http://soaptest1/soaptest/");
> xpath.addNamespace("TEST", "urn:candle-soap:attributes");
> List<?> list = xpath.selectNodes( document );
>
> Note that the qualifier "TEST" had to be inserted and added
> (addNamespace(...)) because otherwise /TABLE refers to a node not beeing
> defined in a namespace. See also:
> http://www.edankert.com/defaultnamespaces.html
>
>
>
> Greetings!
> Steffen
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc