加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml解析的技术(dom,sax)

发布时间:2020-12-16 09:14:42 所属栏目:百科 来源:网络整理
导读:在xml的解析中,常用的有三种技术: dom解析,SAX解析,pull解析. 在dom解析中,将整个文档看成一颗dom树,并且将文档中的每个元素都看成节点对象.在解析的过程中,将整个文档都加载到内存.因此,dom解析有明显的缺点是消耗的内存比较大.但是,却可以随意的访问任意节

在xml的解析中,常用的有三种技术:
dom解析,SAX解析,pull解析.

在dom解析中,将整个文档看成一颗dom树,并且将文档中的每个元素都看成节点对象.在解析的过程中,将整个文档都加载到内存.因此,dom解析有明显的缺点是消耗的内存比较大.但是,却可以随意的访问任意节点的元素.

在sax解析与pull解析中,都是事件驱动形式的解析过程,解析过程大同小异.
只是,在sax解析的过程中,所有的事件驱动是由解析处理器管理的,而pull解析的过程中,需要我们手动的驱动事件.

另外,在sax解析中不能访问开始标签的下一标签文本内容,只能在character方法中
才能查看元素,所以,对于,对于文本节点内容的读取,要记录上一开始标签的标签名,
才能判断.在pull解析中,可以利用parser.nextText()来查看下一文本节点的内容.

部分的重点代码:
xml文件:

<employees xmlns:android="http://localhost:8080" xmlns:tool="http://baidu.com">
 <employee id="1">
   <android:name>李彦宏</android:name>
      <tool:name>百度</tool:name>
        <age>48</age>
      <address>北京</address>
       <sex>男</sex>
    </employee>
    </employees>

dom解析的过程:

//通过解析器获取文档对象
        factory=DocumentBuilderFactory.newInstance();
         builder=factory.newDocumentBuilder();
         document=builder.parse(file);

          //获取文档的根节点
         Element employees= document.getDocumentElement();
         //遍历所有的employee节点
         NodeList list1=employees.getElementsByTagName("employee");
  for(int i=0;i<list1.getLength();i++){
       Node employee=list1.item(i);
       //获取属性的节点
    Node idAtrr=employee.getAttributes().item(0);
     String id=idAtrr.getNodeValue();
     System.out.println("id:"+id);
     //获取所有的子节点
     NodeList list2=employee.getChildNodes();
     for(int j=0;j<list2.getLength();j++){
        Node node=list2.item(j);
        String nodeName=node.getNodeName();
        String context=node.getTextContent();
        if("android:name".equals(nodeName)){
        System.out.println("android:name"+context);
        }else if("tool:name".equals(nodeName)){
        System.out.println("tool:name"+context);
        }else if("age".equals(nodeName)){
        System.out.println("age:"+context);
        }else if("address".equals(nodeName)){
        System.out.println("address"+context);
        }
        }
        }

sax解析:

parser=factory.newSAXParser();
    //开启命名空间特性
    reader=parser.getXMLReader();
    reader.setFeature("http://xml.org/sax/features/namespaces",true);
    parser.parse(file,handler);
            自定义handler 重写其中的生命周期方法:




    class MyHandler extends DefaultHandler{


    public List<Employee> getEmployees() {
    return employees;
    }


    private List<Employee> employees=null;
    private Employee employee=null;
    private String preTag=null;

    @Override
    public void startDocument() throws SAXException {
    employees=new ArrayList<Employee>();
    }


    @Override
    public void endDocument() throws SAXException {
    System.out.println("解析完毕.");
    }


    @Override
    public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException {

    preTag=qName;
    if("employee".equals(qName)){
    String id=attributes.getValue(0);
    employee=new Employee();
    employee.setId(Integer.parseInt(id));
    }
    }


    @Override
    public void endElement(String uri,String qName)
    throws SAXException {
        if("employee".equals(qName)){
        employees.add(employee);
        }
        preTag=null;
    }


    @Override
    public void characters(char[] ch,int start,int length)
    throws SAXException {
    String str=new String(ch,start,length);
    if(preTag!=null){
    if("android:name".equals(preTag)){
    employee.setAndroidName(str);
    }else if("tool:name".equals(preTag)){
    employee.setToolName(str);
    }else if("age".equals(preTag)){
    employee.setAge(Integer.parseInt(str));
    }else if("sex".equals(preTag)){
    employee.setSex(str);
    }else if("address".equals(preTag)){
    employee.setAddress(str);
    }
    }
    }

    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读