zoukankan      html  css  js  c++  java
  • XML操作



      用 Dom4j

    • Dom4j是轻量级的xml框架
    • 添加依赖

        新建一个maven项目,添加依赖如下:

        <dependency>
          <groupId>org.dom4j</groupId>
          <artifactId>dom4j</artifactId>
          <version>2.1.1</version>
        </dependency>
    •  读取代码
    public class Test01 {
      public static void main(String[] args) throws DocumentException {
        SAXReader reader=new SAXReader();
        System.out.println(new File(".").getAbsolutePath());
        //把 xml 文本文件转为一个 Java 对象
        Document doc=reader.read(Test01.class.getResourceAsStream("/person4.xml"));
        System.out.println(doc);
        //读取根
        Element root=doc.getRootElement();
        //根的属性
        Attribute attr=root.attribute("id");
        System.out.println(attr.getValue());
        //读取根的子元素
        List<Element> children=root.elements();
        for (Element element : children) {
          System.out.println(element.getText());
        }
      }
    •  创建代码
    public class Test02 {
      public static void main(String[] args) throws IOException {
        //文档不存在
        Document doc=DocumentHelper.createDocument();
        //1 根元素
        Element root=doc.addElement("person").addAttribute("id", "11");
        //2 向根中添加子元素
        root.addElement("name").addText("zhangsan");
        root.addElement("pwd").addText("123");
        //3 写向文件
        FileWriter writer=new FileWriter("person6.xml");
        doc.write(writer);
        //关闭流
        writer.close();
      }
    }
    •  修改代码
    public class Test03 {
      public static void main(String[] args) throws IOException, DocumentException {
        /**
        * 1)把 person6.xml 中的密码修改为 456 2)添加元素生日
        */
        // 1 读取文档
        SAXReader reader = new SAXReader();
        // 2 读取 xml 文档:把 xml 文本文件转为一个 Java 对象
        File f = new File("person6.xml");
        Document doc = reader.read(f);
        // 2 找到 pwd 元素
        Element root = doc.getRootElement();
        root.element("pwd").setText("456");
        // 3 添加
        root.addElement("birthday").setText("1998-1-1");
        // 4 保存
        Writer writer = new FileWriter(f);
        doc.write(writer);
        //关闭
        writer.close();
      }
    }

    用 jackson-xml

    • jackson-xml 是重量级的 xml 框架,它可以基于注解配置方式完成对象和 XML 之间自动转换
    • 新建一个JavaBean
    @JacksonXmlRootElement(localName="person")
    public class Person implements Serializable {
      private static final long serialVersionUID = 1L;
      @JacksonXmlProperty(isAttribute=true)//把默认是元素修改为属性
      private int id;
      private String name;
      @JsonIgnore(value=true)//忽略某个属性
      private String pwd;
      @JsonFormat(pattern="yyyy-MM-dd")
      private Date birthday;
      public Person() {
      }
      public Person(int id, String name, String pwd, Date birthday) {
        super();
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.birthday = birthday;
      }
      public int getId() {
        return id;
      }
      public void setId(int id) {
        this.id = id;
      }
    •  添加依赖
    <!-- 重量级 xml 操作框架 -->
    <dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>2.9.9</version>
    </dependency>
    •  把对象转为XML
    public class Test01 {
      public static void main(String[] args) throws JsonProcessingException {
        Person person=new Person(12, "sb", "123", new Date(1990, 2, 2));
        XmlMapper xmlMapper=new XmlMapper();
        //Object----->xml
        String xml=xmlMapper.writeValueAsString(person);
        System.out.println(xml);
      }
    }
    •  把XML转为对象
    public class Test02 {
      public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        XmlMapper xmlMapper=new XmlMapper();
        //xml---->Object
        Person person=xmlMapper.readValue("<person id="12"><name>sb</name><birthday>1990-02-01</birthday></person>",Person.class);
        System.out.println(person);
      }
    }


  • 相关阅读:
    linux软件安装
    [vim]使用中问题
    [vim]常用命令
    server
    linear regression
    loss function
    new bird in github
    [bzoj3489]A simple rmq problem
    [Jsoi2015]字符串树
    luogu3899谈笑风生
  • 原文地址:https://www.cnblogs.com/yuanshuai1026/p/11584723.html
Copyright © 2011-2022 走看看