如何在编组和解组时将相同的值映射到jaxb中的两个不同字段 [英] how to map same value to two different fields in jaxb while marshalling and unmarshalling
问题描述
我有一个xml标签Hello,我的java中有一个类似下面的字段
I have a xml tag Hello for which there is a field like below in my java
类HelloWorld {
@XmlElement
私有字符串名称;
}
虽然解组成功将Hello值赋给name变量。现在我想从这个java对象创建一个新的xml (HelloWorld),我正在进行编组,但在这种情况下,我想要一个xml标签而不是我的xml。
如何在Jaxb中实现这一点?
While unmarshalling this successfully assigns Hello value to name variable.Now I want to create a new xml from THIS java object(HelloWorld) for which I am doing the marshalling but in this case I want a xml tag as instead of in my xml. How can I acheive this in Jaxb?
两个Xml都不在我的控制范围内,所以我无法更改标签名称
Both Xml are not in my control so I cannot change the tag name
编辑:
传入XMl - helloworld.xml
Incoming XMl - helloworld.xml
<helloworld>
<name>hello</name>
</helloworld>
@XmlRootElement(name="helloworld)
class HelloWorld{
@XmlElement(name="name")
private String name;
// setter and getter for name
}
JaxbContext context = JAXBContext.newInstance(HelloWorld.class);
Unmarshaller un = conext.createUnmarshaller un();
HelloWorld hw = un.unmarshal(new File("helloworld.xml"));
System.out.println(hw.getName()); // this will print hello as <name> tag is mapped with name variable.
现在我想使用HelloWorld对象的这个hw对象来创建一个像下面的xml
Now I want to use this hw object of HelloWorld object to create a xml like below
<helloworld>
<name_1>hello</name_1> // Note <name> is changed to <name_1>
</helloworld>
我做不想创建像Helloworld这样的另一个类,并在该新类中声明变量name_1。我想重用这个HelloWorld class只是因为标签名称已被更改。
I do not want to create another class like Helloworld and declare a variable name_1 in that new class .I want to reuse this HelloWorld class only because just a tag name has been changed.
但我使用现有的HelloWorld类并尝试编组对象hw,如下所示
But I use existing HelloWorld class and try to marshal the object hw like below
JaxbContext context = JAXBContext.newInstance(HelloWorld.class);
Marshaller m = conext.createMarshaller();
StringWriter writer = new StringWriter();
m.marshal(hw,writer);
System.out.println(writer.toString());
这将打印如下
<helloworld>
<name>hello</name>
</helloworld>
但我需要
<helloworld>
<name_1>hello</name_1> // Note <name> is changed to <name_1>
</helloworld>
原因是解组前传入的xml和编组后的传出xml不在我的下面控制。
The reason for this is that the incoming xml before unmarshalling and the outgoing xml after marshalling is not under my control.
希望这能解释。
推荐答案
选项#1 - 适用于所有JAXB实现
您可以使用XSLT转换将 JAXBSource
转换为您想要的XML:
Option #1 - Works with all JAXB Implementations
You could use a XSLT transform to transform a JAXBSource
into the XML you want:
// Create Transformer
TransformerFactory tf = TransformerFactory.newInstance();
StreamSource xslt = new StreamSource(
"src/blog/jaxbsource/xslt/stylesheet.xsl");
Transformer transformer = tf.newTransformer(xslt);
// Source
JAXBContext jc = JAXBContext.newInstance(Library.class);
JAXBSource source = new JAXBSource(jc, catalog);
// Result
StreamResult result = new StreamResult(System.out);
// Transform
transformer.transform(source, result);
更多信息
- http://blog.bdoughan.com/2012/11/using-jaxb-with-xslt-to-produce-html.html
注意:我是MOXy领导。
MOXy提供了一个映射文档扩展,可用于覆盖字段/属性级别的映射。这意味着您可以仅在注释上创建一个 JAXBContext
,然后根据带有XML的注释创建第二个 JAXBContext
覆盖。
MOXy provides a mapping document extension that you can use to override the mappings at the field/property level. This means you could create one JAXBContext
on just the annotations, and then a second JAXBContext
based on the annotations with an XML overrride.
更多信息
- < a href =http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html =nofollow> http://blog.bdoughan.com/2010/12/extending- jaxb-represent-annotations.html
- http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
这篇关于如何在编组和解组时将相同的值映射到jaxb中的两个不同字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!