簡體   English   中英

使用SAXParser針對XSD驗證XML導致錯誤

[英]Validate XML against XSD with SAXParser results in error

我有一個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'.

我該怎么做才能解決這個問題?

任何幫助表示贊賞,

謝謝!

從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 

我可以通過更改名稱空間前綴為“ xs: ”的XSD並將所有實例搜索/替換為“ xsd: ”(以及xmlns:xsd =定義)來解決此類問題。 然后,我將此XSD托管在內部服務器上,並在XML的schemaLocation屬性中指向該服務器,一切正常。

我嘗試此操作的原因是確切的錯誤:

  • 3)文檔的根元素不是<xsd:schema>

我知道,我知道,這不應該起作用,但確實可以。 如果沒有翻譯,對我不起作用的外部XSD是http://camel.apache.org/schema/spring/camel-spring.xsd

我不確定這是否是SAX或Eclipse中的錯誤。

嘗試使用此代碼來驗證您的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM