XML + dom4j

XML Syntax

XML refers to extensible markup language ( EXtensible Markup Language), is independent of software and hardware information transmission tool, used in many areas of web development, often used to simplify storage and sharing of data.

XML element is the time from (and including) the start tag until (and including) the end of the label. Mixture with other elements, text, or may contain elements of both. Element can also have attributes.

 

 

 

Wherein the datasource is an element, it can contain other elements, e.g. property. The same property is also an element, the element can contain elements in addition also contain text information. Element may contain attributes, such as name attribute property to have.

XML elements can contain attributes in the start tag, attributes (Attribute) provides additional (additional) information on the elements. Properties are not usually provide information and data components, but very important to the application needs to handle this element is.

XML attributes must be quoted, attribute values ​​must be surrounded by quotation marks, although single or double quotes can be used.

If the attribute value contains a double quote, then it is necessary to use single quotes surrounding it, or may use the entity reference.

XML elements must have a closing requires that each element must consist of the start and closing tags. The same start tag and a closing tag name, one more on the wording "/"

 

XML requires the existence of a root element, the so-called root element is not surrounded by other elements (not containing the parent element). And only one root element.

 

 

 

Entity references:

 

 

CDATA段:

 

 

XML parsing

SAX parsing mode:

SAX (simple API for XML) is an alternative method for parsing XML. Compared to DOM, SAX is a faster, more efficient method. It is progressive scan a document, while scanning resolution. And compared to the DOM, SAX parsing can be stopped at any time parsing the document. Its advantages and disadvantages are:

Advantages: parsing can begin immediately, fast, no memory pressure

Disadvantages: can not make changes to the node

DOM parsing mode:

DOM:(Document Object Model, 即文档对象模型) 是 W3C 组织推荐的处理 XML 的一种方式。DOM解析器在解析XML文档时,会把文档中的所有元素,按照其出现的层次关系,解析成一个个Node对象(节点)。其优缺点分别为:

优点:把xml文件在内存中构造树形结构,可以遍历和修改节点

缺点: 如果文件比较大,内存有压力,解析的时间会比较长

 

public static void main(String[] args) {
        /*
         * 解析XML的大致步骤
         * 1:创建SAXReader
         * 2:使用SAXReader读取XML文档并生成Document对象
         *   这一步就是DOM解析耗时耗资源的地方 因为要先将XML文档内容全部读取完毕
         *   并生成一个Document对象.
         * 3:通过Document获取根元素
         * 4:按照XML文档的结构从根元素开始 逐级获取子元素以达到遍历XML文档数据的目的.
         */
        
        /*
         * 该集合用于保存从emplist.xml文档中解析出来的员工信息
         */
        List<Emp> list=new ArrayList<Emp>();
        try{
            //1.创建SAXReader
            SAXReader reader=new SAXReader();
            
            //2.使用SAXReader读取XML文档并生成Document对象
            Document doc=reader.read(new FileInputStream("emplist.xml"));
            
            /*
             * Document提供了获取根元素的方法:
             * Element getRootElement()
             * 
             * Element 的每一个实例用于表示XML文档中的一个元素(一对标签)
             * 
             * Element 提供了获取其元素的相关信息的方法:
             * 
             * String getName()
             * 获取标签的名字
             * 
             * String getText()
             * 获取标签中间的文本信息(开始标签与结束标签之间的文本)
             * 
             * Elment element(String name)
             * 获取指定名字的子元素
             * 
             * List elements(String name)
             * 获取所有同名子元素
             * 
             * Attribute attribute(String name)
             * 获取指定名字的属性
             */
            Element root=doc.getRootElement();
            /*
             * 获取根元素<list>下面所有子元素
             * 若干个<emp>元素.而每一个<emp>元素也是使用Element实例表示,
             * 并存入集合后返回.
             */
            List<Element> empList= root.elements();
            for(Element empEle:empList){
                //获取员工姓名
                Element nameEle=empEle.element("name");
                String name=nameEle.getText();
                //获取age
                int age=Integer.parseInt(empEle.elementText("age"));
                /*
                 * Element有一个可以快速获取子标签中间的文本方法:
                 * String elementText(String name);
                 * 例如:
                 * empEle.elementText("gender")
                 * 等同于
                 * empEle.element("gender").getText()
                 * 
                 */
                //获取gender
                String gender=empEle.elementText("gender");
                //获取salary
                int salary=Integer.parseInt(empEle.elementText("salary"));               
                int id=Integer.parseInt(empEle.attributeValue("id"));                
                Emp emp=new Emp(id,name,age,gender,salary);
                list.add(emp);              
            }
            System.out.println("解析完毕!");
            for(Emp e:list){
                System.out.println(e);
            }                   
        }catch(Exception e){
            e.printStackTrace();
        }
    }

