Aga*_*ska 5 java xpath android arraylist vtd-xml
我有一个很大的,非常嵌套的XML文件.所有实体和属性都将成为我的Object变量.我正在创建此类对象的列表.我知道如何使用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>...
Run Code Online (Sandbox Code Playgroud)
我的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();
}...
Run Code Online (Sandbox Code Playgroud)
感谢您的任何帮助!
下面是一个代码片段,可帮助您提取文档中的相关字段。您使用 AutoPilot 时出现问题。
例如,我建议您将所有 selectXPath 移出 while 循环,因为它们是相对较慢的操作。 另外,如果 xml 具有深度嵌套,您应该考虑打开 VTDGen 的 selectLcDepth 并将其设置为 5。这有助于提高导航/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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
905 次 |
| 最近记录: |