- Home
- Binder
- DatatypeConverter
- JAXB
- JAXBContext
- JAXBElement
- JAXBElement.GlobalScope
- JAXBIntrospector
- Marshaller.Listener
- SchemaOutputResolver
- Unmarshaller.Listener
- Javax.xml.bind.util classes
- JAXBResult
- JAXBSource
- ValidationEventCollector
- Javax.xml.parsers classes
- DocumentBuilder
- DocumentBuilderFactory
- SAXParser
- SAXParserFactory
- Javax.xml.soap classes
- AttachmentPart
- MessageFactory
- MimeHeader
- MimeHeaders
- SAAJMetaFactory
- SOAPConnection
- SOAPConnectionFactory
- SOAPFactory
- SOAPMessage
- SOAPPart
- Javax.xml.validation classes
- Schema
- SchemaFactory
- TypeInfoProvider
- Validator
- ValidatorHandler
- Javax.xml.xpath classes
- XPathConstants
- XPathFactory
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
Javax.xml.bind.JAXBElement Class
Introduction
The javax.xml.bind.JAXBElement class is the JAXB representation of an Xml Element.This class represents information about an Xml Element from both the element declaration within a schema and the element instance value within an xml document with the following properties −
element's xml tag name
value represents the element instance's atttribute(s) and content model
element declaration's declaredType (xs:element @type attribute)
scope of element declaration
boolean nil property. (element instance's xsi:nil attribute)
Class declaration
Following is the declaration for javax.xml.bind.JAXBElement class −
public class JAXBElement<T>
extends Object
implements Serializable
Field
Following are the fields for javax.xml.bind.JAXBElement class −
protected Class<T> declaredType − This is the Java datatype binding for xml element declaration's type
protected QName name − This is the xml element tag name.
protected boolean nil − This is true if the xml element instance has xsi:nil="true".
protected Class scope − This is the scope of xml element declaration representing this xml element instance.
protected T value − This is the xml element value.
Class constructors
| S.N. | Constructor & Description |
|---|---|
| 1 |
JAXBElement(QName name, Class<T> declaredType, Class scope, T value)
This constructs an xml element instance. |
| 2 |
JAXBElement(QName name, Class<T> declaredType, T value)
This constructs an xml element instance. |
Class methods
| S.N. | Method & Description |
|---|---|
| 1 |
Class<T>getDeclaredType()
This method returns the Java binding of the xml element declaration's type attribute. |
| 2 |
QName getName()
This method returns the xml element tag name. |
| 3 |
Class getScope()
This method returns scope of xml element declaration. |
| 4 |
T getValue()
This method returns the content model and attribute values for this element. |
| 5 |
boolean isGlobalScope()
This method returns true iff this xml element declaration is global. |
| 6 |
boolean isNil()
This method returns true iff this element instance content model is nil. |
| 7 |
boolean isTypeSubstituted()
This method returns true iff this xml element instance's value has a different type than xml element declaration's declared type. |
| 8 |
void setNil(boolean value)
This method sets whether this element has nil content. |
| 9 |
void setValue(T t)
This method sets the content model and attributes of this xml element. |
Methods inherited
This class inherits methods from the following classes −
javax.xml.Object
Example
The following example shows the usage of java.xml.bind.JAXBElement class. To proceed, consider the following Student class which will be used to have objects for marshalling and unmarshalling purposes −
package com.tutorialspoint;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Student{
String name;
int age;
int id;
public String getName(){
return name;
}
@XmlElement
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
@XmlElement
public void setAge(int age){
this.age = age;
}
public int getId(){
return id;
}
@XmlAttribute
public void setId(int id){
this.id = id;
}
}
Now let us create main class which will be used to marshal ie. convert Student object to XML. Here we will create a JAXBElement of a Student object and then update StringWriter object using JAXBContext. This example marshals the Student object and prints the XML.
package com.tutorialspoint;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
public class JAXBElementDemo {
public static void main(String[] args) {
//Create a student object
Student student = new Student();
//fill details of the student
student.setName("Robert");
student.setId(1);
student.setAge(12);
try {
//create JAXBElement of type Student
//Pass it the student object
JAXBElement<Student> jaxbElement = new JAXBElement(
new QName(Student.class.getSimpleName()), Student.class, student);
//Create a String writer object which will be
//used to write jaxbElment XML to string
StringWriter writer = new StringWriter();
// create JAXBContext which will be used to update writer
JAXBContext context = JAXBContext.newInstance(Student.class);
// marshall or convert jaxbElement containing student to xml format
context.createMarshaller().marshal(jaxbElement, writer);
//print XML string representation of Student object
System.out.println( writer.toString() );
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Student id="1"> <age>12</age> <name>Robert</name> </Student>