简体   繁体   English

忽略JAXB中的根元素

[英]ignore root element in JAXB

I'm trying to parse an xml file using JAXB. 我正在尝试使用JAXB解析xml文件。 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. 我的问题是我需要跳过根节点,如果我从xml文件中删除它我得到了我需要的东西,否则 - 我得到一个空对象。

I'll give a simplified xml and my code (It behaves the same way): 我将给出一个简化的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>

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

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 . 您应该获得JAXBElement<Office>作为content

You can parse the XML with a StAX XMLStreamReader , then advance it to the element you want to unmarshal, and then unmarshal it. 您可以使用StAX XMLStreamReader解析XML,然后将其推进到要解组的元素,然后解组它。

I posted a full example that should help on the related question linked below: 我发布了一个完整的示例,可以帮助解决下面链接的相关问题:

Simply add the root class in hierarcy. 只需在hierarcy中添加根类。 And get Office class from Root class. 从Root类获取Office类。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM