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>
Related
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 am working with JAXB to marshal and unmarshal a java class.
This is the xml I'm looking for:
<tag name="example" attribute1="enumValue"/>
if attribute1 is set to the default value, I don't want that attribute to print at all, so it would look like this:
<tag name="example"/>
Is there a way to do this?
Right now I have a getter/setter pair that looks like this:
#XmlAttribute(name="attribute1")
public EnumExample getEnumExample() {
return this.enumExample;
}
public void setEnumExample(final EnumExample enumExample) {
this.enumExample = enumExample;
}
You could use an XmlAdapter for this use case:
XmlAdapter (Attribute1Adapter)
You could leverage the fact that JAXB will not marshal null attribute values and use an XmlAdapter to adjust the value being marshalled to XML.
import javax.xml.bind.annotation.adapters.XmlAdapter;
import forum16972549.Tag.Foo;
public class Attribute1Adapter extends XmlAdapter<Tag.Foo, Tag.Foo>{
#Override
public Foo unmarshal(Foo v) throws Exception {
return v;
}
#Override
public Foo marshal(Foo v) throws Exception {
if(v == Foo.A) {
return null;
}
return v;
}
}
Domain Model (Tag)
The #XmlJavaTypeAdapter annotation is used to associate the XmlAdapter.
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Tag {
enum Foo {A, B};
#XmlAttribute
#XmlJavaTypeAdapter(Attribute1Adapter.class)
private Foo attribute1;
public Foo getAttribute1() {
return attribute1;
}
public void setAttribute1(Foo attribute1) {
this.attribute1 = attribute1;
}
}
Demo
Below is some demo code you can use to prove that everything works.
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Tag.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Tag tag = new Tag();
tag.setAttribute1(Tag.Foo.A);
System.out.println(tag.getAttribute1());
marshaller.marshal(tag, System.out);
tag.setAttribute1(Tag.Foo.B);
System.out.println(tag.getAttribute1());
marshaller.marshal(tag, System.out);
}
}
Output
Below is the output from running the demo code.
A
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tag/>
B
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tag attribute1="B"/>
Tag
You could leverage the fact that JAXB will not marshal null attribute values and add some logic to your properties and then use JAXB to map to the field.
import javax.xml.bind.annotation.*;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Tag {
private static Foo ATTRIBUTE1_DEFAULT = Foo.A;
enum Foo {A, B};
#XmlAttribute
private Foo attribute1;
public Foo getAttribute1() {
if(null == attribute1) {
return ATTRIBUTE1_DEFAULT;
}
return attribute1;
}
public void setAttribute1(Foo attribute1) {
if(ATTRIBUTE1_DEFAULT == attribute1) {
this.attribute1 = null;
} else {
this.attribute1 = attribute1;
}
}
}
Demo
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Tag.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Tag tag = new Tag();
tag.setAttribute1(Tag.Foo.A);
System.out.println(tag.getAttribute1());
marshaller.marshal(tag, System.out);
tag.setAttribute1(Tag.Foo.B);
System.out.println(tag.getAttribute1());
marshaller.marshal(tag, System.out);
}
}
Output
A
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tag/>
B
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tag attribute1="B"/>
AFAIK, this is not possible directly using JAXB annotations. But using required=true like this,
#XmlAttribute(required=true name="attribute1")
private String enumExample;
we can do a workaround. Before calling the setter on that attribute, we can check if the value that will be set is the default value. If yes, we can pass null to that setter and that attribute will not be visible after marshalling.
#XmlRootElement
public class Dekkey {
String keyVal;
String kek1;
public String getKek1() {
return kek1;
}
#XmlElement
public void setKek1(String kek1) {
this.kek1 = kek1;
}
public String getKeyval() {
return keyVal;
}
#XmlAttribute
public void setKeyval(String inpKey) {
this.keyVal = inpKey;
}
}
This is my code snippet, where I want to insert a sub-element called userkey to the sub-element kek1. How can I do that?
How to insert attribute value for those sub-elements? I have another class called MarshDemo in which an object of Dekkey is created and then setkeyVal() function is called by passing value to the function.
The output looks like this:
<Dekkey keyVal="xer">
<kek1 keyVal="biv">
<userkey keyVal="wed">
</userkey>
</kek1>
</Dekkey>
I have omitted the getters and setters for brevity, this is what your should look like.
#XmlRootElement
public class Dekkey {
#XmlAttribute
String keyVal;
Kek1 kek1;
}
#XmlElement(name="kek1")
public class Kek1 {
#XmlAttribute
String keyVal;
UserKey userkey;
}
#XmlElement(name="userkey")
public class UserKey {
#XmlAttribute
String keyVal;
}
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Dekkey
Below is how you could map your use case using MOXy's #XmlPath extension (see: http://blog.bdoughan.com/2010/07/xpath-based-mapping.html).
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement(name="Dekkey")
#XmlAccessorType(XmlAccessType.FIELD)
public class Dekkey {
#XmlAttribute
String keyVal;
#XmlPath("kek1/#keyVal")
String kek1;
#XmlPath("kek1/userkey/#keyVal")
String userKey;
}
jaxb.properties
To specify MOXy as your JAXB (JSR-222) provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Dekkey.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum16248263/input.xml");
Dekkey dekkey = (Dekkey) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(dekkey, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8"?>
<Dekkey keyVal="xer">
<kek1 keyVal="biv">
<userkey keyVal="wed"/>
</kek1>
</Dekkey>
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.