VTD-XML: The Future of XML Processing

SourceForge.net Logo

Sourceforge Home

Mailing Lists

XimpleWare

Download


VTD-XML Home

 

Shuffle Elements in an XML document

(For more code samples, visit Official VTD-XML Blog)

(Separate code-only VTD-XML tutorials are available in C, C++, Java and C#)

This example will show you how to re-arrange element fragments in an XML document. In the input XML, the elements a, b and c are interspersed. The application regroups element a, b and c. The XML and source code are below:

old.xml

new.xml

shuffle.java

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>
<a> text </a>
<a> text </a>
<b> text </b>
<b> text </b>
<b> text </b>
<c> text </c>
<c> text </c>
<c> text </c>
</root>

The application logic goes into the following template:

import com.ximpleware.*;
import java.io.*;
public class shuffle {
    public static void main(String[] args) throws Exception {

       // Code here

   }

}

The application instantiates VTDGen object and multiple AutoPilot objects, one for each xpath: "/root/a," "/root/b," and "/root/c."

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");

Then the application parses the file using parseFile(...), evaluates each XPath, and writes out corresponding fragments (by calling getElementFragment(...)) into the output file.

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();

             // write all the fragments for /root/a
             while(ap0.evalXPath()!=-1){
                   long l= vn.getElementFragment();
                   int offset = (int)l;
                   int len = (int)(l>>32);
                   fos.write('\n');
                   fos.write(ba,offset, len);
             }
            ap0.resetXPath();
           

             //  write all the fragments for /root/b                                                      

            while(ap1.evalXPath()!=-1){
                  long l= vn.getElementFragment();
                  int offset = (int)l;
                  int len = (int)(l>>32);
                  fos.write('\n');
                  fos.write(ba,offset, len);
            }
            ap1.resetXPath();

             // write all the fragments for /root/c
            while(ap2.evalXPath()!=-1){
                  long l= vn.getElementFragment();
                  int offset = (int)l;
                  int len = (int)(l>>32);
                  fos.write('\n');
                  fos.write(ba,offset, len);
             }
             ap2.resetXPath();

              // write ending tag

             fos.write('\n');
             fos.write("</root>".getBytes());
}

VTD in 30 seconds

VTD+XML Format

User's Guide

Developer's Guide

VTD: A Technical Perspective

Code Samples

  RSS Reader in Java

  RSS Reader in C

  SOAP in Java

  SOAP in C

  BioInfo in Java

  BioInfo in C

  Modify XML In Java

  Modify XML In C

  Shuffle

  Edit XML

  Index Creation and Loading

  Process Huge XML Files (>2G)

FAQ

Getting Involved

Articles and Presentations

Benchmark

API Doc

Demo