Ignore root element in JAXB
I am trying to parse an XML file using JAXB. My problem is I need to skip the root node. If I remove it from the xml file, I get what I need, otherwise - I get an empty object.
I'll give a simplified xml and my code (it behaves the same):
XML:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<!-- <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Office.xsd">
-->
<Office>
<Employees>
<Employee>
<name>George</name>
<rank>3</rank>
</Employee>
<Employee>
<name>Michael</name>
<rank>5</rank>
</Employee>
<Employee>
<name>Jeff</name>
<rank>1</rank>
</Employee>
<Employee>
<name>Todd</name>
<rank>7</rank>
</Employee>
<Employee>
<name>Jessica</name>
<rank>5</rank>
</Employee>
</Employees>
</Office>
</Root>
Office class:
import javax.xml.bind.annotation.*;
import java.util.Vector;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Office {
@XmlElementWrapper(name = "Employees")
@XmlElement(name = "Employee")
private Vector<Employee> employees;
}
Employee class:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
@XmlElement(name="name")
private String name;
@XmlElement(name="rank")
private int rank;
public void promote() {
rank++;
}
}
Driver:
import javax.xml.stream.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Marshaller;
import java.io.FileReader;
public class Driver {
public static void main (String[] args) {
parseOffice();
}
public static void parseOffice() {
try {
XMLInputFactory f = XMLInputFactory.newInstance();
XMLStreamReader reader = f.createXMLStreamReader(new FileReader("Office.xml"));
JAXBContext context = JAXBContext.newInstance(Office.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Office office = unmarshaller.unmarshal(reader, Office.class).getValue();
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(office, System.out);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
+3
source to share
3 answers
Just add the root class to the hierarchy. And get the Office class from the Root class.
Root class: -
import javax.xml.bind.annotation.*;
import java.util.Vector;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElement(name = "Office")
private Office office;
}
Office class
import javax.xml.bind.annotation.*;
import java.util.Vector;
@XmlAccessorType(XmlAccessType.FIELD)
public class Office {
@XmlElementWrapper(name = "Employees")
@XmlElement(name = "Employee")
private Vector<Employee> employees;
}
Change analysis method: -
JAXBContext context = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Root root = unmarshaller.unmarshal(reader, Root.class).getValue();
Office office = root.getOffice();
+1
source to share