Java-将JSON转换为根标记上有值的XML
我需要将JSON转换为XML格式,并在根标记中使用值 我使用Java-将JSON转换为根标记上有值的XML,java,json,xml,converter,Java,Json,Xml,Converter,我需要将JSON转换为XML格式,并在根标记中使用值 我使用@JsonRootName(“Orden”)配置了和Orden类,并将@JsonProperty添加到所有属性中 实际上,我有一个将JSON转换为XML的实现,但它包含“子节点中的值” 这是我的转换器实现: public static <T> String converToXml (T obj) throws CommonsException { JAXBContext jaxbContext;
@JsonRootName(“Orden”)
配置了和Orden类,并将@JsonProperty
添加到所有属性中
实际上,我有一个将JSON转换为XML的实现,但它包含“子节点中的值”
这是我的转换器实现:
public static <T> String converToXml (T obj) throws CommonsException {
JAXBContext jaxbContext;
String xml = null;
try {
OutputStream os = new ByteArrayOutputStream();
jaxbContext = JAXBContext.newInstance(obj.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(obj, os);
xml = os.toString();
} catch (JAXBException e) {
logger.error("XML converter exception: ", e);
throw new CommonsException("XML converter exception: ", e);
}
return xml;
}
publicstaticstringconvertJSONObjectToXML(objectobj)抛出JsonProcessingException{
ObjectMapper mapper=新的ObjectMapper();
configure(SerializationFeature.WRAP\u ROOT\u值,true);
setSerializationInclusion(JsonInclude.Include.NON_NULL);
字符串jsonString=mapper.writeValueAsString(obj);
JSONObject json=新的JSONObject(jsonString);
字符串xml=xml.toString(json);
返回xml;
}
实施的结果是:
C
109
C
2.5
72
156934
TS
辛克罗尼察
3.
0
2013-10-09T08:04:13
10
但是我需要下面的XML的简短、不友好或任何格式的名称
有什么想法吗?谢谢 根据Andreas发表的评论,我使用了JAXB,我的Orden类现在是混合类(可以从JSON或XML转换) 以下是对象的配置方式:
@XmlRootElement
public class IngresarOrden extends DatosOferta {
private String accion;
private ModoEjecucion ejecucion;
private String operador;
@XmlAttribute
public String getAccion() {
return accion;
}
public void setAccion(String accion) {
this.accion = accion;
}
@XmlAttribute
public ModoEjecucion getEjecucion() {
return ejecucion;
}
public void setEjecucion(ModoEjecucion ejecucion) {
this.ejecucion = ejecucion;
}
@XmlAttribute
public String getOperador() {
return operador;
}
public void setOperador(String operador) {
this.operador = operador;
}
@xmltattribute
是将值放入根标记中所需的注释
这是完整的转换器实现:
public static <T> String converToXml (T obj) throws CommonsException {
JAXBContext jaxbContext;
String xml = null;
try {
OutputStream os = new ByteArrayOutputStream();
jaxbContext = JAXBContext.newInstance(obj.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(obj, os);
xml = os.toString();
} catch (JAXBException e) {
logger.error("XML converter exception: ", e);
throw new CommonsException("XML converter exception: ", e);
}
return xml;
}
publicstaticstringconvertoxml(tobj)抛出异常{
JAXBContext JAXBContext;
字符串xml=null;
试一试{
OutputStream os=新的ByteArrayOutputStream();
jaxbContext=jaxbContext.newInstance(obj.getClass());
Marshaller jaxbMarshaller=jaxbContext.createMarshaller();
setProperty(Marshaller.JAXB_格式化的_输出,true);
jaxbMarshaller.marshall(obj,os);
xml=os.toString();
}捕获(JAXBEException e){
logger.error(“XML转换器异常:”,e);
抛出新的CommonException(“XML转换器异常:”,e);
}
返回xml;
}
谢谢 为什么要使用两个不同的JSON库?--无论如何,既然您正在使用Jackson
ObjectMapper
转换为JSON,为什么不使用JAXB将对象转换为XML呢?这样,您就可以使用JAXB注释完全控制XML生成。Thaks!我会根据你的评论给出答案