忽略JAXB中的根元素

时间:2014-12-13 18:52:24

标签: java xml jaxb

我尝试使用JAXB解析xml文件。 我的问题是我需要跳过根节点,如果我从xml文件中删除它,我得到我需要的东西,否则 - 我得到一个空对象。

我会给出一个简化的xml和我的代码(它的行为方式相同):

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>

办公室课程:

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;

}

员工类:

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++;
   }

}

司机:

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 个答案:

答案 0 :(得分:4)

为什么不创建通用的根元素?

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

将其添加到您的上下文并解组。您应该JAXBElement<Office>作为content

答案 1 :(得分:4)

您可以使用StAX XMLStreamReader解析XML,然后将其推进到要解组的元素,然后解组它。

我发布了一个完整的示例,可以帮助解决下面链接的相关问题:

答案 2 :(得分:1)

只需在层次结构中添加根类。从Root类获取Office类。

根类: -

import javax.xml.bind.annotation.*;
import java.util.Vector;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {


 @XmlElement(name = "Office")
 private Office office; 
 }

办公室课程

 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;

  }

解析方法更改: -

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