簡體   English   中英

使用XAS解析器的XSD未驗證XML

[英]XML is not validated with the XSD using XAS parser

似乎在我下面的xml中驗證不成功。

addressbook.xml

<?xml version ="1.0" encoding="UTF-8"?>

<addressbook>
    <address>
        <name>
            <first-name>Samitha</first-name>
            <last-name>Chathuranga</last-name>
            <sasa>dd</sasa>
        </name>
        <street>107 B</street>
        <village>Poramba</village>
        <city>AG</city>
        <postal-code>80300</postal-code>
        <country>Sri Lanka</country>
    </address>
    <address>
        <name>
            <first-name>Hasara</first-name>
            <last-name>Semini</last-name>
        </name>
        <street>32 A</street>
        <village>Dombanwila</village>
        <city>RG</city>
        <postal-code>34300</postal-code>
        <country>Sri Lanka</country>
    </address>

</addressbook>

我添加了額外的元素dd來檢查它是否已通過以下XSD的驗證,但似乎沒有。 沒有錯誤消息來。 因此驗證似乎不成功。

addressbook.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="addressbook">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="address" maxOccurs="unbounded"/>          
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="address" >
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="name" />
                <xsd:element ref="street" />
                <xsd:element ref="village" />
                <xsd:element ref="city" />
                <xsd:element ref="postal-code" />
                <xsd:element ref="country" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    <xsd:element name="name">
        <xsd:complexType>
        <xsd:sequence>
            <xsd:element ref="first-name"/>
            <xsd:element ref="last-name"/>
        </xsd:sequence>
    </xsd:complexType>
    </xsd:element>
    <xsd:element name="first-name" type="xsd:string" />
    <xsd:element name="last-name" type="xsd:string" />
    <xsd:element name="street" type="xsd:string" />
    <xsd:element name="village" type="xsd:string" />

    <xsd:element name="city">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:length value="2" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>

    <xsd:element name="postal-code" type="xsd:string" />
    <xsd:element name="country" type="xsd:string" />

</xsd:schema>

是什么原因呢?

以下是用於通過SAX進行驗證和解析的2個Java類。

SAX_XSDValidator.java

//1. Checks for well-formed-ness of the XML with extrnal XML Schema when parsing by SAX Parser.
//2. Validated the XML file with external XML schema Using SAX Parser

// JAXP
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class SAX_XSDValidator {
    public static void main(String[] args) {
        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            // checking for well-formed-ness using SAX.=>This doesn't validate
            // against XSD
            // Turn off validation, and turn on namespaces
            factory.setValidating(false);
            factory.setNamespaceAware(true);
            reader.setErrorHandler(new SimpleErrorHandler());
            reader.parse(new InputSource("src/addressbook.xml"));
            System.out.println("Checked well-formed-ness using SAX ");


            // validating using external schema Using SAX Parser
            // SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(true);
            factory.setNamespaceAware(true);
            SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
            factory.setSchema(schemaFactory.newSchema(new Source[] { new StreamSource("src/addressbook.xsd") }));
            reader.setErrorHandler(new SimpleErrorHandler());
            reader.parse(new InputSource("src/addressbook.xml"));
            System.out.println("Validation Checked when Parsing with SAX");


            System.out.println("Parsing successful");

        } catch (ParserConfigurationException e) {
            System.out.println("The underlying parser does not support "
                    + " the requested features.");
        } catch (FactoryConfigurationError e) {
            System.out.println("Error occurred obtaining SAX Parser Factory.");
        } catch (Exception e) {
            System.out.println("Caught Exception");
            e.printStackTrace();
        }
    }
}

SimpleErrorHandler.java

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;

public class SimpleErrorHandler implements ErrorHandler {
    public void warning(SAXParseException e) throws SAXException {
        System.out.println(e.getMessage());
    }

    public void error(SAXParseException e) throws SAXException {
        System.out.println(e.getMessage());
    }

    public void fatalError(SAXParseException e) throws SAXException {
        System.out.println(e.getMessage());
    }

}

您需要創建一個新的SAXParser :您正在更改factory變量,但是您的reader仍然使用舊的SAXParser

// checking for well-formed-ness using SAX.
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);

SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();

reader.setErrorHandler(new SimpleErrorHandler());
reader.parse(new InputSource("src/addressbook.xml"));
System.out.println("Checked well-formed-ness using SAX ");


// validating using external schema Using SAX Parser
factory.setValidating(false); // set validation to false
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
factory.setSchema(schemaFactory.newSchema(new Source[] { new StreamSource("src/addressbook.xsd") }));

parser = factory.newSAXParser(); // create a new parser
reader = parser.getXMLReader(); // and refresh your reader

reader.setErrorHandler(new SimpleErrorHandler());
reader.parse(new InputSource("src/addressbook.xml"));
System.out.println("Validation Checked when Parsing with SAX");


System.out.println("Parsing successful");

暫無
暫無

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

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