- Javax.xml.bind classes
- 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
Java.xml.bind.JAXBElement.setValue() Method Example
Description
The Java.xml.bind.JAXBElement.setValue() method sets the content model and attributes of this xml element.
Declaration
Following is the declaration for java.xml.bind.JAXBElement.setValue() method −
public void setValue(T t)
Parameters
content − object containing content model and attributes of a corresponding xml element
Return Value
NA
Exception
NA
Example
The following example shows the usage of java.xml.bind.JAXBElement.setValue() method. 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; } public String toString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Student ["); stringBuilder.append("ID = "); stringBuilder.append(id); stringBuilder.append(", Name = "); stringBuilder.append(name); stringBuilder.append(", Age = "); stringBuilder.append(age); stringBuilder.append("]"); return stringBuilder.toString(); } }
Now let us create main class which will show the usage of java.xml.bind.JAXBElement.setValue() method. Here we will create a JAXBElement of a Student object and then check content model.Then this example set the content model as the Student object and prints the result.
package com.tutorialspoint; import javax.xml.bind.JAXBElement; 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); //create JAXBElement of type Student //Pass it a null object JAXBElement<Student> jaxbElement = new JAXBElement( new QName(Student.class.getSimpleName()), Student.class, null); Student retrivedStudent; //check if content model not null if(!jaxbElement.isNil()){ //get the content values as Student object retrivedStudent = jaxbElement.getValue(); //print the result System.out.println("Student #1: "+retrivedStudent.toString()); } else { jaxbElement.setValue(student); } //get the content values as Student object retrivedStudent = jaxbElement.getValue(); //print the result System.out.println("Student #2:"+retrivedStudent.toString()); } }
Let us compile and run the above program, this will produce the following result −
Student #2:Student [ID = 1, Name = Robert, Age = 12]
To Continue Learning Please Login
Login with Google