XML
Juneau supports converting arbitrary POJOs to and from XML using ultra-efficient serializers and parsers.
The XML serializer converts POJOs directly to XML without the need for intermediate DOM objects. Likewise, the XML parser uses a STaX parser and creates POJOs directly without intermediate DOM objects.
Unlike frameworks such as JAXB, Juneau does not require POJO classes to be annotated to produce and consume XML. However, several XML annotations are provided for handling namespaces and fine-tuning the format of the XML produced.
The following example shows XML for a typical bean:
Sample Beans
@Marshalled(typeName="person")
public class Person {
// Bean properties
public String name;
@MarshalledProp(calendarFormat=CalendarFormat.ISO_INSTANT) public Calendar birthDate;
public List addresses;
// Getters/setters omitted
}
@Marshalled(typeName="address")
public class Address {
// Bean properties
public String street, city;
public StateEnum state;
public int zip;
public boolean isCurrent;
// Getters/setters omitted
}
Sample Code
Person person = new Person()
.name("John Smith")
.birthDate("1946-08-12T00:00:00Z")
.addresses(
new Address()
.street("100 Main Street")
.city("Anywhereville")
.state(NY)
.zip(12345)
.isCurrent(true)
);
Normal XML:
<person>
<name>John Smith</name>
<birthDate>1946-08-12T04:00:00Z</birthDate>
<addresses>
<address>
<street>100 Main Street</street>
<city>Anywhereville</city>
<state>NY</state>
<zip>12345</zip>
<isCurrent>true</isCurrent>
</address>
</addresses>
</person>
Juneau produces JSON-equivalent XML, meaning any valid JSON document can be losslessly converted into an XML equivalent. In fact, all of the Juneau serializers and parsers are built upon this JSON-equivalence.
@Xml(childName) annotation
The @Xml(childName) annotation can be used to specify the name of XML child elements for bean properties of type collection or array.
| Data type | JSON example | Without annotation | With annotation |
|---|---|---|---|
| | | |
| | | |
In This Section
📄️ 2.2.26.1. XML Methodology
This page describes how the different POJO datatypes are represented in XML.
📄️ 2.2.26.2. XML Serializers
The XmlSerializer class is used to serialize POJOs into XML.
📄️ 2.2.26.3. XML Parsers
The XmlParser class is used to parse XML into POJOs.
📄️ 2.2.26.4. @Marshalled(typeName) Annotation
The @Marshalled(typeName) annotation can be used to override the Juneau default name on bean elements.
🗃️ 2.2.26.5. @Xml(format) Annotation
5 items
📄️ 2.2.26.6. Namespaces
Let's go back to the example of our original Person bean class but add some namespace annotations:
📄️ 2.2.26.7. SOAP/XML Support
Juneau supports serializing POJOs to SOAP-wrapped XML using the