【问题标题】:Validate XML against XSD with SAXParser results in error使用 SAXParser 针对 XSD 验证 XML 会导致错误
【发布时间】:2013-01-04 10:13:49
【问题描述】:

我有一个 XML 文件和一个 XSD 文件,我想根据 XSD 验证 XML。

但我不断收到以下错误:

org.xml.sax.SAXParseException; schema_reference.4: Failed to read schema document '/connector/connector.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

我打印了canonical path 以确保我尝试使用正确的文件。但它不会工作。

XML:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <Content>
<commandLine>
<commandCode>A1</commandCode>
<marks><mark><code>mail</code><value>test@test.com</value></mark></marks>
<customerID>1</customerID>
<MessageType>2</MessageType>
</commandLine>
<Antwoordregel></Antwoordregel>
</Content>
</xs:schema>

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema id="Message" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="Content">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="commandLine" minOccurs="1" maxOccurs="1">
                    <xs:complexType>
                        <xs:sequence minOccurs="1" maxOccurs="1">
                            <xs:element name="commandCode" type="xs:string" minOccurs="1" maxOccurs="1"/>
                            <xs:element name="marks" minOccurs="0" maxOccurs="1">
                                <xs:complexType>
                                    <xs:sequence >
                                        <xs:element name="mark" minOccurs="1" maxOccurs="unbounded">
                                            <xs:complexType>
                                                <xs:sequence >                                                             
                                                    <xs:element name="code" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                                    <xs:element name="value" type="xs:string" minOccurs="1" maxOccurs="1"/>                                                                
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>   
                            <xs:element name="customerID" type="xs:string" minOccurs="0" maxOccurs="1"/>
                            <xs:element name="MessageType" type="xs:string" minOccurs="0" maxOccurs="1"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="Antwoordregel" minOccurs="1" maxOccurs="1">
                    <xs:complexType>
                        <xs:sequence minOccurs="0" maxOccurs="1">
                            <xs:element name="resultCode" type="xs:string" minOccurs="0" maxOccurs="1"/>
                            <xs:element name="statusInfo" minOccurs="0" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="transactionInfo" type="xs:string" minOccurs="1" maxOccurs="1"/>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

我用来验证的代码:

static boolean validateAgainstXSD(String xml){
        try{
            File xsd = new File("connector/connector.xsd");
            System.out.println(xsd.getCanonicalPath());
            SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new StreamSource("/connector/connector.xsd"));

            System.out.println(schema.toString());
            Validator validator  = schema.newValidator();
            validator.validate(new StreamSource(xml));
            return true;
        }catch(Exception exe){
            exe.printStackTrace();
        return false;
        }
    }

它总是返回 false。我尝试使用在线工具通过 XSD 验证 XML,可以在此处找到:www.utilities-online.info/xsdvalidation。这个验证器返回:

Not valid.
Error - Line 2, 122: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 122; cvc-elt.1: Cannot find the declaration of element 'xs:schema'.

我能做些什么来解决这个问题?

感谢您的帮助,

谢谢!

