dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
Thanks for your registration, follow us on our social networks to keep up-to-date