xml是我们经常使用的配置了,今天在这里做一下笔记,将dtd, xsd对xml的约束记录于此。
demo dtd
myClass.dtd
<!ELEMENT myclass (students+)>
<!ELEMENT students (name, age, introduce)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT introduce (#PCDATA)>
myClass.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 约束这个xml -->
<!DOCTYPE myclass SYSTEM "conf/myClass.dtd">
<myclass>
<students>
<name>baixiaoshi</name>
<age>26</age>
<introduce>introduce youself</introduce>
</students>
<students>
<name>hurong</name>
<age>27</age>
<introduce>hello world</introduce>
</students>
</myclass>
解析验证这个xml是否符合dtd中约束的规范
package main.java;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class testXml {
public static void main(String[] args) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setValidating(true);
String rootPath = System.getProperty("user.dir");
String configPath = rootPath + "/conf/myClass.xml";
System.out.println("configPath=" + configPath);
try {
FileInputStream fileInputStream = new FileInputStream(configPath);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
documentBuilder.parse(fileInputStream);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
可以运行看看结果
基础知识记录
<!DOCTYPE 根元素 [元素声明]>
<!DOCTYPE 根元素 SYSTEM "文件名">
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
demo xsd
shiporder.xsd
<?xml version="1.0" encoding="utf-8"?>
<shiporder orderid="1232424"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>baixiaoshi</orderperson>
<shipto>
<name>guoguo</name>
<address>this is my address</address>
<city>fuzhou</city>
<country>china</country>
</shipto>
<item>
<title>english book</title>
<note>edition</note>
<quantity>1</quantity>
<price>99.00</price>
</item>
<item>
<title>desktop computer</title>
<note>this is my computer</note>
<quantity>1</quantity>
<price>9.90</price>
</item>
</shiporder>
shiporder.xml
<?xml version="1.0" encoding="utf-8"?>
<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:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string" />
<xs:element name="country" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string" minOccurs="0" />
<xs:element name="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>
优化后的xsd
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- 简易元素的定义 -->
<xs:element name="orderperson" type="xs:string"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="note" type="xs:string"/>
<xs:element name="quantity" type="xs:positiveInteger"/>
<xs:element name="price" type="xs:decimal"/>
<!-- 属性的定义 -->
<xs:attribute name="orderid" type="xs:string"/>
<!-- 复合元素的定义 -->
<xs:element name="shipto">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="address"/>
<xs:element ref="city"/>
<xs:element ref="country"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="note" minOccurs="0"/>
<xs:element ref="quantity"/>
<xs:element ref="price"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="shiporder">
<xs:complexType>
<xs:sequence>
<xs:element ref="orderperson"/>
<xs:element ref="shipto"/>
<xs:element ref="item" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute ref="orderid" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
验证xsd的java代码
package main.java;
import org.xml.sax.SAXException;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import java.io.File;
import java.io.IOException;
/**
* Created by lion on 2018/1/14.
*/
public class validXml {
public static void main(String[] args) {
String rootPath = System.getProperty("user.dir");
String schemaPath = rootPath + "/conf/shiporder.xsd";
String xmlPath = rootPath + "/conf/shiporder.xml";
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
try {
Schema schema = schemaFactory.newSchema( new File(schemaPath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlPath)));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}