I'm having an issue generating XML via Java code. I have a class that for SailingAvailabilityResponseMessage. Within this class there are several other classes that are also used in the method I'm writing. Here is a snippet of what the SailingAvailabilityResponseMessage classes looks like:
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
#XmlType(propOrder={"type", "agency", "version", "release", "processingInfo", "advisoryMessage", "packageDescription", "sailingGroup", "registrationInformation"})
public class SailingAvailabilityResponseMessage {
private ProcessingInfo processingInfo;
private AdvisoryMessage advisoryMessage;
private PackageDescription packageDescription;
private SailingGroupResponse[] sailingGroup;
private RegistrationInformation registrationInformation;
String type;
#XmlAttribute
public String getType() {
return type;
}
The specific issue I'm encountering is related to the SailingGroupResponse[] sailingGroup;
SailingGroupResponse[] inherits from sailingGroup. Here is what sailingGroup looks like:
public class SailingGroup {
private SailingDescription sailingDescription;
public SailingDescription getSailingDescription() {
return sailingDescription;
}
public void setSailingDescription(SailingDescription sailingDescription) {
this.sailingDescription = sailingDescription;
}
And here is what SailingGroupResponse[] looks like:
import javax.xml.bind.annotation.XmlType;
#XmlType(propOrder={"sailingInformation", "diningDetails", "modeOfTransportation", "currencyInfo", "packageDescription"})
public class SailingGroupResponse extends SailingGroup {
private SailingInformation sailingInformation;
private DiningDetails diningDetails;
private ModeOfTransportation modeOfTransportation;
private CurrencyInfo currencyInfo;
private PackageDescription packageDescription;
The problem I'm encountering with the XML generation is that I'm expecting a tag of <sailingGroup> which I get but then I also get a tag of <sailingGroupResponse> which I do not want as it is not part of the original specs I'm following. I'm generating the XML via WebSphere RDi. I'm on SOAP 1.2 but the business partner I'm trying to develop for is on SOAP 1.1 so instead of being able to use JAX-WS runtime, I'm having to use JAX-RPC (apache AXIS will not allow me to generate).
Is anyone familiar with JAX-RPC runtime and issues similar to the one I'm asking? If anyone knows of a good site for me to visit for additional information, I'm willing to research further although so far I've not had much success in finding an answer.
Thanks,
Caren
Introducing polymorphism into JAXB-annotated classes usually causes complications, as you're finding out.
Try this:
#XmlElement(type = SailingGroup.class)
public SailingGroupResponse[] getSailingGroup() {
return sailingGroup;
}
And you may also need to add #XmlType to SailingGroup.
Are you sure that JAXB is producing the sailingGroupResponse tag? When I run the following stripped down example I get reasonable XML:
XML Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
<sailingGroup/>
<sailingGroup/>
</root>
Demo Code
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
public class Demo {
public static void main(String[] args) throws Exception {
SailingAvailabilityResponseMessage sarm = new SailingAvailabilityResponseMessage();
SailingGroupResponse[] sgr = new SailingGroupResponse[2];
sgr[0] = new SailingGroupResponse();
sgr[1] = new SailingGroupResponse();
sarm.setSailingGroup(sgr);
JAXBElement<SailingAvailabilityResponseMessage> rootElement = new JAXBElement<SailingAvailabilityResponseMessage>(new QName("root"), SailingAvailabilityResponseMessage.class, sarm);
JAXBContext jc = JAXBContext.newInstance(SailingAvailabilityResponseMessage.class, SailingGroup.class, SailingGroupResponse.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(rootElement, System.out);
}
}
SailingAvailabilityResponseMessage
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
#XmlType(propOrder = { "type", "sailingGroup"})
public class SailingAvailabilityResponseMessage {
private SailingGroupResponse[] sailingGroup;
String type;
public SailingGroupResponse[] getSailingGroup() {
return sailingGroup;
}
public void setSailingGroup(SailingGroupResponse[] sailingGroup) {
this.sailingGroup = sailingGroup;
}
#XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
SalingGroupResponse
public class SailingGroupResponse extends SailingGroup {
}
SailingGroup
public class SailingGroup {
}
Related
I have a class with #XmlAccessorType(XmlAccessType.FIELD) annotation, and each private and protected field is annotated with #XmlElement(name='...').
The challenge: I may want to rename one of the xml element names in a later stage. This leads me to the question. Is there a way to override/redefine these annotations, if I create a sub-class ?
I believe that some implementations of JaxB allow for XML configuration to override the annotations. In this case this may actually be possible. Here is an article from Eclipslink explaining how this can be done http://www.eclipse.org/eclipselink/documentation/2.4/solutions/jpatoxml004.htm
In my opinion you can just build an XML configuration for the JaxB file you want to override.
I tried first with the #XmlAccessorType(XmlAccessType.FIELD) and to hide with #XmlTransient. This only works, if you mark the field in the superclass and in the child class with #XmlTransient. But I assume, this is not what you want.
As second approach I've tried with more restrictive #XmlAccessorType(XmlAccessType.PROPERTY) in the superclass and #XmlAccessorType(XmlAccessType.NONE) in the child class. See here my example:
package com.so.example;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/myresource")
public class MyResource {
#GET
#Path("/car")
#Produces(MediaType.APPLICATION_XML)
public Car getCar() {
Car car = new Car();
car.setWheels(4);
return car;
}
#GET
#Path("/suv")
#Produces(MediaType.APPLICATION_XML)
public Suv getSuv() {
Suv suv = new Suv();
List<String> bigWheels = new ArrayList<>();
bigWheels.add("left front wheel");
bigWheels.add("right front wheel");
bigWheels.add("left rear wheel");
bigWheels.add("right rear wheel");
suv.setBigWheels(bigWheels);
return suv;
}
}
Class Car:
package com.so.example;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlAccessorType(XmlAccessType.PROPERTY)
#XmlRootElement
public class Car {
protected Integer wheels;
public Car() {
}
#XmlElement(name = "wheels", nillable = true)
public Integer getWheels() {
return wheels;
}
public void setWheels(Integer wheels) {
this.wheels = wheels;
}
}
Class Suv (Child):
package com.so.example;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
#XmlRootElement
#XmlAccessorType(XmlAccessType.NONE)
public class Suv extends Car {
#XmlTransient
private Integer wheels;
private List<String> bigWheels;
public Suv() {
}
#Override
#XmlTransient
public Integer getWheels() {
return wheels;
}
#Override
public void setWheels(Integer wheels) {
this.wheels = wheels;
}
#XmlElement
public List<String> getBigWheels() {
return bigWheels;
}
public void setBigWheels(List<String> bigWheels) {
this.bigWheels = bigWheels;
}
}
One way to "hide" the element wheels of the superclass would be to mark it as "nillable=true" and not use primitive types. In this case, the field wheels will be marshalled to <wheels xsi:nil="true"/>
If it's possible for you to not use the parent class for marshalling and you are only using child classes, you could use the approach described here:
http://blog.bdoughan.com/2011/06/ignoring-inheritance-with-xmltransient.html
Also you could use moxy and specify a custom binding:
http://www.eclipse.org/eclipselink/documentation/2.4/moxy/runtime003.htm
Whilst in java, to my knowledge, overriding an annotation #XmlElement(name='...') to change the name property is not possible; you can create a global variable in your code and either pass it through your classes or your functions following the #XmlElement(name='...').
In the code below I created a single class but it contains the setter and getter methods required if you want to pass it through to another class
#XMLAccessorType(XMLAccessType.FIELD)
public class YourClass {
#XmlTransient
private String string = ""; //This can be replaced with whatever variable you are manipulating
//That could be an int or a file or anything really
#XmlElement(name = "your_name")
private void doSomething() {
String temp = getString(); //This variable is normally used to pass between different
//classes but may as well use it if you have one
//Your code which manipulates the String
setString(temp); //This variable is normally used to pass between different classes but
//may as well use it if you have one
}
#XmlElement(name = "your_other_name")
private void doSomethingElse() {
String temp = getString();
//Your code which manipulates the String
setString(temp);
}
public void getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
}
I would reccomend looking at the Java Docs for #XmlTransient and these two other relevant SO questions.
How to override JAXB #XMLAccessorType(XMLAccessType.FIELD) specified at a Class level with #XMLElement on a getter method for a property?
Jaxb - Overriding the XMLElement name attribute
I am having hard time unmarshalling the following XML using JAXB. This is the xml which is having only one field with an attribute. I have referred many tutorials where they only do an example of reading a string in fields which s not helpful in my case.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CircuitImpactConfigs id="1">
<Objects>
<Object type="1" impactAnalysisDataBuilderClassName="tttttttt"/>
<Object type="2" impactAnalysisDataBuilderClassName="yyyyyyyyy"/>
<Object type="3" impactAnalysisDataBuilderClassName="eeeeeee" />
<Object type="4" impactAnalysisDataBuilderClassName="iiiiiiiii"/>
<Object type="5" impactAnalysisDataBuilderClassName="rrrrrrrrrr"/>
<Object type="6" impactAnalysisDataBuilderClassName="zzzzzz"/>
<Object type="7" impactAnalysisDataBuilderClassName="qqqqqqqqqqq"/>
</Objects>
<ForceSwitchMode name="FORCE_SWITCHED" />
</CircuitImpactConfigs>
Based on what i learnt from tutorial
My Classes are CircuitImpactConfigs.java
import java.util.List;
import javax.xml.bind.annotation.*;
#XmlRootElement(name = "CircuitImpactConfigs")
#XmlAccessorType(XmlAccessType.FIELD)
public class CircuitImpactConfigs {
private ForceSwitchMode ForceSwitchMode;
List<Obj> Object;
#XmlElement
public List<Obj> getObject() {
return Object;
}
public void setObject(List<Obj> object) {
Object = object;
}
#XmlElement
public ForceSwitchMode getForceSwitchMode() {
return ForceSwitchMode;
}
public void setForceSwitchMode(ForceSwitchMode forceSwitchMode) {
ForceSwitchMode = forceSwitchMode;
}
}
and
import javax.xml.bind.annotation.XmlAttribute;
public class ForceSwitchMode {
private String name;
#XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and
import javax.xml.bind.annotation.XmlAttribute;
public class Obj {
String type;
String impactAnalysisDataBuilderClassName;
#XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
#XmlAttribute
public String getImpactAnalysisDataBuilderClassName() {
return impactAnalysisDataBuilderClassName;
}
public void setImpactAnalysisDataBuilderClassName(String impactAnalysisDataBuilderClassName) {
this.impactAnalysisDataBuilderClassName = impactAnalysisDataBuilderClassName;
}
}
I am getting null list when doing the unmarshalling. This is the class where i create the JAXBcontext object and create unmarshalling object.
import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class CircuitImpactConfUnmarshaller {
public static void main(String[] args) throws JAXBException {
File file = new File("CircuitImpact.xml");
System.out.println(file.exists());
JAXBContext jaxbContext = JAXBContext.newInstance(CircuitImpactConfigs.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
CircuitImpactConfigs que = (CircuitImpactConfigs) jaxbUnmarshaller.unmarshal(file);
System.out.println(que.getForceSwitchMode().getName());
List<Obj> list = que.getObject();
System.out.println(list);
}
}
the last print statement is giving null. I understand i am doing something wrong in the class Obj
JAXB uses implicit naming conventions and explicit annotations to define a mapping between a XML and a Java structure.
Either element and attribute names in the XML match field names in Java (match by naming convention) or you need to use annotations to establish the mapping.
The Java list CircuitImpactConfigs.Object is not getting filled because the mapping failed, since the corresponding element in the XML is named Objects.
You can now either rename CircuitImpactConfigs.Object to CircuitImpactConfigs.Objects or use the name parameter of a JAXB annotation to define the corresponding name:
#XmlElement(name="Objects")
public List<Obj> getObject() {
EDIT: As you indicate in your comments there are still other mapping issues with your code. I would suggest that you adapt another approach:
Create a CircuitImpactConfigs object with all subobjects filled.
Marhsall that object to a XML file.
Check that the XML is in the expected format. If not, tweak the mapping.
Following this approach you can be sure that a XML file in the desired format can be unmarshalled to your Java structure. The code to marshall is
CircuitImpactConfigs que = ...
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(que, System.out);
I am using Java and JAXB for XML processing.
I have the following class:
public class Characteristic {
private String characteristic;
private String value;
#XmlAttribute
public String getCharacteristic() {
return characteristic;
}
public void setCharacteristic(String characteristic) {
this.characteristic = characteristic;
}
#XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public static void main(String[] args) {
Characteristic c = new Characteristic();
c.setCharacteristic("store_capacity");
c.setValue(40);
Characteristic c2 = new Characteristic();
c2.setCharacteristic("number_of_doors");
c2.setValue(4);
}
This is the result that I get:
<characteristics characteristic="store_capacity">40</characteristics>
<characteristics characteristic="number_of_doors">4</characteristics>
I want to get the following result:
<store_capacity>40</store_capacity>
<number_of_doors>4</number_of_doors>
How can I achieve this?
You can use a combination of #XmlElementRef and JAXBElement to produce dynamic element names.
The idea is:
Make Characteristic a subclass of JAXBElement and override the getName() method to return the name based on the characteristic property.
Annotate characteristics with #XmlElementRef.
Provide the #XmlRegistry (ObjectFactory) with an #XmlElementDecl(name = "characteristic").
Below is a working test.
The test itself (nothing special):
#Test
public void marshallsDynamicElementName() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
final Characteristics characteristics = new Characteristics();
final Characteristic characteristic = new Characteristic(
"store_capacity", "40");
characteristics.getCharacteristics().add(characteristic);
context.createMarshaller().marshal(characteristics, System.out);
}
Produces:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<characteristics><store_capacity>40</store_capacity></characteristics>
Let's start with the characteristics root element class. It has a characteristics property which is annotated with #XmlElementRef. This means that the contents should be either JAXBElements or #XmlRootElement-annotated class instances.
#XmlRootElement(name = "characteristics")
public class Characteristics {
private final List<Characteristic> characteristics = new LinkedList<Characteristic>();
#XmlElementRef(name = "characteristic")
public List<Characteristic> getCharacteristics() {
return characteristics;
}
}
In order for this to work you also need an ObjectFactory or something annotated with #XmlRegistry having a corresponding #XmlElementDecl:
#XmlRegistry
public class ObjectFactory {
#XmlElementDecl(name = "characteristic")
public JAXBElement<String> createCharacteristic(String value) {
return new Characteristic(value);
}
}
Recall, the characteristics property must contain either #XmlRootElement-annotated class instances or JAXBElements. #XmlRootElement is not suitable since it's static. But JAXBElement is dynamic. You can subclass JAXBElement and override the getName() method:
public class Characteristic extends JAXBElement<String> {
private static final long serialVersionUID = 1L;
public static final QName NAME = new QName("characteristic");
public Characteristic(String value) {
super(NAME, String.class, value);
}
public Characteristic(String characteristic, String value) {
super(NAME, String.class, value);
this.characteristic = characteristic;
}
#Override
public QName getName() {
final String characteristic = getCharacteristic();
if (characteristic != null) {
return new QName(characteristic);
}
return super.getName();
}
private String characteristic;
#XmlTransient
public String getCharacteristic() {
return characteristic;
}
public void setCharacteristic(String characteristic) {
this.characteristic = characteristic;
}
}
In this case I've overridden the getName() method to dynamically determine the element name. If characteristic property is set, its value will be used as the name, otherwise the method opts to the default characteristic element.
The code of the test is available on GitHub.
If you are using EclipseLink MOXy as your JAXB (JSR-222) provider then you can leverage our #XmlVariableNode extension for this use case (see: http://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-json-schema.html):
Java Model
Characteristics
We will leverage the #XmlVariableNode extension here. This annotation specifies a field/property from the reference class to be used as the element name.
import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlVariableNode;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Characteristics {
#XmlVariableNode("characteristic")
private List<Characteristic> characteristics;
}
Characteristic
We need to mark the characteristic field/property as #XmlTransient so it won't appear as a child element.
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
public class Characteristic {
#XmlTransient
private String characteristic;
#XmlValue
private String value;
}
jaxb.properties
To specify MOXy as your JAXB provider you need to have EclipseLink on your classpath and include a file called jaxb.properties with the following content in the same package as your domain model (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo Code
Demo
Here is some demo code that will read/write the desired XML. Note how the standard JAXB APIs are used.
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Characteristics.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("input.xml");
Characteristics characteristics = (Characteristics) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(characteristics, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<characteristics>
<store_capacity>40</store_capacity>
<number_of_doors>4</number_of_doors>
</characteristics>
The following approach will work with any JAXB (JSR-222) implementation.
Java Model
Characteristics
We will leverage an XmlAnyElement annotation. This annotation gives us alot of flexibility on what type of data can be held, including DOM nodes. We will use an XmlAdapter to convert instances of Characteristic to DOM nodes.
import java.util.List;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Characteristics {
#XmlAnyElement
#XmlJavaTypeAdapter(CharacteristicAdapter.class)
private List<Characteristic> characteristics;
}
Characteristic
As far as JAXB is concerned this class is no longer part of our model.
public class Characteristic {
String characteristic;
String value;
}
CharacteristicAdapter
This XmlAdapter converts the Characteristic object to and from a DOM node allowing us to construct it as we like.
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
public class CharacteristicAdapter extends XmlAdapter<Object, Characteristic> {
private Document doc;
public CharacteristicAdapter() {
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
#Override
public Characteristic unmarshal(Object v) throws Exception {
Element element = (Element) v;
Characteristic characteristic = new Characteristic();
characteristic.characteristic = element.getLocalName();
characteristic.value = element.getTextContent();
return characteristic;
}
#Override
public Object marshal(Characteristic v) throws Exception {
Element element = doc.createElement(v.characteristic);
element.setTextContent(v.value);
return element;
}
}
Demo Code
Demo
Below is some code that will read/write the desired XML. Note that the setAdapter call on Marshaller is not required, but is a performance improvement since it will cause the XmlAdapter to be reused.
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Characteristics.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("input.xml");
Characteristics characteristics = (Characteristics) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setAdapter(new CharacteristicAdapter());
marshaller.marshal(characteristics, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<characteristics>
<store_capacity>40</store_capacity>
<number_of_doors>4</number_of_doors>
</characteristics>
you can specify your class as :
#XmlRootElement(name = "Characteristic")
public class Characteristic {
#XmlElement(name = "store_capacity")
protected String storeCapacity;
#XmlElement(name = "number_of_doors")
protected String numberOfDoors;
/** getters and setters of above attributes **/
}
EDIT : If you wants the attributes to be dynamic then you can refer below link
getting Dynamic attribute for element in Jaxb
I'm serializing Objects to XML with the help of XStream.
How do I tell XStream to insert an xmlns to the XML output of my object?
As an example, I have this simple object I want to serialize:
#XStreamAlias(value="domain")
public class Domain
{
#XStreamAsAttribute
private String type;
private String os;
(...)
}
How do I achieve exactly the following output with XStream?
<domain type="kvm" xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0">
<os>linux</os>
</domain>
XStream doesn't support namespaces but the StaxDriver it uses, does. You need to set the details of your namespace into a QNameMap and pass that into the StaxDriver:
QNameMap qmap = new QNameMap();
qmap.setDefaultNamespace("http://libvirt.org/schemas/domain/qemu/1.0");
qmap.setDefaultPrefix("qemu");
StaxDriver staxDriver = new StaxDriver(qmap);
XStream xstream = new XStream(staxDriver);
xstream.autodetectAnnotations(true);
xstream.alias("domain", Domain.class);
Domain d = new Domain("kvm","linux");
String xml = xstream.toXML(d);
Output:
<qemu:domain type="kvm" xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0">
<qemu:os>linux</qemu:os>
</qemu:domain>
Alternatively, this use case could be handled quite easily with a JAXB implementation (Metro, EclipseLink MOXy, Apache JaxMe, etc):
Domain
package com.example;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Domain
{
private String type;
private String os;
#XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getOs() {
return os;
}
public void setOs(String os) {
this.os = os;
}
}
package-info
#XmlSchema(xmlns={
#XmlNs(
prefix="qemu",
namespaceURI="http://libvirt.org/schemas/domain/qemu/1.0")
})
package com.example;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;
Demo
package com.example;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(Domain.class);
Domain domain = new Domain();
domain.setType("kvm");
domain.setOs("linux");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(domain, System.out);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<domain xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0" type="kvm">
<os>linux</os>
</domain>
For More Information
http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html
http://bdoughan.blogspot.com/2010/10/how-does-jaxb-compare-to-xstream.html
This is a bit of a hack, but it's quick and easy: add a field to your class called xmlns, and only have it non-null during serialisation. To continue your example:
#XStreamAlias(value="domain")
public class Domain
{
#XStreamAsAttribute
private String type;
private String os;
(...)
#XStreamAsAttribute
#XStreamAlias("xmlns:qemu")
String xmlns;
public void serialise(File path) {
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(Domain.class);
(...)
PrintWriter out = new PrintWriter(new FileWriter(path.toFile()));
xmlns = "http://libvirt.org/schemas/domain/qemu/1.0";
xstream.toXML(this, out);
xmlns = null;
}
}
To be complete, setting xmlns = null should be in a finally clause. Using a PrintWriter also allows you to insert an XML declaration at the start of the output, if you like.
I need to create some xml to send off to another application in the below format and I'm trying to use jaxb. There is always only 1 occurrence of bar. Is there a way to do this or is it invalid xml because it has 2 root elements and the other application will need to change how it accepts the xml?
<FOO>
<BAR>
<id>1</id>
<POINTS>111</POINTS>
<CODE>123</CODE>
</BAR>
</FOO>
Below are a couple of approaches:
Standard JAXB
The following will work with any JAXB implementation (Metro, MOXy, JaxMe, etc). You could create the element "FOO", and then marshal your instance of Bar into it. The code below how to demonstrate this using StAX (it could also be done with DOM or SAX):
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
public class Demo {
public static void main(String[] args) throws Exception {
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
xsw.writeStartDocument();
xsw.writeStartElement("FOO");
Bar bar = new Bar();
bar.setId(1);
bar.setPoints(111);
bar.setCode(123);
JAXBContext jc = JAXBContext.newInstance(Bar.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.marshal(bar, xsw);
xsw.writeEndElement();
xsw.writeEndDocument();
xsw.close();
}
}
EclipseLink JAXB (MOXy)
If you happen to be using EclipseLink JAXB (MOXy), then you can use the #XmlPath extension (I'm the MOXy tech lead). Your Bar class would look like:
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="FOO")
#XmlType(propOrder={"id", "points", "code"})
public class Bar {
private int id;
private int points;
private int code;
#XmlPath("BAR/id/text()")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#XmlPath("BAR/POINTS/text()")
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
#XmlPath("BAR/CODE/text()")
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
For more information see:
http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html
http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html