zoukankan      html  css  js  c++  java
  • 解析XML技术

    一、DOM(Document Object Model)

    1.介绍

    a.基于XML文档树结构的解析;

    b.适用于多次访问的XML文档;

    c.特点:比较消耗资源。

    代码示例:

    xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 引号中的URL内容用来唯一标识命名空间,不能重复 -->
    <students> 
        <student id="1">
            <name>stu1</name>
            <sex></sex>
            <age>21</age>
            <height>&gt;180</height>
            <class><![CDATA["元素<title>和</title>的使用"]]></class>
        </student>
        <student id="2">
            <name>stu2</name>
            <sex></sex>
            <age>19</age>
            <height>&lt;170</height>
            <class><![CDATA["元素<body>和</body>的使用"]]></class>
        </student>
        <student id="3">
            <name>stu3</name>
            <sex></sex>
            <age>20</age>
            <height>&lt;180</height>
            <class><![CDATA["元素<span>和</span>的使用"]]></class>
        </student>
        <student id="4">
            <name>stu3</name>
            <sex></sex>
            <age>20</age>
            <height>&lt;180</height>
            <class><![CDATA["其他元素的使用"]]></class>
        </student>
    </students>

    解析代码:

    package com.yh.xml;
    
    import java.io.IOException;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    public class ParseXMLDemo {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            try {
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document document = builder.parse("D:/eclipseWJ/HelloWorld/src/com/yh/xml/Demo2.xml");
                NodeList stus = document.getElementsByTagName("student");
                for (int i = 0; i < stus.getLength(); i++) {
                    Node nodeStu = stus.item(i);
                    Element eleStu = (Element) nodeStu;
                    String stuId = eleStu.getAttribute("id");
                    System.out.println(stuId + "号");
    
                    NodeList attrs = eleStu.getChildNodes();
                    for (int j = 0; j < attrs.getLength(); j++) {
                        Node nodeAttr = attrs.item(j);
                        if (nodeAttr.getNodeType() == Node.ELEMENT_NODE && nodeAttr.getNodeName() == "name") { // 判断当前节点是否为元素节点
                            Element eleAttr = (Element) nodeAttr;
                            String attrName = eleAttr.getTextContent();
                            System.out.println(attrName);
                        }
                    }
                }
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    二、SAX

    1.介绍

    a.基于事件的解析;

    b.适用于大数据量的XML文档;

    c.特点:占用资源少,内存消耗小。

    三、DOM4J

    1.介绍

    a.非常优秀的Java XML API;

    b.性能优异,功能强大;

    c.开放源代码。

  • 相关阅读:
    oracle基本语句
    html页面比较长,如何用js实现网页一打开显示在网页的中部?
    idea拉出Output窗口和还原窗口
    关于idea的目录结构如何变成树状,也就是横向变纵向
    IDEA -- idea无法导入HttpServlet包解决方法
    tomcat启动startup.bat一闪而过
    li标签和checkbox绑定
    利用jQuery对li标签操作
    &#65279导致页面顶部空白一行解决方法
    Myeclipse快速排版的快捷键
  • 原文地址:https://www.cnblogs.com/YeHuan/p/10820864.html
Copyright © 2011-2022 走看看