java 解析 Xml ( VTDGen、AutoPilot )的应用

来源:互联网 发布:手机充电加速软件 编辑:程序博客网 时间:2024/06/04 17:49

java 解析 Xml ( VTDGen、AutoPilot )的应用 java 解析 Xml ( VTDGen、AutoPilot )的使用

今天主要用到了对xml的解析,现在做个记录,首先我贴一段我参考的别人的代码。里面的注释描述的很清楚。

package com.ytxsoft.xml;import com.ximpleware.AutoPilot;import com.ximpleware.VTDGen;import com.ximpleware.VTDNav;public class UserVTDXML { /**  * @param args  */ public static void main(String[] args) {  // TODO Auto-generated method stub  try{   VTDGen gen =new VTDGen();   //解析student.xml对象,不含有命名空间   gen.parseFile("f:\\student.xml", false);   VTDNav nav =gen.getNav();   AutoPilot pilot =new AutoPilot();   //将导航器绑定到pilot对像上   //如果把VTDNav表达为车辆上导航仪器的话,那么AutoPilot就代表开车人,他能更智能化的找到XPATH表达的含义   pilot.bind(nav);   //设置重新设置一个xpath表达式的字符串,但是通常是在之后调用   pilot.selectXPath("/students/student");   //evalXPath()返回nodeset集合中的下一个节点,如果检测到为匹配的节点,则返回为-1   System.out.println(pilot.evalXPath());   System.out.println(pilot.evalXPathToBoolean());   System.out.println(pilot.evalXPathToNumber());   System.out.println(pilot.evalXPathToString());   System.out.println(pilot.getExprString());   //最终操作XML的,还得让VTDNav来做,可见pilot也只是起了一个导航的作用   if(pilot.evalXPath()!=-1){    System.out.println(nav.toString(nav.getAttrVal("name")));   }  }catch(Exception ex){   ex.printStackTrace();  } }}

看过这段代码后相信已经有一些了解了,但仍可能和我一样还存在疑惑,下面是我修改后的一段代码,主要作用是:把下面这种xml文件解析:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMapConfig          PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"          "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"><body>    <id>0001</id>    <name>zhangsan</name>    <records>                <record>                    <school>frist</school>                </record>                <record>                    <school>second</school>                </record>    </records></body>

取出<record>下的<school>中的信息,则可以通过下面的代码实现:

package com.xml;import java.util.ArrayList;import java.util.List;import com.ximpleware.AutoPilot;import com.ximpleware.VTDGen;import com.ximpleware.VTDNav;public class TestXml {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        try {            VTDGen gen = new VTDGen();            // 解析student.xml对象,不含有命名空间            gen.parseFile("e:\\student.xml", false);            VTDNav nav = gen.getNav();            AutoPilot ap = new AutoPilot();            AutoPilot ape = new AutoPilot();            // 将导航器绑定到ap对像上            // 如果把VTDNav表达为车辆上导航仪器的话,那么Autoap就代表开车人,他能更智能化的找到XPATH表达的含义            ap.bind(nav);            ape.bind(nav);            // 设置重新设置一个xpath表达式的字符串,但是通常是在之后调用            ape.selectXPath("/body/records/record");            List<String> records = new ArrayList<String>();            String school= "" ;            // evalXPath()返回nodeset集合中的下一个节点,如果检测到为匹配的节点,则返回为-1            while(ape.evalXPath() != -1){                                ap.selectXPath("school");                school= ap.evalXPathToString();                records.add(school);            }            ape.resetXPath();            for(String s : records){                System.out.println("===" + s);            }        } catch (Exception ex) {            ex.printStackTrace();        }    }}

基本的就这样,介绍到这里了。

0 0
原创粉丝点击