栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用vtd

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用vtd

我想其中有些取决于您要如何解析文件。

这是一个“非生产”示例,其中使用了一些有用的技术,包括:

  • XPath选择(此处仅使用“ / *”)
  • 浏览所有同级节点
  • 通过子节点向下看
  • 使用AutoPilot将节点属性提取到地图中

希望能帮助到你

package scce;import com.ximpleware.AutoPilot;import com.ximpleware.NavException;import com.ximpleware.VTDGen;import com.ximpleware.VTDNav;import com.ximpleware.XPathevalException;import com.ximpleware.XPathParseException;import java.io.File;import java.io.IOException;import java.util.linkedHashMap;import java.util.Map;public class VTDParserExample {    VTDGen vg;    VTDNav vn;    public VTDParserExample() {        vg = new VTDGen();        }    public void parseAndPrint() throws NavException {        int level = 0;        for(boolean el = (vn != null);          el == true ;         el = vn.toElement(VTDNav.NEXT_SIBLING)) { printTag(vn, level); parseAndPrintChildren(level);  }    }     private void parseAndPrintChildren(int level) throws NavException {        vn.push();        for(boolean el = vn.toElement(VTDNav.FIRST_CHILD);          el == true ;         el = vn.toElement(VTDNav.NEXT_SIBLING)) { printTag(vn, level + 1); parseAndPrintChildren(level + 1);  }        vn.pop();    }    public VTDNav loadFile(String filePath) throws IOException {        File fDoc = new File(filePath);        if (fDoc != null && fDoc.exists()) { System.out.println("loadFile file exists ["+filePath+"]"); vg.clear(); if (vg.parseFile(filePath, true)) {     vn = vg.getNav(); }        }        else { throw new IOException("File ["+filePath+"] invalid");        }        if (vn == null) {      throw new IOException("Cannot parse file ["+filePath+"]");        }        return vn;    }    public void getElementsByXpath() {        AutoPilot ap = new AutoPilot(vn);        try        { String xpQ = "public class VTDParserExample {    VTDGen vg;    VTDNav vn;    AutoPilot ap;    public VTDParserExample() {        vg = new VTDGen();        }    public void parseAndPrint() throws NavException {        int level = 0;        for(boolean el = (vn != null);          el == true ;         el = vn.toElement(VTDNav.NEXT_SIBLING)) { printTag(vn, level); parseAndPrintChildren(level);  }    }     private void parseAndPrintChildren(int level) throws NavException {        vn.push();        for(boolean el = vn.toElement(VTDNav.FIRST_CHILD);          el == true ;         el = vn.toElement(VTDNav.NEXT_SIBLING)) { printTag(vn, level + 1); parseAndPrintChildren(level + 1);  }        vn.pop();    }    private VTDNav loadFile(String filePath) throws IOException {        File fDoc = new File(filePath);        if (fDoc != null && fDoc.exists()) { System.out.println("loadFile file exists ["+filePath+"]"); vg.clear(); if (vg.parseFile(filePath, true)) {     vn = vg.getNav(); }        }        else { throw new IOException("File ["+filePath+"] invalid");        }        if (vn == null) {      throw new IOException("Cannot parse file ["+filePath+"]");        }        return vn;    }    public boolean getElementsByXpath() {        boolean found = false;        ap = new AutoPilot(vn);        try        { String xpQ = "//Machine"; ap.selectXPath(xpQ);     if (ap.evalXPathToBoolean()) {     found = true; } else {     System.out.println(this.getClass()+".getAllElements evalXPathToBoolean["+ap.evalXPathToBoolean()+"]"); }        }        catch(XPathParseException e) { e.printStackTrace();        }        return found;    }    private void loadAttributeMap(VTDNav nav, Map<String, String>amap) {        nav.push();        try { AutoPilot apAtt = new AutoPilot(nav); apAtt.selectXPath("@*"); int j=-1; while ((j=apAtt.evalXPath())!=-1) {     String name = nav.toString(j);     String val = nav.toString(j+1);     amap.put(name, val); }       }        catch(XPathParseException | XPathevalException | NavException e) { e.printStackTrace();        }        nav.pop(); }    private void printTag(VTDNav vn, int level) throws NavException {        String tag = vn.toString(vn.getCurrentIndex());        System.out.print("Level ["+level+"] Tag ["+tag+"]");        Map<String, String>amap = new linkedHashMap<String, String>();        loadAttributeMap(vn, amap);        for (String aname: amap.keySet()) { String aval = amap.get(aname); System.out.print(" @"+aname+"="+aval);        }        System.out.print("n");    }    public static void main(String[] args) {        VTDParserExample vp = new VTDParserExample();        try { vp.loadFile("src/scce/famedoro.xml"); if (vp.getElementsByXpath()) {      vp.parseAndPrintAP();      }        }        catch (Exception ex) { ex.printStackTrace();        }    }    private void parseAndPrintAP() {        int level = 0;        int result = -1;        try { while((result = ap.evalXPath())!=-1){     printTag(vn, level);     parseAndPrintChildren(level); }        } catch (XPathevalException | NavException ex) { ex.printStackTrace();        }    }}

这-将不同的XPath设置为“ // Machine”会产生:

loadFile file exists [src/scce/famedoro.xml]Level [0] Tag [Machine] @Number=1 @TotalPacks=4 @AvailablePacks=4 @StockReturnPacks=4 @BlockedPacks=0 @NextExpiryDate=2015-01-17Level [0] Tag [Machine] @Number=1 @TotalPacks=6 @AvailablePacks=6 @StockReturnPacks=6 @BlockedPacks=0 @NextExpiryDate=2015-01-17Level [0] Tag [Machine] @Number=1 @TotalPacks=16 @AvailablePacks=16 @StockReturnPacks=16 @BlockedPacks=0 @NextExpiryDate=2015-01-17Level [0] Tag [Machine] @Number=2 @TotalPacks=11 @AvailablePacks=11 @StockReturnPacks=11 @BlockedPacks=0 @NextExpiryDate=2014-04-17


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号