如何使用 VTD-XML 解析器将嵌套的 XML 文件元素放入对象列表?
How to put nested XML file elements to list of objects with VTD-XML parser?
我有很大的嵌套 XML 文件。所有实体和属性都将成为我的对象变量。我正在创建此类对象的列表。我知道如何使用 DOM、SAX 和 XMLPullParser 来完成它并且它工作正常但我在 VTD 解析器方面遇到问题。解析后的 ListView 为空。我将 XML 文件的一部分和我的代码放在下面。也许有人知道我做错了什么。
<MedlineCitationSet>
<MedlineCitation Owner="NLM" Status="MEDLINE">
<PMID Version="1">10540283</PMID>
<DateCreated>
<Year>1999</Year>
<Month>12</Month>
<Day>17</Day>
</DateCreated>
<Article PubModel="Print">
<Journal>
<ISSN IssnType="Print">0950-382X</ISSN>
<JournalIssue CitedMedium="Print">
<Volume>34</Volume>
<Issue>1</Issue>
</JournalIssue>...
我的android代码:
try {
articlesList = new ArrayList<>();
VTDGen vtdGen = new VTDGen();
vtdGen.setDoc(bytes);
vtdGen.parse(false);
AutoPilot ap = new AutoPilot();
VTDNav vtdNav = vtdGen.getNav();
int i = -1;
ap.bind(vtdNav);
ap.selectXPath("/MedlineCitationSet/MedlineCitation");
while ((ap.evalXPath()) != -1) {
articlesList.add(new Article());
String year = null, day = null, month = null;
i++;
if (vtdNav.hasAttr("Owner"))
articlesList.get(i).setOwner(vtdNav.toNormalizedString(vtdNav.getAttrVal("Owner")));
if (vtdNav.hasAttr("Status"))
articlesList.get(i).setStatus(vtdNav.toNormalizedString(vtdNav.getAttrVal("Status")));
vtdNav.push();
AutoPilot ap1 = new AutoPilot();
ap1.selectXPath("/MedlineCitationSet/MedlineCitation/PMID");
ap1.bind(vtdNav);
while ((ap1.evalXPath()) != -1) {
articlesList.get(i).setPMID(vtdNav.toNormalizedString(vtdNav.getText()));
articlesList.get(i).setVersion(vtdNav.toNormalizedString(vtdNav.getAttrVal("Version")));
}
ap1.resetXPath();
ap1.selectXPath("/MedlineCitationSet/MedlineCitation/DateCreated");
ap1.bind(vtdNav);
while ((ap1.evalXPath() != -1)) {
vtdNav.push();
AutoPilot ap1x = new AutoPilot();
ap1x.selectXPath("/MedlineCitationSet/MedlineCitation/DateCreated/Year");
ap1x.bind(vtdNav);
while ((ap1x.evalXPath()) != -1) {
year = vtdNav.toNormalizedString(vtdNav.getText());
}
ap1x.resetXPath();
ap1x.selectXPath("/MedlineCitationSet/MedlineCitation/DateCreated/Month");
ap1x.bind(vtdNav);
while ((ap1x.evalXPath()) != -1) {
month = vtdNav.toNormalizedString(vtdNav.getText());
}
ap1x.resetXPath();
ap1x.selectXPath("/MedlineCitationSet/MedlineCitation/DateCreated/Day");
ap1x.bind(vtdNav);
while ((ap1x.evalXPath()) != -1) {
day = vtdNav.toNormalizedString(vtdNav.getText());
}
articlesList.get(i).setDateCreated(day + "-" + month + "-" + year);
vtdNav.pop();
}
ap1.resetXPath();
ap1.selectXPath("/MedlineCitationSet/MedlineCitation/Article");
ap1.bind(vtdNav);
while ((ap1.evalXPath()) != -1) {
if (vtdNav.hasAttr("Print"))
articlesList.get(i).setPubModel(vtdNav.toNormalizedString(vtdNav.getAttrVal("Print")));
vtdNav.push();
AutoPilot ap2 = new AutoPilot();
ap2.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal");
ap2.bind(vtdNav);
{
vtdNav.push();
AutoPilot ap2x = new AutoPilot();
ap2x.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal/ISSN");
ap2x.bind(vtdNav);
while ((ap2x.evalXPath()) != -1) {
articlesList.get(i).setISSN(vtdNav.toNormalizedString(vtdNav.getText()));
articlesList.get(i).setIssnType(vtdNav.toNormalizedString(vtdNav.getAttrVal("IssnType")));
}
ap2x.resetXPath();
ap2x.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal/JournalIssue");
ap2x.bind(vtdNav);
while ((ap2x.evalXPath()) != -1) {
articlesList.get(i).setCitedMedium(vtdNav.toNormalizedString(vtdNav.getAttrVal("CitedMedium")));
vtdNav.push();
AutoPilot ap3 = new AutoPilot();
ap3.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal/JournalIssue/Volume");
ap3.bind(vtdNav);
while ((ap3.evalXPath()) != -1) {
articlesList.get(i).setVolume(vtdNav.toNormalizedString(vtdNav.getText()));
}
ap3.resetXPath();
ap3.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal/JournalIssue/Issue");
ap3.bind(vtdNav);
while ((ap3.evalXPath()) != -1) {
articlesList.get(i).setIssue(vtdNav.toNormalizedString(vtdNav.getText()));
}
ap3.resetXPath();
vtdNav.pop();
}...
感谢您的帮助!
下面是一个代码片段,可帮助您提取文档中的相关字段。您对 AutoPilot 的使用存在问题。
例如,我建议您将所有 selectXPath 移出 while 循环,因为它们是相对较慢的操作。 此外,如果 xml有很深的嵌套,应该考虑开启VTDGen的selectLcDepth,设置为5。这有助于提高 navigation/xpath 性能。以下只是可以完成的示例。同样对于简单的 XPath,您可以使用 VTDNav 的本地游标 API,这更方便...
如果您有任何问题,请告诉我...
VTDGen vtdGen = new VTDGen();
vtdGen.selectLcDepth(5);
vtdGen.parseFile("c:\xml\agata.xml",false);
AutoPilot ap = new AutoPilot(),ap1=new AutoPilot(),
ap2=new AutoPilot(),ap3=new AutoPilot();
VTDNav vn = vtdGen.getNav();
int i = -1;
ap.bind(vn);ap1.bind(vn);ap2.bind(vn);ap3.bind(vn);
ap.selectXPath("/MedlineCitationSet/MedlineCitation");
ap1.selectXPath("PMID");
ap2.selectXPath("DateCreated");
ap3.selectXPath("Article");
while ((ap.evalXPath()) != -1) {
String year = null, day = null, month = null;
i++;
if (vn.hasAttr("Owner")) System.out.println("Owner==>"+vn.toNormalizedString(vn.getAttrVal("Owner")));
//articlesList.get(i).setOwner(vtdNav.toNormalizedString(vtdNav.getAttrVal("Owner")));
if (vn.hasAttr("Status"))
System.out.println("Stats==>"+vn.toNormalizedString(vn.getAttrVal("Status")));
//articlesList.get(i).setStatus(vtdNav.toNormalizedString(vtdNav.getAttrVal("Status")));
vn.push();
while((ap1.evalXPath())!=-1){
System.out.println("Version==>"+vn.toNormalizedString(vn.getAttrVal("Version")));
System.out.println("PMID==>"+vn.toNormalizedString(vn.getText()));
}
ap1.resetXPath();
vn.pop();
vn.push();
while((ap2.evalXPath())!=-1){
vn.toElement(VTDNav.FIRST_CHILD,"Year");
System.out.println("Year==>"+vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
vn.toElement(VTDNav.FIRST_CHILD,"Month");
System.out.println("Month==>"+vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
vn.toElement(VTDNav.FIRST_CHILD,"Day");
System.out.println("Day==>"+vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
}
ap2.resetXPath();
vn.pop();
VN.push();
while((ap3.evalXPath())!=-1){
System.out.println("PubModel==>"+vn.toNormalizedString(vn.getAttrVal("PubModel")));
VN.pop();
}
我有很大的嵌套 XML 文件。所有实体和属性都将成为我的对象变量。我正在创建此类对象的列表。我知道如何使用 DOM、SAX 和 XMLPullParser 来完成它并且它工作正常但我在 VTD 解析器方面遇到问题。解析后的 ListView 为空。我将 XML 文件的一部分和我的代码放在下面。也许有人知道我做错了什么。
<MedlineCitationSet>
<MedlineCitation Owner="NLM" Status="MEDLINE">
<PMID Version="1">10540283</PMID>
<DateCreated>
<Year>1999</Year>
<Month>12</Month>
<Day>17</Day>
</DateCreated>
<Article PubModel="Print">
<Journal>
<ISSN IssnType="Print">0950-382X</ISSN>
<JournalIssue CitedMedium="Print">
<Volume>34</Volume>
<Issue>1</Issue>
</JournalIssue>...
我的android代码:
try {
articlesList = new ArrayList<>();
VTDGen vtdGen = new VTDGen();
vtdGen.setDoc(bytes);
vtdGen.parse(false);
AutoPilot ap = new AutoPilot();
VTDNav vtdNav = vtdGen.getNav();
int i = -1;
ap.bind(vtdNav);
ap.selectXPath("/MedlineCitationSet/MedlineCitation");
while ((ap.evalXPath()) != -1) {
articlesList.add(new Article());
String year = null, day = null, month = null;
i++;
if (vtdNav.hasAttr("Owner"))
articlesList.get(i).setOwner(vtdNav.toNormalizedString(vtdNav.getAttrVal("Owner")));
if (vtdNav.hasAttr("Status"))
articlesList.get(i).setStatus(vtdNav.toNormalizedString(vtdNav.getAttrVal("Status")));
vtdNav.push();
AutoPilot ap1 = new AutoPilot();
ap1.selectXPath("/MedlineCitationSet/MedlineCitation/PMID");
ap1.bind(vtdNav);
while ((ap1.evalXPath()) != -1) {
articlesList.get(i).setPMID(vtdNav.toNormalizedString(vtdNav.getText()));
articlesList.get(i).setVersion(vtdNav.toNormalizedString(vtdNav.getAttrVal("Version")));
}
ap1.resetXPath();
ap1.selectXPath("/MedlineCitationSet/MedlineCitation/DateCreated");
ap1.bind(vtdNav);
while ((ap1.evalXPath() != -1)) {
vtdNav.push();
AutoPilot ap1x = new AutoPilot();
ap1x.selectXPath("/MedlineCitationSet/MedlineCitation/DateCreated/Year");
ap1x.bind(vtdNav);
while ((ap1x.evalXPath()) != -1) {
year = vtdNav.toNormalizedString(vtdNav.getText());
}
ap1x.resetXPath();
ap1x.selectXPath("/MedlineCitationSet/MedlineCitation/DateCreated/Month");
ap1x.bind(vtdNav);
while ((ap1x.evalXPath()) != -1) {
month = vtdNav.toNormalizedString(vtdNav.getText());
}
ap1x.resetXPath();
ap1x.selectXPath("/MedlineCitationSet/MedlineCitation/DateCreated/Day");
ap1x.bind(vtdNav);
while ((ap1x.evalXPath()) != -1) {
day = vtdNav.toNormalizedString(vtdNav.getText());
}
articlesList.get(i).setDateCreated(day + "-" + month + "-" + year);
vtdNav.pop();
}
ap1.resetXPath();
ap1.selectXPath("/MedlineCitationSet/MedlineCitation/Article");
ap1.bind(vtdNav);
while ((ap1.evalXPath()) != -1) {
if (vtdNav.hasAttr("Print"))
articlesList.get(i).setPubModel(vtdNav.toNormalizedString(vtdNav.getAttrVal("Print")));
vtdNav.push();
AutoPilot ap2 = new AutoPilot();
ap2.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal");
ap2.bind(vtdNav);
{
vtdNav.push();
AutoPilot ap2x = new AutoPilot();
ap2x.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal/ISSN");
ap2x.bind(vtdNav);
while ((ap2x.evalXPath()) != -1) {
articlesList.get(i).setISSN(vtdNav.toNormalizedString(vtdNav.getText()));
articlesList.get(i).setIssnType(vtdNav.toNormalizedString(vtdNav.getAttrVal("IssnType")));
}
ap2x.resetXPath();
ap2x.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal/JournalIssue");
ap2x.bind(vtdNav);
while ((ap2x.evalXPath()) != -1) {
articlesList.get(i).setCitedMedium(vtdNav.toNormalizedString(vtdNav.getAttrVal("CitedMedium")));
vtdNav.push();
AutoPilot ap3 = new AutoPilot();
ap3.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal/JournalIssue/Volume");
ap3.bind(vtdNav);
while ((ap3.evalXPath()) != -1) {
articlesList.get(i).setVolume(vtdNav.toNormalizedString(vtdNav.getText()));
}
ap3.resetXPath();
ap3.selectXPath("/MedlineCitationSet/MedlineCitation/Article/Journal/JournalIssue/Issue");
ap3.bind(vtdNav);
while ((ap3.evalXPath()) != -1) {
articlesList.get(i).setIssue(vtdNav.toNormalizedString(vtdNav.getText()));
}
ap3.resetXPath();
vtdNav.pop();
}...
感谢您的帮助!
下面是一个代码片段,可帮助您提取文档中的相关字段。您对 AutoPilot 的使用存在问题。
例如,我建议您将所有 selectXPath 移出 while 循环,因为它们是相对较慢的操作。 此外,如果 xml有很深的嵌套,应该考虑开启VTDGen的selectLcDepth,设置为5。这有助于提高 navigation/xpath 性能。以下只是可以完成的示例。同样对于简单的 XPath,您可以使用 VTDNav 的本地游标 API,这更方便...
如果您有任何问题,请告诉我...
VTDGen vtdGen = new VTDGen();
vtdGen.selectLcDepth(5);
vtdGen.parseFile("c:\xml\agata.xml",false);
AutoPilot ap = new AutoPilot(),ap1=new AutoPilot(),
ap2=new AutoPilot(),ap3=new AutoPilot();
VTDNav vn = vtdGen.getNav();
int i = -1;
ap.bind(vn);ap1.bind(vn);ap2.bind(vn);ap3.bind(vn);
ap.selectXPath("/MedlineCitationSet/MedlineCitation");
ap1.selectXPath("PMID");
ap2.selectXPath("DateCreated");
ap3.selectXPath("Article");
while ((ap.evalXPath()) != -1) {
String year = null, day = null, month = null;
i++;
if (vn.hasAttr("Owner")) System.out.println("Owner==>"+vn.toNormalizedString(vn.getAttrVal("Owner")));
//articlesList.get(i).setOwner(vtdNav.toNormalizedString(vtdNav.getAttrVal("Owner")));
if (vn.hasAttr("Status"))
System.out.println("Stats==>"+vn.toNormalizedString(vn.getAttrVal("Status")));
//articlesList.get(i).setStatus(vtdNav.toNormalizedString(vtdNav.getAttrVal("Status")));
vn.push();
while((ap1.evalXPath())!=-1){
System.out.println("Version==>"+vn.toNormalizedString(vn.getAttrVal("Version")));
System.out.println("PMID==>"+vn.toNormalizedString(vn.getText()));
}
ap1.resetXPath();
vn.pop();
vn.push();
while((ap2.evalXPath())!=-1){
vn.toElement(VTDNav.FIRST_CHILD,"Year");
System.out.println("Year==>"+vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
vn.toElement(VTDNav.FIRST_CHILD,"Month");
System.out.println("Month==>"+vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
vn.toElement(VTDNav.FIRST_CHILD,"Day");
System.out.println("Day==>"+vn.toNormalizedString(vn.getText()));
vn.toElement(VTDNav.PARENT);
}
ap2.resetXPath();
vn.pop();
VN.push();
while((ap3.evalXPath())!=-1){
System.out.println("PubModel==>"+vn.toNormalizedString(vn.getAttrVal("PubModel")));
VN.pop();
}