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

 

可以将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){

        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leboop-L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值