Unmarshalling a simple XML - java

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

Related

How can I make a class field become a tag name using JAXB?

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

JAXB2Marshaller not unmarshaling xml element into Character

I am having problem unmarshalling xml element into character using JAXB2Marshaller.
My XML input contains one element <dateCheckFlag>Y</dateCheckFlag>:
when I try to get dateCheckFlag element value into Character type in my pojo, it gives me null.
Assume all setter getter and constructor are present.
Can anybody help me out how to unmarshall xml element into Character...?
#XmlRootElement(name="Emp")
class Emp
{
#XmlElement(name="name");
String name;
#XmlElement(name="dateCheckFlag");
Character dateCheckFlag;
Emp(){}
Emp(String name, Character dateCheckFlag)
{
this.name= name;
this.dateCheckFlag = dateCheckFlag;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public void setDateCheckFlag(Character flag)
{
this.dateCheckFlag=flag;
}
public Character getName()
{
return dateCheckFlag;
}
The JAXB (JSR-222) specification does not define the XML representation for char/Character.
By default that JAXB reference implmentation converts a Character to the xs:unsignedShort type. This means that it is expecting a document like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Emp>
<name>Jane Doe</name>
<dateCheckFlag>89</dateCheckFlag>
</Emp>
XmlAdapter
You will be able to use an XmlAdapter to get the XML that you are looking for. An XmlAdapter allows you to convert one object to another for the purposes of changing the XML representation.
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class CharacterAdapter extends XmlAdapter<String, Character> {
#Override
public Character unmarshal(String string) throws Exception {
return string.charAt(0);
}
#Override
public String marshal(Character character) throws Exception {
return new String(new char[] {character});
}
}
Emp
The #XmlJavaTypeAdapter annotation is used to specify the XmlAdapter.
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
#XmlRootElement(name = "Emp")
#XmlAccessorType(XmlAccessType.FIELD)
class Emp {
String name;
#XmlJavaTypeAdapter(CharacterAdapter.class)
Character dateCheckFlag;
}
Demo
Below is some code that converts the XML to/from the object model.
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Emp.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum15240702/input.xml");
Emp emp = (Emp) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(emp, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<Emp>
<name>Jane Doe</name>
<dateCheckFlag>Y</dateCheckFlag>
</Emp>

Marshalling/unmarshalling fields to tag with attributes using JAXB

Let's say I have class Example:
class Example{
String myField;
}
I want to unmarshal it in this way:
<Example>
<myField value="someValue" />
</Example>
Is it possible to unmarshal object in such way using JAXB XJC? ( I know about XmlPath in EclipseLink, but can't use it).
You could leverage an XmlAdapter for this use case. In that XmlAdapter you will convert a String to/from an object that has one property mapped to an XML attribute.
XmlAdapter
package forum12914382;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class MyFieldAdapter extends XmlAdapter<MyFieldAdapter.AdaptedMyField, String> {
#Override
public String unmarshal(AdaptedMyField v) throws Exception {
return v.value;
}
#Override
public AdaptedMyField marshal(String v) throws Exception {
AdaptedMyField amf = new AdaptedMyField();
amf.value = v;
return amf;
}
public static class AdaptedMyField {
#XmlAttribute
public String value;
}
}
Example
package forum12914382;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
#XmlRootElement(name="Example")
#XmlAccessorType(XmlAccessType.FIELD)
class Example{
#XmlJavaTypeAdapter(MyFieldAdapter.class)
String myField;
}
Demo
package forum12914382;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Example.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum12914382/input.xml");
Example example = (Example) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(example, System.out);
}
}
input.xml/Output
<Example>
<myField value="someValue" />
</Example>
Related Example
JAXB Element mapping
Yes, manually add the #XmlAttribute-Annotation or generate the classes from an XSD.

JAXB location in file for unmarshalled objects

I have some objects being unmarshalled from an XML file by JAXB. Is it possible to have JAXB tell me or somehow find out where in the XML file (line and column) each object comes from?
This information is available at some point, because JAXB gives it to me during schema validation errors. But I would like to have it available for validated objects too.
You could do this in JAXB by leveraging an XMLStreamReader and an Unmarshaller.Listener:
Demo
package forum383861;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Unmarshaller.Listener;
import javax.xml.stream.Location;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Customer.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
FileInputStream xml = new FileInputStream("src/forum383861/input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
Unmarshaller unmarshaller = jc.createUnmarshaller();
LocationListener ll = new LocationListener(xsr);
unmarshaller.setListener(ll);
Customer customer = (Customer) unmarshaller.unmarshal(xsr);
System.out.println(ll.getLocation(customer));
System.out.println(ll.getLocation(customer.getAddress()));
}
private static class LocationListener extends Listener {
private XMLStreamReader xsr;
private Map<Object, Location> locations;
public LocationListener(XMLStreamReader xsr) {
this.xsr = xsr;
this.locations = new HashMap<Object, Location>();
}
#Override
public void beforeUnmarshal(Object target, Object parent) {
locations.put(target, xsr.getLocation());
}
public Location getLocation(Object o) {
return locations.get(o);
}
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<address/>
</customer>
Output
[row,col {unknown-source}]: [2,1]
[row,col {unknown-source}]: [3,5]
Customer
package forum383861;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Customer {
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address
package forum383861;
public class Address {
}
For More Information
http://blog.bdoughan.com/2011/08/using-unmarshallerlistener-to-capture.html
I'm afraid not. JAXB builds on top of a XML parser, this one will have built up a logical representation of your XML document forgetting the original string representation of your document.
The validation step is done while your string is still read in, so your parser is able to give you an error message telling you the position of the error. JAXB will only bypass that error message. But as soon as the XML is validated and parsed, only the logical representation will exist.

How to add an XML namespace (xmlns) when serializing an object to XML

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.

Categories

Resources