gpt4 book ai didi

java - 如何使用 xsd 验证 xml?

转载 作者:行者123 更新时间:2023-11-30 04:54:08 37 4
gpt4 key购买 nike

当 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>

java 代码如下:

// 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 进行了测试。主要假设是文档元素有两个元素,第一个是 XSD,第二个是要验证的 XML。

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

XML 无效,因为 cvc-datatype-valid.1.2.1:“d44.95”不是“float”的有效值。

否则,一切都会正常,程序会打印 XML is valid.

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());
}
}
}

关于java - 如何使用 xsd 验证 xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9102275/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com