XSD验证,返回所有空白所需标记和无效数据类型标记的列表

时间:2017-08-11 11:29:47

标签: java xml xsd xsd-validation

在我的代码中,我需要银行所需标签的名称和无效的数据类型标签。

我使用以下代码。那是返回xml的行号或列号但我需要标签名称。

代码:

    public static void validateImportLCXMLSchema(String xsdPath, String xmlPath){
    try {
        SchemaFactory factory=
                SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new File(xsdPath));
        Validator validator = schema.newValidator();
        final List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
        validator.setErrorHandler(new ErrorHandler(){
            @Override
            public void warning(SAXParseException exception) throws SAXException{
                exceptions.add(exception);
            }
            @Override
            public void fatalError(SAXParseException exception) throws SAXException{
                exceptions.add(exception);
            }
            @Override
            public void error(SAXParseException exception) throws SAXException{
                exceptions.add(exception);
            }
        });
        validator.validate(new StreamSource(new File(xmlPath)));
        for(SAXParseException e:exceptions){
            System.out.println(e.getMessage());
        }
    } catch (Exception e) {
    }
}

输出:

cvc-type.3.1.3: The value '' of element 'InfoType' is not valid.
cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type '#AnonType_corpReferenceHeader'.
cvc-type.3.1.3: The value '' of element 'corpReference' is not valid.

我需要:

InfoType -> Balnk  
corpReference-> Blank  
ABC -> Invalid data type 

XML:

<importLCDetails>
    <importLCHeader>
        <InfoType></InfoType>
        <formOfDocumentaryCredit></formOfDocumentaryCredit>
        <corpReference></corpReference>
        <Reference></Reference>
        <lcCorporateDetails>
            <address>HNo 43 chd</address>
            <city>pkl</city>
            <postalCode>123456</postalCode>
            <country>India</country>
            <contact>0172265955</contact>
            <corpReference>Structure0107LC</corpReference>
            <clientName>corp pqr</clientName>
        </lcCorporateDetails>           
    </importLCHeader>
</importLCDetails>

XSD:

<xsd:complexType name="importLCHeader">
    <xsd:sequence>
        <xsd:element name="InfoType">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="import" />
                    <xsd:enumeration value="importAmendment" />
                    <xsd:enumeration value="importUtilisation" />
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:element>
        <xsd:element minOccurs="0" name="formOfDocumentaryCredit"
            type="xsd:string" />
        <xsd:element minOccurs="0" name="formOfGuarantee" type="xsd:string" />
        <xsd:element name="corpReference">
            <xsd:simpleType>
                <xsd:restriction base="xsd:string">
                    <xsd:minLength value="1" />
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:element>
        <xsd:element minOccurs="1" name="Reference" type="xsd:string" />
    </xsd:sequence>
</xsd:complexType>

还有其他方法可以实现这一目标吗?

0 个答案:

没有答案