3

当我使用 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>

验证事件处理程序类

public class MyValidationEventHandler implements ValidationEventHandler {

public boolean handleEvent(ValidationEvent event) {
    System.out.println("\nEVENT");
    switch (event.getSeverity()) {
    case 0:
        System.out.println("SEVERITY:  WARNING");
        break;
    case 1:
        System.out.println("SEVERITY:  ERROR");
        break;
    case 2:
        System.out.println("SEVERITY:  FATAL_ERROR");
        break;
    }
    System.out.println("MESSAGE:  " + event.getMessage());
    System.out.println("LINKED EXCEPTION:  " + event.getLinkedException());
    System.out.println("LOCATOR");
    System.out.println("    LINE NUMBER:  " + event.getLocator().getLineNumber());
    System.out.println("    COLUMN NUMBER:  " + event.getLocator().getColumnNumber());
    System.out.println("    OFFSET:  " + event.getLocator().getOffset());
    System.out.println("    OBJECT:  " + event.getLocator().getObject());
    System.out.println("    NODE:  " + event.getLocator().getNode());
    System.out.println("    URL:  " + event.getLocator().getURL());
    return true;
}

}

输出:

事件严重性:致命错误消息:cvc-complex-type.2.4.a:从元素“case_manager1”开始发现无效内容。需要“{patent_status, case_manager}”之一。链接异常:org.xml.sax.SAXParseException;systemId:文件:/D:/AHWeb2.0.3/test3.xml;行号:24;列号:55;cvc-complex-type.2.4.a:从元素“case_manager1”开始发现无效内容。需要“{patent_status, case_manager}”之一。定位器行号:24 列号:55 偏移量:-1 对象:null 节点:null URL:file:/D:/AHWeb2.0.3/test3.xml

事件严重性:错误消息:意外元素(uri:“”,本地:“case_manager1”)。预期元素是 <{}tags>、<{}title>、<{}patent_number>、<{}brief_description>、<{}status>、<{}full_description>、<{}categories>、<{}case_manager> ,<{}patent_status> 链接异常:空定位器行号:24 列号:55 偏移量:-1 对象:空节点:空 URL:文件:/D:/AHWeb2.0.3/test3.xml

事件严重性:FATAL_ERROR MESSAGE:元素类型“case_manager1”必须由匹配的结束标记“”终止。链接异常:org.xml.sax.SAXParseException;systemId:文件:/D:/AHWeb2.0.3/test3.xml;行号:25;列号:5;元素类型“case_manager1”必须由匹配的结束标记“”终止。定位器行号:25 列号:5 偏移量:-1 对象:空 节点:空 URL:文件:/D:/AHWeb2.0.3/test3.xml 文件:/D:/AHWeb2.0.3/test3.xml 无效javax.xml.bind.UnmarshalException - 带有链接异常:[org.xml.sax.SAXParseException; systemId:文件:/D:/AHWeb2.0.3/test3.xml;行号:25;列号:5;元素类型“case_manager1”必须由 javax.xml.bind.helpers 中的匹配结束标记“”.] 终止。AbstractUnmarshallerImpl.createUnmarshalException(未知来源)在 com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(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(UnmarshallerImpl.unmarshal) 在 javax .xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(未知来源)在 com.test.innovation.validation.InnovationUnmarshalDemo.main(InnovationUnmarshalDemo.java:34) 引起:org.xml.sax.SAXParseException; systemId:文件:/D:/AHWeb2.0.3/test3.xml;行号:25;列号:5;元素类型“case_manager1”必须由匹配的结束标签“终止

谢谢。

4

2 回答 2

3

我想自定义验证器会帮助你:

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()); 
于 2013-06-20T14:02:56.267 回答
2

好的,所以问题出在这里:

/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 在语法上不正确,因此这种模式验证机制无法继续传递严重错误。

于 2013-06-12T11:56:36.910 回答