XML concise tutorial (7)
Table of Contents
Development History
##XMLComparison with HTML Extensible
XML Syntax details compared with HTML
XML validation DTD
XMLNamespace
XMLSyntax structure
XML Validation Schema
DOM4JRead and write configuration file
About SLT
XML Validation Schema
In addition to the above advantages,
Schema is even more impressive than DTD The exciting thing is that it is itself a well-formed XML document, so it is very easy to write Schema. Compared with DTD which has its own independent syntax, it is very difficult to write and maintain. A Schema file is an XML file, so the corresponding
Schema## written by XML The process of # is to write XML against XML. In this case, it is very easy to write Schema. The following demonstrates how to write the corresponding Schema against XMLOriginal XML file (test2.xml)
<?xml version="1.0"encoding="ISO-8859-1"?> <shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>George Bush</orderperson> <shipto> <name>John Adams</name> <address>Oxford Street</address> <city>London</city> <country>UK</country> </shipto> <item> <title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price> </item> <item> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder>
##For the above XML Let’s start creating a Schema. The principle to follow is how to write the original XML and then describe the corresponding Schema, just like you are face to face with a person. The description is the same.
Schema code is as follows (shiporder.xsd)
## <?xml version="1.0"encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element name="orderperson"type="xs:string"/>
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:elementname="name" type="xs:string"/>
<xs:elementname="address" type="xs:string"/>
<xs:elementname="city" type="xs:string"/>
<xs:elementname="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item"maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:elementname="title" type="xs:string"/>
<xs:elementname="note" type="xs:string" minOccurs="0"/>
<xs:elementname="quantity" type="xs:positiveInteger"/>
<xs:element name="price"type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="orderid"type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
Code analysis:
The first line is allXML The statement needs no elaboration.
The second line does thisXML(Schema itself is a XML) defines a namespace.
Starting from the fourth line are some requirements for the originalXML: 首先定义了根元素为shiporder(行4),其次因为shiporder元素有一个属性,其中包含其他的元素所以其为复合类型(行5)。然后通过sequence元素按照顺序包围其子元素(行10---行15),描述元素的名称以及元素的类型(行11----行14),如果需要描述元素的限制条件(行22)。描述根元素的属性,由于是必选属性所以选择required关键字,需要注意的是这个属性必须放在最后(行29) 通过Schema验证XML的代码和前面文章中的DTD验证大同小异,代码如下:
如果原XML文件符合Schema文件中的描述则返回true;否则抛出异常进行描述哪里不符合,并且返回false。(具体的操作可在实际工程中自行定制,这里只是进行简单的描述)
以上就是XML简明教程(7) 的内容,更多相关内容请关注PHP中文网(www.php.cn)!package ValidateXml;
import java.io.File;
import java.io.IOException;
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;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
importcom.sun.org.apache.xml.internal.utils.DefaultErrorHandler;
public class XmlValidator
{
private String xsdFilePath;
public XmlValidator(String xsdFilePath)
{
this.xsdFilePath =xsdFilePath;
}
public String validata(String xmlFilePath,ErrorHandler errorHandler)
{
String msg = null;
SchemaFactoryfactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try
{
Schema schema = factory.newSchema(new File(xsdFilePath));
Validator validator = schema.newValidator();
validator.setErrorHandler(errorHandler);
validator.validate(new StreamSource(new File(xmlFilePath)));
}
catch (SAXExceptione)
{
msg = e.getMessage();
e.printStackTrace();
}
catch (IOExceptione)
{
msg = e.getMessage();
e.printStackTrace();
}
return msg;
}
public static void main(String[] args)
{
String xmlFilePath ="d://test2.xml";
String xsdFilePath ="d://shiporder.xsd";
XmlValidator my =new XmlValidator(xsdFilePath);
String msg =my.validata(xmlFilePath, new DefaultErrorHandler());
System.out.println(msg == null);
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and
