如何使用jaxb创建java对象
我正在尝试从 xml 文件创建 java 对象。我正在使用 jaxb(unmarshalling) 创建 java 对象。我收到错误 javax.xml.bind.UnmarshalException: 意外元素 (uri:"http://www.w3.org/2001/XMLSchema",本地: “模式”)。预期的元素是
我做了一些谷歌,发现我们需要 xsd 文件来做到这一点...所以我使用 apache inst2xsd 工具将其转换为 xsd。我正在使用以下java代码:
import java.io.FileNotFoundException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="report")
public class Report
{
public static void main(String [] args) throws FileNotFoundException
{
try
{
JAXBContext jc = JAXBContext.newInstance(new Class[] {com.bcbsks.testjb.Report.class});
Unmarshaller um = jc.createUnmarshaller();
Report myJAXBObject = (Report)um.unmarshal(new java.io.FileInputStream("report.xsd"));
}
catch( UnmarshalException ue )
{
ue.printStackTrace();
}
catch( JAXBException je )
{
je.printStackTrace();
}
}
}
但我收到以下错误:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2001/XMLSchema", local:"schema"). Expected elements are (none)
你能告诉我我做错了什么吗?
非常感谢任何帮助。
I am trying to create java objects from xml file. I am using jaxb(unmarshalling) to create java objects.I am getting errors javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2001/XMLSchema", local:"schema"). Expected elements are
I did some google and found out that, we need xsd file to do that... so I converted it to xsd using apache inst2xsd tool. I am using following java code:
import java.io.FileNotFoundException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.UnmarshalException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="report")
public class Report
{
public static void main(String [] args) throws FileNotFoundException
{
try
{
JAXBContext jc = JAXBContext.newInstance(new Class[] {com.bcbsks.testjb.Report.class});
Unmarshaller um = jc.createUnmarshaller();
Report myJAXBObject = (Report)um.unmarshal(new java.io.FileInputStream("report.xsd"));
}
catch( UnmarshalException ue )
{
ue.printStackTrace();
}
catch( JAXBException je )
{
je.printStackTrace();
}
}
}
But I am getting fol;owing error:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2001/XMLSchema", local:"schema"). Expected elements are (none)
Can you please tell me whats wrong I am doing?
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你错过了几个步骤。您没有发布 report.xsd 是什么,也没有发布示例 xml,所以我将进行一些猜测。
对于初学者来说,您正在尝试解组 xsd 而不是 xml,这本身就是问题的根源。话虽这么说,您的 Report.java 类看起来没有正确生成,因此即使您尝试针对 xml 文件,您的解组也不太可能起作用。
如果您有正确创建的 XSD 文件,那么您应该做的第一件事是使用 xjc 创建 JaxB POJO。
xjc
随 java 一起安装,您可以使用它从 xsd 创建带注释的 java 类。它还将创建 2 个附加文件 - ObjectFactory.java 和 package-info.java,供 JAXB 使用。 (您可以使用 -d 参数指定输出路径(有关开关的完整列表,请参阅 --help)生成这些文件后,您必须基于该包/文件创建 JAXBContext。
解组器生成一个 JAXBElement,您可以从中提取实际的报告类。
希望这有帮助。
I think you are missing a few steps. You didn't post what report.xsd is, nor a sample xml, so I'm going to take a few guesses.
For starters, you are trying to unmarshal the xsd and not xml, which is itself the root of the problem. That being said, your Report.java class does not look properly generated so it is unlikely that your unmarshalling would work even if you tried against your xml file.
If you have a properly created XSD file, the first thing you should do is create the JaxB POJOs using
xjc
.xjc
comes installed with java, and you use it to create annotated java classes from the xsd. It will also create 2 additional files - ObjectFactory.java and package-info.java which are used by JAXB. (You can specify the output path using the -d param (see --help for the full list of switches)Once you have those files generated, you have to create your JAXBContext based on that package/file.
The unmarshaller generates a JAXBElement, from which you can extract the actual report class.
Hope this helps.
您尝试解组的 bean 上没有任何属性。但更重要的是,您正在尝试从 XSD 本身反序列化您的对象。错误消息是一个很好的指示符:
unexpected element (uri:"http://www.w3.org/2001/XMLSchema", local:"schema")
JAXB 正在吐出此错误消息因为它正在尝试将 XSD 的元数据映射到您的 bean 的属性。当然,你的豆子实际上没有。错误消息的下一部分表明:
Expected elements are (none)
您需要正确定义 Java Bean(在其上添加一些属性!),并实际获取一个表示bean 的序列化版本。
There are no properties on the bean you are trying to unmarshal. But more importantly, you are trying to deserialize your object from the XSD itself. The error message is a good indicator here:
unexpected element (uri:"http://www.w3.org/2001/XMLSchema", local:"schema")
JAXB is spitting out this error message because it is attempting to map the XSD's metadata to properties of your bean. Which of course, your bean doesn't actually have any. The next part of the error message indicates as much:
Expected elements are (none)
You need to define your Java Bean properly (put some properties on it!), and actually get an XML file that represents the serialized version of your bean.