【问题标题】:Populate POJO from XML using VTD-XML使用 VTD-XML 从 XML 填充 POJO
【发布时间】:2018-09-18 13:59:30
【问题描述】:

示例 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>

基于上述结构,我想读取元素/属性并填充 POJO(最好是 XPath),因为元素路径不一致,例如:&lt;Tag1&gt;

我不知道如何继续。我尝试过使用 AutoPilot,但它会按顺序读取数据,例如所有节点 ID。我想不出一种方法来读取&lt;Node&gt; 中的所有数据,然后继续下一个,依此类推。最后我需要返回填充的 POJO 集合。

DOM 超出范围,因为 XML 文件很大,大约 800MB 到 1GB 大小,大约 1GB。 600,000+ &lt;Node&gt;.

提前致谢。

XML 阅读器

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) {
}

}

【问题讨论】:

  • 您能否提供一个 pojo 示例和一些代码示例来说明您希望如何进行? Autopilot 可以轻松进行随机访问,这就是 vtd 的重点……它会从一个节点跳到另一个节点……
  • @vtd-xml-author 我已经包含了代码。我尝试了各种选项,但没有全部的副本。 POJO 是保存解析数据的普通 bean
  • 我认为您正在尝试嵌套迭代,我将做一个示例,您可以适应您的情况...

标签: java xpath vtd-xml


【解决方案1】:

我稍微修改了您的代码以使其正常工作...加上纠正一些事情...将在代码中嵌入更多 cmets...。

注意事项

  1. 永远不要把 autoPIlot 实例化和 xpath 选择放在一边 环形。从不!
  2. resetXPath() 重用
  3. 使用 push 和 pop 在最外层的 while 循环中保持一致的状态。

导入 com.ximpleware.*; 公共课锯{

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

}

【讨论】:

  • 感谢示例代码。我会检查并通知您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
相关资源
最近更新 更多