emplist.xml:
<?xml version="1.0" encoding="UTF-8"?>
<list>
     <emp id="1">
          <name>张三</name>
          <age>22</age>
          <gender>男</gender>
          <salary>2500</salary>
     </emp>
     <emp id="2">
          <name>李四</name>
          <age>25</age>
          <gender>女</gender>
          <salary>5000</salary>
     </emp>
     <emp id="3">
          <name>王五</name>
          <age>30</age>
          <gender>女</gender>
          <salary>7500</salary>
     </emp>
     <emp id="4">
          <name>赵六</name>
          <age>35</age>
          <gender>男</gender>
          <salary>6000</salary>
     </emp>
</list>

 

Emp 实体类:
/**
 * 表示一个员工的信息
 */
public class Emp {

    private  int id;
    
    private String name;
    
    private int age;
    
    private String gender;
    
    private int salary;
    
    public Emp(){
        
    }

    public Emp(int id, String name, int age, String gender, int salary) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }
    
    public String toString(){
        return id+","+name+","+age+","+gender+","+salary;
    }
    
}

 

XML:

 

/**
 * 使用DMO生成XML文档
 * @author jiyiyuan
 *
 */
public class WriteXMLDemo {
    public static void main(String[] args) {
        /*
         *生成XML文档的大致步骤
         *1:创建一个Document对象表示一个空白文档。
         *2:向Document对象中添加根元素 
         *3:按照预定的XML文档结构从根元素开始逐级添加子元素
         *4:创建XMLWriter
         *5:通过XMLWriter将Document写出以形成XML文档
         */
        
        List<Emp> list=new ArrayList<Emp>();
        list.add(new Emp(1,"安其拉",25,"",5000));
        list.add(new Emp(2,"妲己",15,"",25000));
        list.add(new Emp(3,"亚瑟",35,"",15000));
        list.add(new Emp(4,"典韦",55,"",7000));
        
        try{
            //1.创建一个Document对象表示一个空白文档。
            Document doc=DocumentHelper.createDocument();
            
            /*
             * Document 提供了添加根源的方法
             * Element addElement(String name)
             * 向当前文档中添加给定名字的根元素,并将它以Element
             * 实例返回,以便继续向根元素中追加内容
             * 需要注意,该方法只能调用一次,因为一个文档中只能有一个根元素
             */
            
            Element root=doc.addElement("list");
            /*
             * Element提供了向标签中添加信息的相关方法:
             * 
             * Element addElement(String name)
             * 添加给定名字的子标签并将其返回
             * 
             * Element addText(String text)
             * 添加文本信息,返回值为当前标签,这样做便于连续追加操作
             * 
             * Element addAttribute(String name,String value)
             * 添加给定名字及对应值的属性,返回值为当前标签
             * 
             * 
             * 将每一个emp实例以一个<emp>标签的形式 添加到根标签<list>中
             */
            for(Emp emp:list){
                Element empEle =root.addElement("emp");
                //添加<name>
                Element nameEle=empEle.addElement("name");
                nameEle.addText(emp.getName());
                //添加<age>
                Element ageEle=empEle.addElement("age");
                ageEle.addText(String.valueOf(emp.getAge()));
                //添加<gender>
                Element genderEle=empEle.addElement("gender");
                genderEle.addText(emp.getGender());
                //添加<salary>
                Element salaryEle=empEle.addElement("salary");
                salaryEle.addText(String.valueOf(emp.getSalary()));
                //添加id属性
                empEle.addAttribute("id",String.valueOf(emp.getId()));
            }
            //4.创建XMLWriter
            XMLWriter writer=new XMLWriter(new FileOutputStream("myemp.xml"),
                    OutputFormat.createPrettyPrint());
            //5.通过XMLWriter将Document写出以形成XML文档
            writer.write(doc);
            System.out.println("写出完毕!");
            writer.close();
        }catch(Exception e){
            e.printStackTrace();
        }   
        
    }

}

Guess you like

Origin www.cnblogs.com/jyy599/p/12088741.html