1 Value 1 12345" /> 1 Value 1 12345" /> 1 Value 1 12345"/>

Populate POJO from XML using VTD-XML

162 views Asked by At

Sample XML

<?xml version="1.0" encoding="UTF-8"?>
<Document>
    <Report>
        <Node>
            <Detail>
                <Id>1</Id>
                <Value>Value 1</Value>
                <Tag1>
                    <Owner>
                        <Id>
                            <INT>12345</INT>
                        </Id>
                    </Owner>
                </Tag1>
            </Detail>
            <Status>
                <Result>Pass</Result>
            </Status>
        </Node>
        <Node>
            <Detail>
                <Id>2</Id>
                <Value>Value 2</Value>
                <Tag1>
                    <Owner>
                        <Id>
                            <String>TEST</String>
                        </Id>
                    </Owner>
                </Tag1>
            </Detail>
            <Status>
                <Result>Fail</Result>
            </Status>
        </Node> 
        <Node>
            <Detail>
                <Id>3</Id>
                <Value>Value 3</Value>
                <Tag1>
                    <Owner>
                        <Id>
                            <UN>UNKNOWN</UN>
                        </Id>
                    </Owner>
                </Tag1>
            </Detail>
            <Status>
                <Result>Waiting</Result>
            </Status>
        </Node>
    </Report>
</Document>

Based on above structure, I want to read elements / attributes and populate POJO (XPath preferably) as element path is not consistent, e.g.: <Tag1>.

I'm not able to figure out how to proceed. I've tried using AutoPilot but it reads data sequentially, e.g. all Node Ids. I can't figure out a way to read all data within <Node> and then proceed to next one and so on. At the end I need to return populated POJO collection.

DOM is out of scope as XML file is huge, almost 800MB to 1GB in size with approx. 600,000+ <Node>.

Thanks in advance.

XML Reader

public void process(final String fullPath) {

try {
    final VTDGen vg = new VTDGen();

    if (vg.parseFile(fullPath, false)) {
        final VTDNav vn = vg.getNav();
        final AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath(ROOT);

        while ((ap.evalXPath()) != -1) {
            final AutoPilot pilot1 = new AutoPilot(vn);
            pilot1.selectXPath(ROOT + "/Detail/Id");

            while (pilot1.evalXPath() != -1) {
                final int t = vn.getText();
                if (t != -1) {
                    System.out.println(vn.toNormalizedString(t));
                }

                final AutoPilot pilot2 = new AutoPilot(vn);
                pilot2.selectXPath(ROOT + "/Status/Result");

                if (pilot2.evalXPath() != -1) {
                    final int k = vn.getText();
                    if (k != -1) {
                        System.out.println(vn.toNormalizedString(k));
                    }
                }
                // pilot2.resetXPath();
            }
        }
    }
}
catch (Exception exception) {
}

}

1

There are 1 answers

1
vtd-xml-author On

I modified your code a bit to make it work... plus correcting a few things... will come back with more comments embedding in the code....

A few things to keep in mind

  1. never put autoPIlot instantiation and xpath selection in a while loop. Never!
  2. Reuse by resetXPath()
  3. Use push and pop to maintain the consistent state in the outermost while loop.

import com.ximpleware.*; public class sawi {

public static void main(String[] args) throws Exception{
    // TODO Auto-generated method stub
    final VTDGen vg = new VTDGen();

    if (vg.parseFile("d:\\xml\\sawi.xml", false)) {
        final VTDNav vn = vg.getNav();
        final AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/Document/Report/Node");
        final AutoPilot pilot1 = new AutoPilot(vn);
        pilot1.selectXPath("Detail/Id");
        final AutoPilot pilot2 = new AutoPilot(vn);
        pilot2.selectXPath("Status/Result");
        while ((ap.evalXPath()) != -1) {
            vn.push();
            while (pilot1.evalXPath() != -1) {
                final int t = vn.getText();
                if (t != -1) {
                    System.out.println(vn.toNormalizedString(t));
                }

            } 
            vn.pop();
            vn.push();
                if (pilot2.evalXPath() != -1) {
                    final int k = vn.getText();
                    if (k != -1) {
                        System.out.println(vn.toNormalizedString(k));
                    }
                }
            vn.pop();
            pilot2.resetXPath();
            pilot1.resetXPath();

           // vn.pop();
        }
    }
}

}