【问题讨论】:

    标签: java xml xsd validation


    【解决方案1】:

    从 XML 文件中删除第二行

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified" attributeFormDefault="unqualified">
    

    正确的 XML 是

    <?xml version="1.0" encoding="UTF-8"?>
    <Content>
        <commandLine>
            <commandCode>A1</commandCode>
            <marks><mark><code>mail</code><value>test@test.com</value></mark></marks>
            <customerID>1</customerID>
            <MessageType>2</MessageType>
        </commandLine>
        <Antwoordregel></Antwoordregel>
    </Content>
    

    解决方案:

    请检查文件夹结构并再次运行。

    源代码:

    package com.shashi.mpoole;
    
    
    import java.io.File;
    
    import javax.xml.XMLConstants;
    import javax.xml.transform.stream.StreamSource;
    import javax.xml.validation.Schema;
    import javax.xml.validation.SchemaFactory;
    import javax.xml.validation.Validator;
    
    public class XMLValid {
    
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
    
            if(validateAgainstXSD(new File("connector/connector.xml")))
            {
                System.out.println("Success");
            }
            else
            {
                System.out.println("Failure");
            }
        }
    
    
    
        static boolean validateAgainstXSD(File xml){
            try{
    
                SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = factory.newSchema(new StreamSource("connector/connector.xsd"));
    
                Validator validator  = schema.newValidator();
                validator.validate(new StreamSource(xml));
                return true;
            }catch(Exception exe){
                exe.printStackTrace();
            return false;
            }
        }
    
    }
    

    文件夹结构

    -----Project
    
        ------------- src
    
                      -------------XMLValid.java
    
        ------------- connector
    
                      -------------connector.xsd
    
                      -------------connector.xml 
    

    【讨论】:

    • 非常感谢!它现在使用该网站进行验证。但我仍然在 Java 中得到相同的 SAXParseException:org.xml.sax.SAXParseException; schema_reference.4: Failed to read schema document '/connector/connector.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not &lt;xsd:schema&gt;. 你知道是什么原因造成的吗?再次感谢您的帮助!
    • 再次感谢,尝试在我的代码中实现它。但我仍然无法让它工作。有一个MalformedURLException no protocol。所以我已将文件路径更改为 `file://connector/connector.xsd 但我仍然遇到同样的错误。
    • 你检查文件夹结构了吗
    • 是的,我将所有文件放在与您的帖子相同的结构中,我得到的确切例外是:java.net.MalformedURLException: no protocol: connector/connector.xsd
    • 我做了,我不明白了>.
    【解决方案2】:

    试试这个代码来验证你的 XSD, 这是避免异常的正确标准 (这是完整的证明代码)。

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema id="Message" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    
        <xs:element name="commandCode"  type="xs:string"/>
        <xs:element name="code"  type="xs:string"/>
        <xs:element name="value" type="xs:string" />
        <xs:element name="customerID" type="xs:string" />
        <xs:element name="MessageType" type="xs:string" />
        <xs:element name="resultCode" type="xs:string" />
        <xs:element name="transactionInfo" type="xs:string" />
    
    <xs:element name="mark" >
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="code" minOccurs="1" maxOccurs="1"/>
                <xs:element ref="value" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    
    <xs:element name="marks" >
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="mark" minOccurs="1" maxOccurs="unbounded" />
             <!--End element Mark -->
        </xs:sequence>
        </xs:complexType>
    </xs:element> 
    <xs:element name="commandLine">
        <xs:complexType>
            <xs:sequence minOccurs="1" maxOccurs="1">
                <xs:element ref="commandCode"  minOccurs="1" maxOccurs="1"/>
                <xs:element ref="marks" minOccurs="0" maxOccurs="1"/>
                <xs:element ref="customerID" minOccurs="0" maxOccurs="1"/>
                <xs:element ref="MessageType"  minOccurs="0" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="statusInfo">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="transactionInfo" minOccurs="1" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    
    <xs:element name="Antwoordregel" >
        <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="1">
                <xs:element ref="resultCode" minOccurs="0" maxOccurs="1"/>
                <xs:element ref="statusInfo" minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>  
        </xs:complexType>
    </xs:element>
    
    <!-- First Content Document Element -->
    <xs:element name="Content">
            <xs:complexType>
                <xs:sequence>
                    <xs:element ref="commandLine" minOccurs="1" maxOccurs="1" />
                    <xs:element ref="Antwoordregel" minOccurs="1" maxOccurs="1"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    
    </xs:schema>
    

    【讨论】:

      【解决方案3】:

      我能够通过更改具有“xs:”命名空间前缀的 XSD 并将所有实例搜索/替换为“xsd:”来解决此类问题>" 代替(以及 xmlns:xsd= 定义)。然后我将这个 XSD 托管在一个内部服务器上,在 XML 中的 schemaLocation 属性中指向那里,一切正常。

      由于措辞准确的错误,我尝试了这个:

      • 3) 文档的根元素不是

      我知道,我知道,这不应该奏效,但确实奏效了。没有翻译对我不起作用的外部 XSD 是 http://camel.apache.org/schema/spring/camel-spring.xsd

      我不确定这是 SAX 还是 Eclipse 中的错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多