Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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 如何使用内部的xsd验证xml?_Java_Xml_Xsd_Xsd Validation - Fatal编程技术网

Java 如何使用内部的xsd验证xml?

Java 如何使用内部的xsd验证xml?,java,xml,xsd,xsd-validation,Java,Xml,Xsd,Xsd Validation,当xml文档包含模式时,我无法根据xml模式验证xml文件。xml文件如下所示: <?xml version="1.0"?> <catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:x="urn:book"> <!-- START OF SCHEMA --> <xsd:schema targetNamespace="urn:book"> <xsd:elem

当xml文档包含模式时,我无法根据xml模式验证xml文件。xml文件如下所示:

<?xml version="1.0"?>
<catalog xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
         xmlns:x="urn:book"> 
<!-- START OF SCHEMA -->
<xsd:schema targetNamespace="urn:book">
 <xsd:element name="book">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="author" type="xsd:string"/>
      <xsd:element name="title" type="xsd:string"/>
      <xsd:element name="genre" type="xsd:string"/>
      <xsd:element name="price" type="xsd:float"/>
      <xsd:element name="publish_date" type="xsd:date"/>
      <xsd:element name="description" type="xsd:string"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:string"/>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>
<!-- END OF SCHEMA -->
   <x:book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with
      XML.</description>
   </x:book>
</catalog>
// define the type of schema - we use W3C:
String schemaLang = "http://www.w3.org/2001/XMLSchema";

// get validation driver:
SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

// create schema by reading it from an XSD file:
Schema schema = factory.newSchema(new StreamSource("..........."));
Validator validator = schema.newValidator();

// at last perform validation:
validator.validate(new StreamSource("myDoc.xml"));
我的问题是在这种情况下如何使用SchemaFactory对象


我非常感谢你的帮助

我想这就是你想要的;该代码旨在说明,而不是说明良好的编程实践。它已经用XML进行了测试。主要的假设是document元素有两个元素,第一个是XSD,第二个是要验证的XML

例如,如果将44.95更改为d44.95,您将获得以下输出:

XML无效,因为cvc数据类型有效。1.2.1:“d44.95”不是“float”的有效值

否则,一切正常,程序打印XML是有效的


作为Gardea解决方案的替代方案(这很好,只是我不喜欢任何涉及使用DOM的东西),您可以进行转换,将模式和book元素提取为单独的文档,然后再对其中一个进行验证。我之所以提到这一点,是因为验证前转换是一种未充分使用的设计模式。

首先要注意的是:根据模式,您的文档无效。这是因为模式将
book
定义为根元素,而文档将
catalog
定义为根元素。因此,基本上,您需要将文档分为两部分:模式和内容。实现这一目标有多种途径;您可以使用XSL转换,或者通过DOMAPI处理文档。不久前,有人希望根据大的ol XML模式验证XSD文件,以用于XML模式。如果要了解这一点,可能会有所帮助。基本上,您需要在提供文档的模式的同时提供大ol'模式。这可以通过向
SchemaFactory.newSchema(StreamSource[])
:)提供多个架构来实现。。。因此,“良好编程”免责声明;我在回答中选择了清晰,我认为domapi仍然是最好的教学工具。解释XPathAPI的使用就是说我有意避免使用条件逻辑。谢谢你提出这个问题。@Michael Kay在验证之前进行转换是一种未充分利用的设计模式-完全同意
import java.io.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.*;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import javax.xml.xpath.*;
import org.xml.sax.InputSource;

public class TestValidation {
    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodes = (NodeList)xpath.evaluate("/*/*", new InputSource("XmlWithEmbeddedXsd.xml"), XPathConstants.NODESET);
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        Validator validator = factory.newSchema(new DOMSource(nodes.item(0))).newValidator();
        try {
            validator.validate(new DOMSource(nodes.item(1)));
            System.out.println("XML is valid.");
        }
        catch (SAXException ex) {
            System.out.println("XML is not valid because " + ex.getMessage());
        }
    }
}