JAXBContext注解方式解析XML

    技术2022-09-02  90

    config.xml内容如下:

    <?xml version="1.0" encoding="UTF-8"?> <sql> <thread>1</thread> <jsonfiles> <jsonfile> <id>2</id> <name>zs</name> </jsonfile> <jsonfile> <id>56</id> <name>tt</name> </jsonfile> </jsonfiles> <jsonfiles> <jsonfile> <id>3</id> <name>ls</name> </jsonfile> </jsonfiles> </sql>

     

    sql标签对应实体类Sql:

    package com.leboop.www; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable; import java.util.List; /** * Created by leboop on 2020/7/1. */ @XmlRootElement(name = "sql") @XmlAccessorType(XmlAccessType.FIELD) public class Sql implements Serializable{ @XmlElement(name = "thread") private String thread; @XmlElement(name = "jsonfiles") private List<Jsonfiles> jsonfilesList; public String getThread() { return thread; } public void setThread(String thread) { this.thread = thread; } public List<Jsonfiles> getJsonfilesList() { return jsonfilesList; } public void setJsonfilesList(List<Jsonfiles> jsonfilesList) { this.jsonfilesList = jsonfilesList; } }

     

    jsonfiles标签对应实体类Jsonfiles:

    package com.leboop.www; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.util.List; /** * Created by leboop on 2020/7/2. */ @XmlRootElement(name = "jsonfiles") @XmlAccessorType(XmlAccessType.FIELD) public class Jsonfiles { @XmlElement(name = "jsonfile") private List<Jsonfile> jsonfileList; public List<Jsonfile> getJsonfileList() { return jsonfileList; } public void setJsonfileList(List<Jsonfile> jsonfileList) { this.jsonfileList = jsonfileList; } }

     

     jsonfile对应实体类Jsonfile:

    package com.leboop.www; import javax.xml.bind.annotation.XmlRootElement; /** * Created by leboop on 2020/7/2. */ @XmlRootElement(name = "jsonfile") @XmlAccessorType(XmlAccessType.FIELD) public class Jsonfile { private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

     

    JAXBcontext解析config.xml:

    package com.leboop.www; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; import java.io.File; import java.util.List; /** * Created by leboop on 2020/7/2. */ public class XmlMain { public static void main(String[] args) throws Exception { JAXBContext context = JAXBContext.newInstance(Sql.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Sql config = (Sql) unmarshaller.unmarshal( new File("G:\\idea_workspace\\oozie\\src\\main\\resources\\config.xml")); System.out.println(config.getThread()); for (Jsonfiles jsonfiles : config.getJsonfilesList()) { for (Jsonfile j : jsonfiles.getJsonfileList()) { System.out.println("id:" + j.getId() + ",name="+j.getName()); } } } }

    输出:

    1 id:2,name=zs id:56,name=tt id:3,name=ls

     

    可以将xml放在集群的HDFS上,如下加载xml:

    /** * Created by leboop on 2020/7/2. */ public class JAXBTest { public static void main(String[] args) { try { JAXBContext context = JAXBContext.newInstance(Boy.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Configuration conf = new Configuration(); conf.set("fs.defaultFS", "hdfs://192.168.128.11:9000"); FileSystem hdfs = FileSystem.get(conf); Path file = new Path("/input/boy.xml"); InputStream in = hdfs.open(file); Boy boy2 = (Boy) unmarshaller.unmarshal(in); System.out.println(boy2.getName()); }catch (JAXBException e){ }catch (IOException e){ } } }

     

    Processed: 0.011, SQL: 9