Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
希望在使用JAXB验证XML时获得所有错误列表吗_Jaxb - Fatal编程技术网

希望在使用JAXB验证XML时获得所有错误列表吗

希望在使用JAXB验证XML时获得所有错误列表吗,jaxb,Jaxb,当我使用JAXB验证XML文件时,您能帮助我获得所有错误的列表吗 现在我已经实现了同样的代码。我已经使用对XML文件进行解组来实现代码 如果XML文件中有任何错误,那么当我在那时解组XML文件时,我得到了异常,但是在给出第一个异常之后它出现了,但是我想要该XML文件的所有错误的列表。在得到第一个错误后,不应出现此错误 Source xsdFileName = new StreamSource(new File("validate6.xsd")); Source xmlFileName =

当我使用JAXB验证XML文件时,您能帮助我获得所有错误的列表吗

现在我已经实现了同样的代码。我已经使用对XML文件进行解组来实现代码

如果XML文件中有任何错误,那么当我在那时解组XML文件时,我得到了异常,但是在给出第一个异常之后它出现了,但是我想要该XML文件的所有错误的列表。在得到第一个错误后,不应出现此错误

Source xsdFileName = new StreamSource(new File("validate6.xsd"));
    Source xmlFileName = new StreamSource(new File("test3.xml"));

    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(xsdFileName);

        JAXBContext jc = JAXBContext.newInstance(Innovations.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new MyValidationEventHandler());
        Innovations innovation = (Innovations) unmarshaller.unmarshal(xmlFileName);

        System.out.println("Total Size :: " + innovation.getInnovationList().size());
        System.out.println(xmlFileName.getSystemId() + " is valid");
    } catch (JAXBException e) {
        e.printStackTrace();
        System.out.println(xmlFileName.getSystemId() + " is NOT valid");
    } catch (SAXParseException e) {
        e.printStackTrace();
        System.out.println(xmlFileName.getSystemId() + " is NOT valid");
    } catch (SAXException e) {
        e.printStackTrace();
        System.out.println(xmlFileName.getSystemId() + " is NOT valid");
    }
XSD文件:

<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="innovations">
    <xs:complexType>
        <xs:sequence>
            <xs:element type="xs:string" name="organization" />
            <xs:element name="innovation" maxOccurs="unbounded"
                minOccurs="1">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="title" minOccurs="1">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="1" />
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="brief_description" minOccurs="1">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="1" />
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="categories" minOccurs="1">
                            <xs:complexType mixed="true">
                                <xs:sequence>
                                    <xs:element name="category" maxOccurs="unbounded"
                                        minOccurs="1">
                                        <xs:simpleType>
                                            <xs:restriction base="xs:string">
                                                <xs:minLength value="1" />
                                            </xs:restriction>
                                        </xs:simpleType>
                                    </xs:element>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                        <xs:element name="tags" minOccurs="1">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="tag" maxOccurs="unbounded"
                                        minOccurs="1">
                                        <xs:simpleType>
                                            <xs:restriction base="xs:string">
                                                <xs:minLength value="1" />
                                            </xs:restriction>
                                        </xs:simpleType>
                                    </xs:element>
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                        <xs:element name="full_description" minOccurs="1">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="1" />
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="patent_number" maxOccurs="unbounded" minOccurs="0">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="1" />
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="patent_status" maxOccurs="unbounded" minOccurs="0">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="1" />
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                        <xs:element name="case_manager" minOccurs="1">
                            <xs:complexType>
                                <xs:simpleContent>
                                    <xs:extension base="xs:string">
                                        <xs:attribute type="xs:string" name="first_name"
                                            use="optional" />
                                        <xs:attribute type="xs:string" name="last_name"
                                            use="optional" />
                                    </xs:extension>
                                </xs:simpleContent>
                            </xs:complexType>
                        </xs:element>
                        <xs:element name="status" minOccurs="1">
                            <xs:simpleType>
                                <xs:restriction base="xs:string">
                                    <xs:minLength value="1" />
                                </xs:restriction>
                            </xs:simpleType>
                        </xs:element>
                    </xs:sequence>
                    <xs:attribute type="xs:short" name="file_number" use="required" />
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
}

