Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据xml模式验证xml文件_Java_Xml_Validation_Schema_Jaxb - Fatal编程技术网

Java 根据xml模式验证xml文件

Java 根据xml模式验证xml文件,java,xml,validation,schema,jaxb,Java,Xml,Validation,Schema,Jaxb,这是关于根据XML模式(例如:schemafile.xsd)验证XML文件(例如:marshalledfile.XML)。我们正在使用jaxb将java对象打包成xml文件 最好的方法是什么 有人能举一个简单的例子说明怎么做吗 谢谢你的帮助 谢谢, Alo您可以直接在封送拆收器中设置模式。首先,您需要创建一个模式实例(javax.xml.validation包): 既然已经有了模式,只需将封送拆收器中的属性设置为 验证生成的XML: MovieLibrary library = ...; //

这是关于根据XML模式(例如:schemafile.xsd)验证XML文件(例如:marshalledfile.XML)。我们正在使用jaxb将java对象打包成xml文件

  • 最好的方法是什么

  • 有人能举一个简单的例子说明怎么做吗

  • 谢谢你的帮助

    谢谢,
    Alo

    您可以直接在封送拆收器中设置模式。首先,您需要创建一个模式实例(javax.xml.validation包):

    既然已经有了模式,只需将封送拆收器中的属性设置为 验证生成的XML:

    MovieLibrary library = ...; // <-- your JAXB-annotated tree
    
    JAXBContext ctx = JAXBContext.newInstance(MovieLibrary.class);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.setSchema(schema);
    marshaller.marshal(new JAXBElement<MovieLibrary>(new QName("movieLibrary"), 
                                                        MovieLibrary.class, library),
                       new FileOutputStream("/tmp/library.xml"));
    
    MovieLibrary库=//
    
    MovieLibrary library = ...; // <-- your JAXB-annotated tree
    
    JAXBContext ctx = JAXBContext.newInstance(MovieLibrary.class);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.setSchema(schema);
    marshaller.marshal(new JAXBElement<MovieLibrary>(new QName("movieLibrary"), 
                                                        MovieLibrary.class, library),
                       new FileOutputStream("/tmp/library.xml"));