java對象和xml之間轉換--Jaxb--1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OPC>
    <opcServiceIP>localhost</opcServiceIP>
    <port>8989</port>
    <gap>0/1 * * * * ?</gap>
    <OPCGap>500</OPCGap>
    <percentDeadBand>0.0</percentDeadBand>
    <items>
        <item>23FC0020.PV</item>
        <item>23FC0020.SP</item>
        <item>23FC0020.OP</item>
        <item>23FC0020.ILOCK</item>
        <item>23FC0020.T1</item>
        <item>23FC0020.K</item>
        <item>42FC0040.PV</item>
        <item>42FC0040.SP</item>
        <item>42FC0040.OP</item>
        <item>57PC0020.PV</item>
        <item>57PC0020.SP</item>
        <item>57PC0020.OP</item>
        <item>57PC0020.PTDESC</item>
        <item>57PC0020.PTINAL</item>
        <item>59XV0011.PV</item>
        <item>59XV0011.STARTS</item>
        <item>59XV0011.CALC1</item>
        <item>59HX0021.PV</item>
        <item>59HX0021.OP</item>
        <item>59HX0022.PV</item>
    </items>
</OPC>




public static void readConfig(){
        String path = "config/config.xml";
        try {
            JAXBContext context = JAXBContext.newInstance(OPCEntity.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            OPCEntity unmarshal = (OPCEntity) unmarshaller.unmarshal(new File(path));
            DataUtil.opcEntity = unmarshal;
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); 
        }
    }


package common;

import java.util.Set;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="OPC")
public class OPCEntity {
    @XmlElement(name="port")
    private Integer port;
    @XmlElement(name="gap")
    private String gap;
    @XmlElement(name="OPCGap")
    private String OPCGap;
    @XmlElement(name="opcServiceIP")
    private String opcServiceIP;
    @XmlElement(name="percentDeadBand")
    private String percentDeadBand;
    @XmlElementWrapper(name="items")
    @XmlElement(name="item")
    private Set<String> items;
    public Integer getPort() {
        return port;
    }
    public String getGap() {
        return gap;
    }
    public String getOPCGap() {
        return OPCGap;
    }
    public String getPercentDeadBand() {
        return percentDeadBand;
    }
    public Set<String> getItems() {
        return items;
    }

    public String getOpcServiceIP() {
        return opcServiceIP;
    }
    @Override
    public String toString() {
        return "OPCEntity [port=" + port + ", gap=" + gap + ", OPCGap="
                + OPCGap + ", percentDeadBand=" + percentDeadBand + ", items="
                + items + "]";
    }

}



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