Home >Backend Development >XML/RSS Tutorial >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
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);
}
}