how to map same value to two different fields in jaxb while marshalling and unmarshalling

烂漫一生 提交于 2020-01-06 04:10:16

问题


I have a xml tag Hello for which there is a field like below in my java

class HelloWorld{ @XmlElement private String name; }

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?

Both Xml are not in my control so I cannot change the tag name

EDIT:

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.

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>

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.

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());

This will print like below

  <helloworld>
<name>hello</name>
  </helloworld>

but I require

  <helloworld>
<name_1>hello</name_1>   // Note <name> is changed to <name_1>
  </helloworld>

The reason for this is that the incoming xml before unmarshalling and the outgoing xml after marshalling is not under my control.

Hope this explains.


回答1:


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);

For More Information

  • http://blog.bdoughan.com/2012/11/using-jaxb-with-xslt-to-produce-html.html

Option #2 - Works with EclipseLink JAXB (MOXy)

Note: I'm the MOXy lead.

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.

For More Information

  • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html


来源:https://stackoverflow.com/questions/16444464/how-to-map-same-value-to-two-different-fields-in-jaxb-while-marshalling-and-unma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!