dom4j生成和解析xml文件

xml文件内容



 
    
        杨磊
        24
        [email protected]
        
深圳南山
李四 20 [email protected]
广州番禺

SAXReader读取标签元素,节点,属性的基本操作

public static void readXml() {
        //创建解析器
        SAXReader reader = new SAXReader();
        try {
            //加载文件读到Document中
            Document document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
            //获取根元素的信息
            Element element = document.getRootElement();
            //打印根元素名称
            System.out.println(element.getName());
            //获取子元素
            List eleList = element.elements();
            for (Element element2 : eleList) {
                //打印子元素名称
                System.out.println(element2.getName());
                //获取元素属性
                Attribute attribute = element2.attribute(0);
                //获取元素属性名和属性值
                System.out.println(attribute.getName() + " " + attribute.getValue());
                //获取下一级子元素
                List eleList2 = element2.elements();
                for (Element element3 : eleList2) {
                    //获取下一级子元素的名称和值
                    System.out.println(element3.getName() + ": " + element3.getText());
                }
            }
            //根据元素名称获取根元素下的子元素(下一级子元素,没有找到会报空指针异常)
            Element ele = element.element("person");
            System.out.println(ele.getName());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
image.png
public static void readXml() {
        //创建解析器
        SAXReader reader = new SAXReader();
        try {
            //加载文件读到Document中
            Document document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
            //获取根元素的信息
            Element element = document.getRootElement();
            //打印根元素名称
            System.out.println(element.getName());
            //获取根元素的直接子节点
            Iterator nodeIterator = element.nodeIterator();
            while (nodeIterator.hasNext()) {
                Node node = nodeIterator.next();
                if (node instanceof Comment) {//是否为注释
                    System.out.println("注释内容:" + node.getText());
                }else if (node instanceof Element) {//是否为元素
                    System.out.println("标签名:" + node.getName());
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
image.png
public static void readXml() {
        //创建解析器
        SAXReader reader = new SAXReader();
        try {
            //加载文件读到Document中
            Document document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
            //获取根元素的信息
            Element element = document.getRootElement();
            //打印根元素名称
            System.out.println(element.getName());
            //获取所有的直接子元素
            List childs = element.elements();
            //遍历直接子元素
            for (Element element2 : childs) {
                //获取属性集合迭代器
                Iterator attrIterator = element2.attributeIterator();
                while (attrIterator.hasNext()) {
                    Attribute attribute = attrIterator.next();
                    System.out.println(attribute.getName() + " " + attribute.getValue());
                }
            }
            //获取根元素的person子元素的第一个属性
            System.out.println(element.element("person").attribute(0).getName());
            //获取根元素的person子元素的第一个属性值
            System.out.println(element.element("person").attribute(0).getValue());
            
            //获取根元素的person子元素的属性个数
            System.out.println(element.element("person").attributeCount());
            //按名称获取根元素的person子元素的属性值
            System.out.println(element.element("person").attributeValue("id"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
image.png

通过递归获取所有节点的标签名字

public static void main(String[] args) {
        //创建解析器
        SAXReader reader = new SAXReader();
        Document document;
        try {
            //加载文件读到Document中
            document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
            //获取根元素信息
            Element element = document.getRootElement();
            readXml(element);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    public static void readXml(Element element) {
        System.out.println(element.getName());
        List eleList = element.elements();
        for (Element element2 : eleList) {
            readXml(element2);
        }
    }
image.png

把xml文件的所有标签名名字、属性、标签体、注释、空白处 等...的内容全部打印出来,格式要求与xml文件的内容一致。

public static void main(String[] args) {
        //创建解析器
        SAXReader reader = new SAXReader();
        Document document;
        try {
            //加载文件读到Document中
            document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
            //获取根元素信息
            Element element = document.getRootElement();
            StringBuilder builder = new StringBuilder();
            readXml(element, builder);
            System.out.println(builder);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    public static void readXml(Element element, StringBuilder builder) {
        builder.append("<" + element.getName());
        //获取到属性迭代器,添加所有属性
        Iterator attrIterator = element.attributeIterator();
        while (attrIterator.hasNext()) {
            Attribute attribute = attrIterator.next();
            builder.append(" " + attribute.getName() + "=\"" + attribute.getValue() + "\"");
        }
        //添加结束标签的符号
        builder.append(">");
        //获取所有的子节点
        Iterator nodeIterator = element.nodeIterator();
        while (nodeIterator.hasNext()) {
            Node node = nodeIterator.next();
            if (node instanceof Text) {//文本
                builder.append(node.getText());
            }else if (node instanceof Comment) {
                builder.append("");
            }else if (node instanceof Element) {
                //迭代读下一个元素
                readXml((Element)node, builder);
            }
        }
        //添加结束标签
        builder.append("");
    }
image.png

dom4j生成xml文件

public static void main(String[] args) {
        File file = new File("E:\\test.xml");
        createXml(file);
    }
    
    public static void createXml(File file) {
        try {
            //创建document
            Document document = DocumentHelper.createDocument();
            //创建根节点
            Element element = document.addElement("root");
            //在根节点下添加第一个子节点
            Element firstElement = element.addElement("person").addAttribute("attr1", "value1");
            firstElement.addElement("people")
                    .addAttribute("attr", "first child")
                    .addText("person one child one");
            firstElement.addElement("people")
                    .addAttribute("attr", "second child")
                    .addText("person one child two");
            //在根节点下添加第二个子节点
            Element secondElement = element.addElement("person").addAttribute("attr2", "value2");
            secondElement.addElement("people")
                    .addAttribute("attr", "second child")
                    .addText("person two child one");
            secondElement.addElement("people")
                    .addAttribute("attr", "second child")
                    .addText("person two child two");
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            writer.write(document);
                        writer.close();
            System.out.println("create successfully");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



  
    person one child one
    person one child two
  
  
    person two child one
    person two child two
  

你可能感兴趣的:(dom4j生成和解析xml文件)