zoukankan      html  css  js  c++  java
  • dom4j解析XML文档

    xml

    <?xml version="1.0" encoding="UTF-8"?>
    <university name="pku">
        <college name="c1">
            <class name="class1">
                <student name="stu1" sex='male' age="21" />
                <student name="stu2" sex='female' age="20" />
                <student name="stu3" sex='female' age="20" />
            </class>
            <class name="class2">
                <student name="stu4" sex='male' age="19" />
                <student name="stu5" sex='female' age="20" />
                <student name="stu6" sex='female' age="21" />
            </class>
        </college>
        <college name="c2">
            <class name="class3">
                <student name="stu7" sex='male' age="20" />
            </class>
        </college>
        <college name="c3">
        </college>
    </university>

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;

    import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.ProcessingInstruction;
    import org.dom4j.VisitorSupport;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;

    /**
    * Dom4j读写xml
    * @author whwang
    */
    public class TestDom4j {
    public static void main(String[] args) {
    read1();
    // read2();
    write();
    }

    public static void read1() {
    try {
    SAXReader reader = new SAXReader();
    InputStream in = TestDom4j.class.getClassLoader().getResourceAsStream("university.xml");
    Document doc = reader.read(in);
    Element root = doc.getRootElement();
    readNode(root, "");
    } catch (DocumentException e) {
    e.printStackTrace();
    }
    }

    @SuppressWarnings("unchecked")
    public static void readNode(Element root, String prefix) {
    if (root == null) return;
    // 获取节点的属性
    List<Attribute> attrs = root.attributes();
    if (attrs != null && attrs.size() > 0) {
    System.err.print(prefix);
    for (Attribute attr : attrs) {
    System.err.print(attr.getValue() + " ");
    }
    System.err.println();
    }
    // 获取他的子节点
    List<Element> childNodes = root.elements();
    prefix += " ";
    for (Element e : childNodes) {
    readNode(e, prefix);
    }
    }

    public static void read2() {
    try {
    SAXReader reader = new SAXReader();
    InputStream in = TestDom4j.class.getClassLoader().getResourceAsStream("university.xml");
    Document doc = reader.read(in);
    doc.accept(new MyVistor());
    } catch (DocumentException e) {
    e.printStackTrace();
    }
    }



    /**
    * 写入方法
    */
    public static void write() {
    try {
    // 创建一个xml文档
    Document doc = DocumentHelper.createDocument();
    Element university = doc.addElement("university");
    university.addAttribute("name", "tsu");
    // 注释
    university.addComment("这个是根节点");
    Element college = university.addElement("college");
    college.addAttribute("name", "cccccc");
    college.setText("text");

    File file = new File("src/dom4j-modify.xml");
    if (file.exists()) {
    file.delete();
    }
    file.createNewFile();
    XMLWriter out = new XMLWriter(new FileWriter(file));
    out.write(doc);
    out.flush();
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    class MyVistor extends VisitorSupport {
    public void visit(Attribute node) {
    System.out.println("Attibute: " + node.getName() + "="
    + node.getValue());
    }

    public void visit(Element node) {
    if (node.isTextOnly()) {
    System.out.println("Element: " + node.getName() + "="
    + node.getText());
    } else {
    System.out.println(node.getName());
    }
    }

    @Override
    public void visit(ProcessingInstruction node) {
    System.out.println("PI:" + node.getTarget() + " " + node.getText());
    }
    }

  • 相关阅读:
    win10 安装cmake报错: "Installation directory must be on a local drive"
    python量化笔记16之夏普比率计算公式
    python机器量化之十七:混淆矩阵(confusion_matrix)含义
    使用Aspose.word (Java) 填充word文档数据
    python笔记(十八)机器量化分析—数据采集、预处理与建模
    js 使FORM表单的所有元素不可编辑的示例代码 表
    Web前端面试题:写一个mul函数
    vue + electron 快速入门
    Java新特性stream流
    mycat中间件进行MySQL数据表的水平拆分
  • 原文地址:https://www.cnblogs.com/bingbingJava/p/3624770.html
Copyright © 2011-2022 走看看