小编use*_*591的帖子

如何使用jaxb创建java对象

我正在尝试从xml文件创建java对象.我正在使用jaxb(解组)来​​创建java对象.我收到错误javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2001/XMLSchema", local:"schema"). Expected elements are

我做了一些谷歌并发现,我们需要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(); 
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

但我得到了fol;由于错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.w3.org/2001/XMLSchema", local:"schema"). Expected elements are …
Run Code Online (Sandbox Code Playgroud)

java jaxb

5
推荐指数
1
解决办法
7049
查看次数

标签 统计

java ×1

jaxb ×1