JAXBContext註解方式解析XML

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章