Java 解组意外的元素错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18744374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unmarshalling unexpected element error
提问by Fendrix
I have a very similar problem to this topic unexpected element error while unmarshalling, but still I am not able to solve the exception. As the namespace in XML and package-info is exactly the same... or is it because of the xsd file ?
我在解组时遇到了与本主题意外元素错误非常相似的问题,但我仍然无法解决异常。由于 XML 和 package-info 中的命名空间完全相同......还是因为 xsd 文件?
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.example.org/Uni", local:"Uni"). Expected elements are <{http://www.example.org/Uni}uni>
XML:
XML:
<?xml version="1.0" encoding="UTF-8"?>
<tns:Uni xmlns:tns="http://www.example.org/Uni" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/Uni Uni.xsd ">
<tns:Semester>
<tns:nr>1</tns:nr>
<tns:datum>01.02.2012</tns:datum>
</tns:Semester>
<tns:Semester>
<tns:nr>2</tns:nr>
<tns:datum>01.02.2012</tns:datum>
</tns:Semester>
</tns:Uni>
XSD
XSD
<complexType name="Uni">
<choice>
<element name="Semester" type="tns:Semester" maxOccurs="unbounded"></element>
</choice>
</complexType>
<complexType name="Semester">
<sequence>
<element name="nr" type="int"></element>
<element name="datum" type="string"></element>
</sequence>
</complexType>
package-info
包信息
@javax.xml.bind.annotation.XmlSchema(
namespace = "http://www.example.org/Uni", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package parserTest;
Uni.java
统一Java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Uni
{
@XmlElement(name = "Semester")
protected List<Semester> semester;
public List<Semester> getSemester()
{
if (this.semester == null)
{
this.semester = new ArrayList<Semester>();
}
return this.semester;
}
}
Unmarshalling
解组
public static void main(String[] args) throws JAXBException
{
ObjectFactory factoryForElementObjects = new ObjectFactory();
List<Semester> semesterL = new ArrayList<Semester>();
Uni uni = new Uni();
uni.semester = semesterL;
JAXBContext context = JAXBContext.newInstance(Uni.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Uni un = (Uni) unmarshaller.unmarshal(new File("src/main/resources/Uni.xml"));
List<Semester> semesterA = un.semester;
System.out.println(semesterA.get(0).nr);
}
采纳答案by Jean Waghetti
The problem is that unmarshaller is expecting element uni, but it finds Uni.
问题是 unmarshaller 正在期待 element uni,但它发现Uni.
Setting the XML element name for the Uni class should do the trick:
为 Uni 类设置 XML 元素名称应该可以解决问题:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Uni")
public class Uni
{
@XmlElement(name = "Semester")
protected List<Semester> semester;
public List<Semester> getSemester()
{
if (this.semester == null)
{
this.semester = new ArrayList<Semester>();
}
return this.semester;
}
}

