JAXB中的解组错误 [英] Unmarshal error in JAXB

查看:162
本文介绍了JAXB中的解组错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是PersonType类的生成代码。

  package demo; 

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name =PersonType,propOrder = {
name,
address
})
公共类PersonType {

@XmlElement(name =Name,required = true)
protected String name;
@XmlElement(name =Address,required = true)
protected List< AddressType>地址;

public String getName(){
return name;
}


public void setName(String value){
this.name = value;
}


公开列表< AddressType> getAddress(){
if(address == null){
address = new ArrayList< AddressType>();
}
返回this.address;
}

}

这是生成的代码 AddressTypeclass。

  package demo; 

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name =AddressType,propOrder = {
number,
street
})
公共类AddressType {

@XmlElement(name =Number)
@XmlSchemaType(name =unsignedInt)
protected long number;
@XmlElement(name =Street,required = true)
protected String street;


public long getNumber(){
返回号码;
}


public void setNumber(long value){
this.number = value;
}


public String getStreet(){
return street;
}

public void setStreet(String value){
this.street = value;
}

}

这是我想要的xml文件unmarshal

 < Person xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance 
xsi:noNamespaceSchemaLocation =C:\ JAXB Demo\demo.xsd>
< Name> Sharon Krisher< / Name>
<地址>
< Street> Iben Gevirol< / Street>
< Number> 57< / Number>
< /地址>
<地址>
< Street> Moshe Sharet< / Street>
< Number> 89< / Number>
< /地址>
< / Person>

代码foe unarchall

  JAXBContext context = JAXBContext.newInstance(PersonType.class); 
Unmarshaller unmarshaller = context.createUnmarshaller();
//unmarshaller.setValidating(true);
PersonType person =(PersonType)unmarshaller.unmarshal(new File(C:\\Users\\sithi\\workspace \\TestJAXB \\src\\ data .xml));

此代码给出了一个例外:

 线程main中的异常javax.xml.bind.UnmarshalException:意外元素(uri:,local:PersonType)。预期元素是(无)


解决方案

有两种方法可以将类与根元素相关联。第一个是类上的 @XmlRootElement ,第二个是用<$ c注释的类上的 @XmlElementDecl 注释$ c> @XmlRegistry (当从XML生成模型时,这个类被称为 ObjectFactory



创建 JAXBContext



生成模型时从XML Schema中你应该在包名上创建 JAXBContext

  JAXBContext jc = JAXBContext.newInstance(demo); 

或生成 ObjectFactory class:

  JAXBContext jc = JAXBContext.newInstance(demo.ObjectFactory.class); 

这将确保处理 ObjectFactory 类。 / p>



@XmlRootElement @XmlElementDecl



如果没有 @XmlRootElement @XmlElementDecl 将一个类与文档的根元素相关联,那么你将需要使用一个带有类参数的unmarshal方法。

  PersonType pt = unmarshaller.unmarshal(xml,PersonType.class)。getValue(); 


This is the generated code for "PersonType" class.

package demo;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PersonType", propOrder = {
"name",
"address"
})
public class PersonType {

@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Address", required = true)
protected List<AddressType> address;

public String getName() {
    return name;
}


public void setName(String value) {
    this.name = value;
}


public List<AddressType> getAddress() {
    if (address == null) {
        address = new ArrayList<AddressType>();
    }
    return this.address;
}

}

This is the generated code for "AddressType" class.

    package demo;

 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlSchemaType;
 import javax.xml.bind.annotation.XmlType;

 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "AddressType", propOrder = {
"number",
"street"
 })
public class AddressType {

@XmlElement(name = "Number")
@XmlSchemaType(name = "unsignedInt")
protected long number;
@XmlElement(name = "Street", required = true)
protected String street;


public long getNumber() {
    return number;
}


public void setNumber(long value) {
    this.number = value;
}


public String getStreet() {
    return street;
}

public void setStreet(String value) {
    this.street = value;
}

}

This is the xml file I want to unmarshal

<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\JAXB Demo\demo.xsd">
  <Name>Sharon Krisher</Name>
  <Address>
    <Street>Iben Gevirol</Street>
    <Number>57</Number>
  </Address>
<Address>
  <Street>Moshe Sharet</Street>
   <Number>89</Number>
  </Address>
</Person>

Code foe unarchall

JAXBContext context = JAXBContext.newInstance(PersonType.class);
    Unmarshaller unmarshaller =context.createUnmarshaller();
    //unmarshaller.setValidating(true);
    PersonType person =(PersonType) unmarshaller.unmarshal(new File("C:\\Users\\sithi\\workspace\\TestJAXB\\src\\data.xml") );

This code gives an exception:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"PersonType"). Expected elements are (none)

解决方案

There are two ways to associate a class with a root element. The first is @XmlRootElement on the class, the second is an @XmlElementDecl annotation on a class annotated with @XmlRegistry (when the model is generated from an XML a Schema this class is called ObjectFactory.

Creating the JAXBContext

When you generate a model from XML Schema you should create the JAXBContext on the package name:

JAXBContext jc = JAXBContext.newInstance("demo");

Or generated ObjectFactory class:

JAXBContext jc = JAXBContext.newInstance(demo.ObjectFactory.class);

This will make sure the ObjectFactory class is processed.

No @XmlRootElement or @XmlElementDecl?

If there isn't an @XmlRootElement or @XmlElementDecl associating a class with the root element of your document then you will need to use one of the unmarshal methods that take a class parameter.

PersonType pt = unmarshaller.unmarshal(xml, PersonType.class).getValue();

这篇关于JAXB中的解组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