输出: 事件 严重性:致命错误 消息:cvc复杂类型。2.4.a:发现以元素“case_manager1”开头的无效内容。应为“{patent_status,case_manager}”之一。 链接异常:org.xml.sax.saxpasseeption;systemId:file:/D:/AHWeb2.0.3/test3.xml;行号:24;栏目号:55;cvc复杂类型.2.4.a:发现以元素“case_manager1”开头的无效内容。应为“{patent_status,case_manager}”之一。 定位器 电话号码:24 栏目号:55 偏移量:-1 对象:空 节点:空 URL:file:/D:/AHWeb2.0.3/test3.xml

事件 严重性:错误 消息:意外元素(uri:,本地:“case_manager1”)。预期的元素为,,,,,,,, 链接异常:null 定位器 电话号码:24 栏目号:55 偏移量:-1 对象:空 节点:空 URL:file:/D:/AHWeb2.0.3/test3.xml

事件 严重性:致命错误 消息:元素类型“case_manager1”必须由匹配的结束标记“”终止。 链接异常:org.xml.sax.saxpasseeption;systemId:file:/D:/AHWeb2.0.3/test3.xml;行号:25;列数:5;元素类型“case_manager1”必须由匹配的结束标记“”终止。 定位器 电话号码:25 列号:5 偏移量:-1 对象:空 节点:空 URL:file:/D:/AHWeb2.0.3/test3.xml 文件:/D:/AHWeb2.0.3/test3.xml无效 javax.xml.bind.UnmarshalException -除此之外: [org.xml.sax.SAXParseException;systemId:file:/D:/AHWeb2.0.3/test3.xml;行号:25;列号:5;元素类型“case\u manager1”必须由匹配的结束标记终止。“” 位于javax.xml.bind.helpers.AbstractUnmarshallerImpl.CreateUnmarshaleException(未知源) 位于com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshaleException(UnmarshallerImpl.java:514) 位于com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215) 位于com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184) 位于javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(未知源) 位于javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(未知源) 位于com.test.innovation.validation.innovationNMarshaldemo.main(innovationNMarshaldemo.java:34) 原因:org.xml.sax.saxpasseeption;systemId:file:/D:/AHWeb2.0.3/test3.xml;行号:25;列数:5;元素类型“case_manager1”必须由匹配的结束标记“”终止。 位于org.apache.xerces.parsers.AbstractSAXParser.parse(未知源) 位于org.apache.xerces.jaxp.SAXParserImpl$jaxpsaxpasser.parse(未知源代码) 位于com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211) ... 4更多


谢谢。

好的,问题在于:

/test3.xml is NOT valid javax.xml.bind.UnmarshalException - with linked exception: [org.xml.sax.SAXParseException; systemId: file:/D:/AHWeb2.0.3/test3.xml; lineNumber: 25; columnNumber: 5; The element type "case_manager1" must be terminated by the matching end-tag "".] at javax.xml.bind.helpers.AbstractUnmarshallerI mpl.createUnmarshalException(Unknown Source) at...
XML的第25行不正确-缺少结束标记

由于XML语法不正确,此架构验证机制无法继续。我想a将帮助您:

public class MyValidationEventHandler implements ValidationEventHandler{

    public boolean handleEvent(ValidationEvent ve) {            
        if (ve.getSeverity()==ve.FATAL_ERROR ||  
                ve .getSeverity()==ve.ERROR){
            ValidationEventLocator  locator = ve.getLocator();
            //print message from valdation event
            System.out.println("Message is " + ve.getMessage());
            //output line and column number
            System.out.println("Column is " + 
                    locator.getColumnNumber() + 
                    " at line number " + locator.getLineNumber());
        }
        return true;
    }

}
然后

setEventHandler(new MyValidationEventHandler()); 

运行此代码时,完整的控制台输出是什么?请查看输出!嘿,伙计,我知道。我想要的是我想要做一个实用程序,它提供XML的所有错误列表。现在使用这个工具,我只得到第一个错误。我要一份所有错误的清单获取?请参阅我的编辑。我认为您无法通过这种类型的错误,因为XML的语法不正确。+1-@NishithShah JamesB是正确的。当文档的格式不好时,JAXB实现无法做很多事情来恢复文档,您只会得到一个异常。
setEventHandler(new MyValidationEventHandler());