<?xml version="1.0" encoding="UTF-8" ?> <!-- 声明xml文档头 -->
<!-- 内部DTD约束 -->
<!--
!DOCTYPE: 固定语法 文档类型
students: 自定义根标签名字
[]: 存放子标签
ELEMENT: 元素(标签)
#PCDATA:元素是字符串类型#PCDATA(不能再有子元素,也不能为空)
DTD中标签的顺序就是日后使用的顺序, 不允许改变
-->
<!DOCTYPE students[
<!-- 标签 -->
<!--
(student): 只能有一对student标签
(student?): 0~1次
(student+): 至少1次
(student*): 0~多次
-->
<!ELEMENT students (student*)>
<!ELEMENT student (name, age, sex)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!-- 属性 -->
<!--
ATTLIST: 属性
student: 哪个标签指定属性
id: 指定的属性名
CDATA: 属性控制
#REQUIRED: 必须有
#FIXED "值": 固定值
#IMPLIED: 可有可无
-->
<!ATTLIST student id CDATA #REQUIRED>
<!ATTLIST name class CDATA #FIXED "qwe">
<!ATTLIST age test CDATA #IMPLIED>
]>
<students>
<student id="aa">
<name class="qwe">张三</name>
<age test="aaa">18</age>
<sex>男</sex>
</student>
</students>
外部DTD是我们自己编写的DTD文件。通过引入方式引入DTD。
在外部创建一个xxx.dtd文件,文件内容和内部dtd [ ] 中的内容相同。
<!ELEMENT students (student*)>
<!ELEMENT student (name, age, sex)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ELEMENT sex (#PCDATA)>
<!ATTLIST student id CDATA #REQUIRED>
<!ATTLIST name class CDATA #FIXED "qwe">
<!ATTLIST age test CDATA #IMPLIED>```
==xml引入外部dtd文件==
```java
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE students SYSTEM "aa.dtd">
<students>
<student id="aa" >
<name class="qwe">张三</name>
<age test="aaa">18</age>
<sex>男</sex>
</student>
</students>
<!--
声明xsd约束
aa: 自定义名称(随意)
schema: 约束(固定)
-->
<aa:schema xmlns:aa="http://www.w3.org/2001/XMLSchema">
<!--
aa:element: 标签
name: 标签名
-->
<aa:element name="students">
<!-- 必须设置为复杂类型 -->
<aa:complexType>
<!-- 在此标签中规范了 标签的顺序 -->
<aa:sequence>
<!-- 引用其他标签 -->
<!-- maxOccurs: 指定可以有多少个标签 -->
<aa:element ref="student" maxOccurs="2"/>
</aa:sequence>
</aa:complexType>
</aa:element>
<aa:element name="student">
<!-- 复杂类型可以指定 包含的标签 属性 -->
<aa:complexType>
<aa:sequence>
<!-- type: 值的类型 -->
<aa:element name="name" type="aa:string"/>
<aa:element name="age" type="aa:int"/>
<aa:element name="sex" type="aa:boolean"/>
</aa:sequence>
<!--
use: 属性的设置
required : 必须存在
prohibited: 禁用
optional: 可选的
fixed="值": 固定值
-->
<aa:attribute name="id" use="optional" fixed="aa"/>
</aa:complexType>
</aa:element>
</aa:schema>
<?xml version="1.0" encoding="UTF-8" ?>
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="s.xsd">
<student id="aa">
<name></name>
<age>11</age>
<sex>false</sex>
</student>
</students>
下面是Dom解析本地的xml文件
在这里插public class T1 {
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
parse();
}
public static void parse() throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//获取文档构建对象
DocumentBuilder db = dbf.newDocumentBuilder();
//指定解析的文件,解析为document对象
Document document = db.parse("src/com/sxt/student.xml");
//通过标签名获取节点,首先获取根节点
NodeList students = document.getElementsByTagName("students");
Node item = students.item(0);
System.out.println(item.getNodeName());
//获取所有子节点
NodeList childNodes = item.getChildNodes();
for (int i = 0; i < childNodes.getLength() ; i++) {
Node node = childNodes.item(i);
//判断是否是元素节点
if (node.getNodeType() == Node.ELEMENT_NODE){
System.out.println(node.getNodeName());
//获取此节点中的子节点
NodeList nodes = node.getChildNodes();
for (int j = 0; j < nodes.getLength() ; j++) {
Node item1 = nodes.item(j);
if (item1.getNodeType() == Node.ELEMENT_NODE){
System.out.println(item1.getNodeName());
System.out.println(item1.getTextContent());
Node aClass = item1.getAttributes().getNamedItem("class");
if (aClass != null){
System.out.println(aClass);
}
}
}
}
}
}
}入代码片
## 引用xsd
```java
<?xml version="1.0" encoding="UTF-8" ?>
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="s.xsd">
<student id="aa">
<name></name>
<age>11</age>
<sex>false</sex>
</student>
</students>
这里是SAX解析xml文件
public class T1 {
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
parse();
}
public static void parse() throws ParserConfigurationException, SAXException, IOException {
//解析工厂
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
//获取解析对象
SAXParser saxParser = saxParserFactory.newSAXParser();
//使用解析器解析xml
saxParser.parse(new File("D:\\workspace_ideas\\javase_13\\src\\com\\sxt\\student.xml"), new MyHandler());
}
public static class MyHandler extends DefaultHandler {
String name = null;
@Override
public void startDocument() throws SAXException {
System.out.println("文档开始解析");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("开始解析,标签名"+qName);
name = qName;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String s = new String(ch,start,length);
System.out.println(s);
}
@Override
public void endDocument() throws SAXException {
System.out.println("文档解析结束");
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println("解析结束,标签名"+qName);
}
}
}
public class DomParse {
public static List<Phone> parse(String path) throws ParserConfigurationException, IOException, SAXException {
//解析xm代码写在这
//1.获取构建器工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//2.获取文档构建器
DocumentBuilder db = dbf.newDocumentBuilder();
//3.获取文档对象
Document parse = db.parse(path);
//开始解析,获取根标签
NodeList phones = parse.getElementsByTagName("phones");
Node root = phones.item(0);
System.out.println("root:"+root.getNodeName());
//获取根标签中的所有子节点
NodeList childNodes = root.getChildNodes();
//定义手机对象的集合
ArrayList<Phone> list = new ArrayList<>();
//定义手机对象
for (int i = 0; i < childNodes.getLength(); i++) {
Phone phone = new Phone();
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE){
System.out.println(" 子节点:"+item.getNodeName());
NodeList childNodes1 = item.getChildNodes();
for (int i1 = 0; i1 < childNodes1.getLength(); i1++) {
Node item1 = childNodes1.item(i1);
if (item1.getNodeType() == Node.ELEMENT_NODE){
System.out.println(" 子节点"+item1.getNodeName()+" 子节点内容"+item1.getTextContent());
if (item1.getNodeName().equals("name")){
phone.setName(item1.getTextContent());
} else if (item1.getNodeName().equals("price")){
phone.setPrice(Double.parseDouble(item1.getTextContent()));
} else if(item1.getNodeName().equals("color")){
phone.setColor(item1.getTextContent());
}
}
}
list.add(phone);
}
}
return list;
}
}
public class SAXParse {
public static List<Phone> parse(String path) throws ParserConfigurationException, SAXException, IOException {
//1.获取sax解析工厂
SAXParserFactory spf = SAXParserFactory.newInstance();
//获取解析器对象
SAXParser saxParser = spf.newSAXParser();
//获取解析对象,解析xml
MyHandler myHandler = new MyHandler();
saxParser.parse(new File("phone.xml"), myHandler);
return myHandler.list;
}
private static class MyHandler extends DefaultHandler {
String name = null;
Phone phone;
List<Phone> list;
@Override
public void startDocument() throws SAXException {
System.out.println("文档开始解析");
// phone = new Phone();
list = new ArrayList<>();
}
@Override
public void endDocument() throws SAXException {
System.out.println("文档解析结束");
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
System.out.println("解析开始标签," + qName);
name = qName;
if (qName.equals("phone")) {
phone = new Phone();
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println("解析结束标签," + qName);
if (qName.equals("phone")) {
list.add(phone);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String val = new String(ch, start, length);
System.out.println(val + "长度" + val.length());
if (!(val.trim().length() == 0)) {
if (name.equals("name")) {
phone.setName(val);
} else if (name.equals("price")) {
phone.setPrice(Double.parseDouble(val));
} else if (name.equals("color")) {
phone.setColor(val);
}
}
}
}
}