Michael
Michael

Reputation: 112

ignore root element in JAXB

I'm trying to parse an xml file using JAXB. My problem is that I need to skip the root node, If I delete 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 way):

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();
         }
      }
}

Upvotes: 3

Views: 12831

Answers (3)

bdoughan
bdoughan

Reputation: 149047

You can parse the XML with a StAX XMLStreamReader, then advance it to the element you want to unmarshal, and then unmarshal it.

I posted a full example that should help on the related question linked below:

Upvotes: 4

Panther
Panther

Reputation: 3339

Simply add the root class in hierarcy. And get Office class from 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 in parse method :-

       JAXBContext context = JAXBContext.newInstance(Root.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Root root = unmarshaller.unmarshal(reader, Root.class).getValue();
        Office office = root.getOffice();

Upvotes: 1

lexicore
lexicore

Reputation: 43709

Why don't you create a generic root element?

@XmlRootElement(name="Root" ...)
public class Root {
   @XmlAnyElement(lax=true)
   private Object content;
}

Add it to your context and unmarshal. You should get a JAXBElement<Office> as content.

Upvotes: 4

Related Questions