Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
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 4

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 1: Remove Element Fragments
This example removes all the fragments evaluating to the XPath expression /root/b. In all these examples, the table shows the XML for both the input and output.

 

 

Input XML Output XML
<root>
<a> text </a>
<b> text </b>
<c> text </c>
<a> text </a>
<b> text </b>
<c> text </c>
<a> text </a>
<b> text </b>
<c> text </c>
</root>
<root>
<a> text </a>

<c> text </c>
<a> text </a>

<c> text </c>
<a> text </a>

<c> text </c>
</root>

Here's the Java code:



import com.ximpleware.*; public class removeFragments { public static void main(String[] args) throws Exception{ VTDGen vg = new VTDGen(); AutoPilot ap = new AutoPilot(); XMLModifier xm = new XMLModifier(); ap.selectXPath("/root/b"); if (vg.parseFile("old.xml", false)) { VTDNav vn = vg.getNav(); ap.bind(vn); xm.bind(vn); while(ap.evalXPath()!=-1){ xm.remove(); } xm.output("new.xml"); } } } import java.io.*; public class arrangeFragments { public static void writeFragment( OutputStream os, long l, byte[] ba) throws IOException { int offset = (int) l; int len = (int) (l >> 32); os.write('\n'); os.write(ba, offset, len); } public static void main(String[] args) throws Exception { VTDGen vg = new VTDGen(); AutoPilot ap0 = new AutoPilot(); AutoPilot ap1 = new AutoPilot(); AutoPilot ap2 = new AutoPilot(); ap0.selectXPath("/root/a"); ap1.selectXPath("/root/b"); ap2.selectXPath("/root/c"); if (vg.parseFile("old.xml", false)) { VTDNav vn = vg.getNav(); ap0.bind(vn); ap1.bind(vn); ap2.bind(vn); FileOutputStream fos = new FileOutputStream("new.xml"); fos.write("<root>".getBytes()); byte[] ba = vn.getXML().getBytes(); while (ap0.evalXPath() != -1) { long l = vn.getElementFragment(); writeFragment(fos,l,ba); } ap0.resetXPath(); while (ap1.evalXPath() != -1) { long l = vn.getElementFragment(); writeFragment(fos,l,ba); } ap1.resetXPath(); while (ap2.evalXPath() != -1) { long l = vn.getElementFragment(); writeFragment(fos,l,ba); } ap2.resetXPath(); fos.write('\n'); fos.write("</root>".getBytes()); } } }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap