使用Jackson读取xml

   找了不少,什么峰的,什么dn的参差不齐的资料,废话不少,问题是导入的包也没有。不多废话,看下面代码直接复用。

package bean;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.FileInputStream;
import java.io.IOException;

/**
* @author Sunshine
* @version 1.0
* @date 2020/9/24 13:17
*/
public class ReadTest {
public static void main(String[] args) throws IOException {
FileInputStream input = new FileInputStream("c:\\aoligei\\dabaojian.xml(绝对路径,不知道啥是绝对路径就打开xml所在的文件夹,复制路径再加上文件名)");
XmlMapper mapper = new XmlMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Project pomXml = mapper.readValue(input, Project.class);
}
}

    这里要注意用Maven构建项目,构建过程就不展示了  idea自己整。不用也可以,下载了jar引入本地依赖也行。下面是依赖的Maven 坐标

Tips:绿色是主要依赖,后面的无关紧要。看你用不用。

<dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.11</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.11</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.8.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    这里需要注意到的是上面标注的红色部分

        Project pomXml = mapper.readValue(input, Project.class);

  这句是啥意思呢,言简意赅就是,按揭。按揭就是你想要啥就定义啥,需要注意的坑是实体类定义时,注意嵌套关系,比如说就像下面的pom.xml的格式:

project 标签:需定义成实体类,
parent标签:同上

  尤其注意下面标注粉色的位置,因为这种嵌套关系就好比List<对象> ,就是说  dependency 也得定义成实体类。然后project中dependencies的定义为List<Dependency > dependencies;而像蓝色部分的parent就可以在project中直接给parent 实体就ok,因为是一个对象下的属性 不用定义List。还有一种就是可以自定义标签名的注解是:
@JacksonXmlProperty(localName = "parent") 

  用法就是:

  @JacksonXmlProperty(localName = "parent")(这个是标签名)

  private String nipples;(这个是自定义的名,上面的标签自动映射成这个)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
      <groupId>org.example</groupId>
      <artifactId>jacksonXML</artifactId>
      <version>1.0-SNAPSHOT</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.11</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.11</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.8.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>    

  

posted on 2020-09-24 15:30  王半仙儿的博客  阅读(5303)  评论(0编辑  收藏  举报

导航