如何使用vtd-xml解析xml而不使用硬编码attr名称?

时间:2017-02-08 11:41:05

标签: java xml parsing vtd-xml

这是我的示例XML文件,实际上高于2gb。使用vtd-xml我已经实现了这么多:

当前代码:

https://gist.github.com/shadow-fox/21d1d4f30cbed0909f403c3ac0e1fa4d

public void reader() throws IOException, ParseException, NavException, XPathParseExceptionHuge, NavExceptionHuge,
            XPathEvalExceptionHuge {
        VTDGenHuge vg = new VTDGenHuge();
    if (vg.parseFile("sku_extract_main.xml",true,VTDGenHuge.MEM_MAPPED)) {
        VTDNavHuge vnh = vg.getNav();
        AutoPilotHuge aph = new AutoPilotHuge(vnh);
        aph.selectElementNS("*", "*");
        int i = 0;
        while (aph.iterate()) {
            int t = vnh.getText();
            if (t != -1) {
                System.out.println(vnh.toString(vnh.getCurrentIndex()) + "|||" + vnh.toNormalizedString(t));
                i++;
            }
        }
    }
}

当前结果:

PVAL|||298374234
PVAL|||1231
PVAL|||brown
PVAL|||medium
PVAL|||7
PVAL|||solid
PVAL|||brown

我想要的是什么:

Sku_ID|||298374234
LotNum|||1231
COLOR|||brown
WIDTH|||medium
SIZE|||7
Pattern|||solid
Color Family|||brown

示例xml:

<?xml version="1.0" encoding="UTF-8" ?>
<RECORDS>
  <RECORD>
    <PROP NAME="Sku_ID">
      <PVAL>298374234</PVAL>
    </PROP>
    <PROP NAME="LotNum">
      <PVAL>1231</PVAL>
    </PROP>
    <PROP NAME="COLOR">
      <PVAL>brown</PVAL>
    </PROP>
    <PROP NAME="WIDTH">
      <PVAL>medium</PVAL>
    </PROP>
    <PROP NAME="SIZE">
      <PVAL>7</PVAL>
    </PROP>
    <PROP NAME="Pattern">
      <PVAL>solid</PVAL>
    </PROP>
    <PROP NAME="Color Family">
      <PVAL>brown</PVAL>
    </PROP>
  </RECORD>
</RECORDS>

我不想硬编码attr名称。我想在他们访问它们时检索它们。我该怎么做?

1 个答案:

答案 0 :(得分:1)

下面是我编写代码以编辑attr名称和值...它是基于xpath的...

TemplateHaskell