Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用XAS解析器不会使用XSD验证XML_Java_Xml_Validation_Xsd_Sax - Fatal编程技术网

Java 使用XAS解析器不会使用XSD验证XML

Java 使用XAS解析器不会使用XSD验证XML,java,xml,validation,xsd,sax,Java,Xml,Validation,Xsd,Sax,在我下面的xml中,验证似乎没有成功 addressbook.xml <?xml version ="1.0" encoding="UTF-8"?> <addressbook> <address> <name> <first-name>Samitha</first-name> <last-name>Chathuranga</last-

在我下面的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>
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
变量,但是
读取器仍然使用旧的
SAXParser

//使用SAX检查格式是否正确。
SAXParserFactory=SAXParserFactory.newInstance();
工厂设置验证(true);
factory.setNamespaceAware(true);
SAXParser parser=factory.newSAXParser();
XMLReader=parser.getXMLReader();
setErrorHandler(新的SimpleErrorHandler());
parse(新的InputSource(“src/addressbook.xml”);
System.out.println(“使用SAX检查格式良好的状态”);
//使用SAX解析器使用外部模式进行验证
factory.setValidating(false);//将验证设置为false
SchemaFactory SchemaFactory=SchemaFactory.newInstance(“http://www.w3.org/2001/XMLSchema");
setSchema(schemaFactory.newSchema(newsource[]{newstreamsource(“src/addressbook.xsd”)});
parser=factory.newSAXParser();//创建一个新的解析器
reader=parser.getXMLReader();//让你的读者耳目一新
setErrorHandler(新的SimpleErrorHandler());
parse(新的InputSource(“src/addressbook.xml”);
System.out.println(“使用SAX解析时检查验证”);
System.out.println(“解析成功”);

非常感谢。问题就这样解决了。但是我有个问题。。为什么要设置factory.setValidating(false)。事实上,设置为真或假会发生什么?当我在上面将其设置为true时,出现一个错误:“文档无效:找不到语法。文档根元素“addressbook”必须与DOCTYPE root“null”匹配。“文档怎么会因为没有引用DOCTYPE而无效?
setValidating()
将通过查找DTD(语法)来验证XML。”。但是在这里,您使用的是XSD,而不是DTD,那么您必须禁用此验证。“真实”验证由
setSchema()
函数实现。但是我还有一个问题。。在第一部分中“//使用SAX检查格式良好的状态。=>当我给出上面的setValidating(true)时,这不会根据XSD进行验证”。文档无效:未找到语法。文档根元素“addressbook”必须与DOCTYPE root“null”匹配。“错误不会出现。实际上根本不会出现错误。因此,这与您的上述解释相冲突。!!!只要您不设置
架构,SAX就不会检查有效性。您没有正确地获取我的问题。我的问题是,即使我给出了setValidating(true),也会出现这种情况在第一部分中,前面提到的错误没有出现?我没有确定DTD。因此,根据您之前的解释,因为我没有给出DTD,应该会出现错误..这里是什么情况。。。?
//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();
        }
    }
}
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());
    }

}