Jaxb 如何执行XML文件的编组和解编组?
我需要执行此XML的编组和解编组 代码:Jaxb 如何执行XML文件的编组和解编组?,jaxb,Jaxb,我需要执行此XML的编组和解编组 代码: 试试这个: @XmlRootElement public class GlobalConfig { @XmlElementWrapper(name = "DataSources") protected List<DataSource> dataSources; private static JAXBContext context; private GlobalConfig() { /*
试试这个:
@XmlRootElement
public class GlobalConfig {
@XmlElementWrapper(name = "DataSources")
protected List<DataSource> dataSources;
private static JAXBContext context;
private GlobalConfig() {
/* For JAXB */
}
/**
* @return A marshaller for instances of {@link GlobalConfig}.
* @throws JAXBException
* If the marshaller cannot be obtained.
*/
public static Marshaller getMarshaller() throws JAXBException {
final Marshaller marshaller = getJAXBContext().createMarshaller();
try {
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
} catch (PropertyException __) {
System.out.println("Cannot format XML of GlobalConfig.");
/* That's okay */
}
return marshaller;
}
/**
* @return An unmarshaller for instances of {@link GlobalConfig}
* @throws JAXBException
* If the unmarshaller cannot be obtained.
*/
public static Unmarshaller getUnmarshaller() throws JAXBException {
final Unmarshaller um = getJAXBContext().createUnmarshaller();
um.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
return um;
}
/**
* @return The {@link JAXBContext} to use to marshall/unmarshall instances of
* {@link GlobalConfig}
* @throws JAXBException
* If the context cannot be obtained.
*/
private synchronized static JAXBContext getJAXBContext() throws JAXBException {
if (context == null)
context = JAXBContext.newInstance(GlobalConfig.class);
return context;
}
public static class DataSource {
@XmlAttribute
protected int id;
@XmlAttribute
protected String source;
@XmlAttribute(name = "type")
protected String type_;
@XmlAttribute
protected String dbInstance;
@XmlAttribute
protected String urlFunction;
@XmlAttribute
protected String dataProvider;
private DataSource() {
/* For JAXB */
}
}
@XmlRootElement
公共类全局配置{
@XmlElementWrapper(name=“数据源”)
受保护列表数据源;
私有静态JAXBContext上下文;
私有GlobalConfig(){
/*对于JAXB*/
}
/**
*@返回{@link GlobalConfig}实例的封送处理程序。
*@jaxbeexception
*如果无法获得封送员。
*/
公共静态封送处理程序getMarshaller()抛出JAXBEException{
final Marshaller Marshaller=getJAXBContext().createMarshaller();
试一试{
setProperty(marshaller.JAXB_格式化的_输出,true);
}捕获(属性异常){
System.out.println(“无法格式化GlobalConfig的XML”);
/*没关系*/
}
返回编组员;
}
/**
*@return{@link GlobalConfig}实例的解组器
*@jaxbeexception
*如果无法获取解组器。
*/
公共静态解组器getUnmarshaller()抛出JAXBEException{
final Unmarshaller um=getJAXBContext().createUnmarshaller();
setEventHandler(新的javax.xml.bind.helpers.DefaultValidationEventHandler());
返回嗯,;
}
/**
*@return{@link JAXBContext}用于marshall/unmarshall的实例
*{@link GlobalConfig}
*@jaxbeexception
*如果无法获取上下文。
*/
私有同步静态JAXBContext getJAXBContext()抛出JAXBException{
if(上下文==null)
context=JAXBContext.newInstance(GlobalConfig.class);
返回上下文;
}
公共静态类数据源{
@XmlAttribute
受保护的int-id;
@XmlAttribute
受保护的字符串源;
@XmlAttribute(name=“type”)
受保护字符串类型;
@XmlAttribute
受保护的字符串实例;
@XmlAttribute
受保护的字符串函数;
@XmlAttribute
受保护的字符串数据提供者;
私有数据源(){
/*对于JAXB*/
}
}
使用getMarshaller和getUnmarshaller方法封送/取消封送。您尝试了什么?您的问题到底是什么?上下文是什么?请确保您提出了一个高质量的问题。谢谢!在这个网站上,您应该尝试自己编写代码。如果有问题,您可以发布您尝试使用的代码清楚地解释什么是不起作用的,并提供一个答案。我建议读一个好的问题,并且。另外,一定要记下答案并阅读。
@XmlRootElement
public class GlobalConfig {
@XmlElementWrapper(name = "DataSources")
protected List<DataSource> dataSources;
private static JAXBContext context;
private GlobalConfig() {
/* For JAXB */
}
/**
* @return A marshaller for instances of {@link GlobalConfig}.
* @throws JAXBException
* If the marshaller cannot be obtained.
*/
public static Marshaller getMarshaller() throws JAXBException {
final Marshaller marshaller = getJAXBContext().createMarshaller();
try {
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
} catch (PropertyException __) {
System.out.println("Cannot format XML of GlobalConfig.");
/* That's okay */
}
return marshaller;
}
/**
* @return An unmarshaller for instances of {@link GlobalConfig}
* @throws JAXBException
* If the unmarshaller cannot be obtained.
*/
public static Unmarshaller getUnmarshaller() throws JAXBException {
final Unmarshaller um = getJAXBContext().createUnmarshaller();
um.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
return um;
}
/**
* @return The {@link JAXBContext} to use to marshall/unmarshall instances of
* {@link GlobalConfig}
* @throws JAXBException
* If the context cannot be obtained.
*/
private synchronized static JAXBContext getJAXBContext() throws JAXBException {
if (context == null)
context = JAXBContext.newInstance(GlobalConfig.class);
return context;
}
public static class DataSource {
@XmlAttribute
protected int id;
@XmlAttribute
protected String source;
@XmlAttribute(name = "type")
protected String type_;
@XmlAttribute
protected String dbInstance;
@XmlAttribute
protected String urlFunction;
@XmlAttribute
protected String dataProvider;
private DataSource() {
/* For JAXB */
}
}