您的位置:首页 > 编程语言 > Java开发

java对象和xml之间转换--Jaxb--1

2016-10-26 19:03 399 查看
<?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 + "]";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: