dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Manipulate XML Content the Ximple Way  : Page 5

For many common use cases, you can improve your XML-processing performance by taking advantage of XML-VTD's document-centric processing model.


advertisement
Example 2: Inserting Namespace-Compensated Element Fragments
This example extracts a namespace-compensated element fragment (m:reservation) from the input XML #2 (highlighted section), and inserts it both before and after the a element from the input XML #1. The output XML shows the results, with the inserted content highlighted.

Input XML #1

Input XML #2

Output XML

<root>
<a> text </a>
<b> text </b> </root>  

<?xml version='1.0' ?>
  <env:Envelope xmlns:env=
    "http://www.w3.org/2003/05/
   soap-envelope" xmlns="abc">
  <env:Header xmlns="cd">
    <m:reservation       xmlns:m="http://travelcompany.
      example.org/reservation"
      xmlns:n="abc" xmlns:g="abc"
      env:role=
        "http://www.w3.org/2003/05/
      soap-envelope/role/next"
      env:mustUnderstand="true">
        <m:reference>uuid:093a2da1-
          q345-739r-ba5d-pqff98fe8j7d
       </m:reference>
       <m:dateAndTime>
         2001-11-29T13:20:00.000-05:00
       </m:dateAndTime>
   </m:reservation>

   <n:passenger xmlns:n=
     "http://mycompany.
        example.com/employees"
      env:role="http://www.w3.org/2003/05/
      soap-envelope/role/next"
      env:mustUnderstand="true">
<n:name>Whoever</n:name>
</n:passenger>
</env:Header>
</env:Envelope>

<root>

<m:reservation       xmlns:m="http://travelcompany.
      example.org/reservation"
      xmlns:n="abc" xmlns:g="abc"
      env:role=
         "http://www.w3.org/2003/05/
      soap-envelope/role/next"
      env:mustUnderstand="true">
        <m:reference>uuid:093a2da1-
          q345-739r-ba5d-pqff98fe8j7d
       </m:reference>
       <m:dateAndTime>
         2001-11-29T13:20:00.000-05:00
       </m:dateAndTime>
   </m:reservation>

<a> text </a>

<m:reservation       xmlns:m="http://travelcompany.
      example.org/reservation"
      xmlns:n="abc" xmlns:g="abc"
      env:role=
         "http://www.w3.org/2003/05/
      soap-envelope/role/next"
      env:mustUnderstand="true">
        <m:reference>uuid:093a2da1-
          q345-739r-ba5d-pqff98fe8j7d
       </m:reference>
       <m:dateAndTime>
         2001-11-29T13:20:00.000-05:00
       </m:dateAndTime>
   </m:reservation>

<b> text </b>
</root>

 

 


   import com.ximpleware.*; 
   public class nsFragment { 
      public static void main(String[] args) throws Exception{ 
         VTDGen vg = new VTDGen(); 
         XMLModifier xm = new XMLModifier(); 
         AutoPilot ap0 = new AutoPilot(); 
         AutoPilot ap1 = new AutoPilot(); 
         ap0.declareXPathNameSpace("m",
            "http://travelcompany.example.org/reservation"); 
         ap0.declareXPathNameSpace("env",
            "http://www.w3.org/2003/05/soap-envelope"); 
         ap0.selectXPath("/env:Envelope/env:Header/m:reservation"); 
         ap1.selectXPath("/root/a"); 
         ElementFragmentNs efn = null; 
         if (vg.parseFile("old.xml",true)){ 
            VTDNav vn1 = vg.getNav(); 
            if (vg.parseFile("soapHeader.xml",true)){ 
               VTDNav vn0 = vg.getNav(); 
               xm.bind(vn1); 
               ap0.bind(vn0); 
               ap1.bind(vn1); 
               if (ap0.evalXPath()==-1){ 
                  System.out.println("XPath navigation failed 0"); 
                  System.exit(0); 
               } 
               efn = vn0.getElementFragmentNs(); 
               if (ap1.evalXPath()==-1){ 
                  System.out.println("XPath navigation failed 1"); 
                  System.exit(0); 
               } 
               xm.insertBeforeElement(efn); 
               xm.insertAfterElement(efn); 
               xm.output("new.xml"); 
            } 
         } 
      } 
   } 


Thanks for your registration, follow us on our social networks to keep up-to-date