2

y question is simple but I didn´t find anyone else with the same problem. I am trying to use JAXB to create a XML,so I have the class:

@XmlRootElement
public class Container{
private String name;
private String value;

public Container(String name, String value){
    this.name = name;
    this.value = value;
  }
}

Using the marshal :

public class Demo {

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Container.class);

    Container container = new Container("potatoes","5");


    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(container, System.out);
  }
 }

The output that I receive is:

<Container>
<name>potatoes</name>
<value>5</value>
 </Container>

is there any way of the output be like:

<Container>
<potatoes>5</potatoes>
</Container>

Using the JAXB?

Zebedeu
  • 95
  • 2
  • 9
  • 1
    See http://stackoverflow.com/questions/3293493/dynamic-tag-names-with-jaxb – helderdarocha May 23 '14 at 20:34
  • the output you want...potatoes = 5 and value = 5. (5 is being repeated). It does not make much sense. a tag would normally have some information within it.... may be you need to reconsider your need/design OR may be you gave a wrong example. – prembhaskal May 23 '14 at 21:28
  • This is an example. Of course I want to do something a little more meaningful. Nevertheless I adjusted the example :) – Zebedeu May 23 '14 at 21:43

1 Answers1

4

You can use an @XmlAnyElement-annotated property and return element as JAXBElement:

@XmlAnyElement
public JAXBElement<String> getThing() {
    return new JAXBElement<String>(new QName(this.name), String.class, this.value);
}
Reno
  • 51
  • 1