XML
1.创建一个简单的xml文档
<?xml version="1.0" encoding="UTF-8"?><!--XML语法-文档声明,一般放在开头-->
<goodList>
<!--包含标签体-->
<good>
<price id="1" birthday="2022.7.2">12</price>
<name>香蕉</name>
<place>北京</place>
</good>
<good>
<price id="2">14</price>
<name>苹果</name>
<place>广州</place>
</good>
<good>
<price>34</price>
<name>椰子</name>
<place>海南</place>
</good>
<!--不含标签体-->
<good></good>
<goods></goods>
<xxx></xxx>
</goodList>
2. 约束
(1)内部引入
直接创建一个xml文档
<?xml version="1.0"?>
<!-- xml文档的约束规则 -->
<!-- note表示创建了一个根节点和括号中的四个子标记,#PCDATA表示子标记为文本文档 -->
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
(2)外部引入
引用本地文件:
创建一个DTD文件
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
创建一个XML文件
<?xml version="1.0" encoding="UTF-8"?>
<!--<!DOCTYPE 根节点名称 SYSTEM "DTD文件地址">-->
<!DOCTYPE note SYSTEM "note.dtd">
<note>
<to></to>
<from></from>
<heading></heading>
<body></body>
</note>
如果引用内部文件报错可以用以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note PUBLIC "-//ccl custom dtd 1.0" "file:///D:\java练习\XML-project\src\note.dtd">
<note>
<to></to>
<from></from>
<heading></heading>
<body></body>
</note>
外部引入(网络)
示例:Struts框架的配置文档的首行
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
(3)schema约束
创建一个XML Schema文档:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com.cn"
xmlns="http://www.w3school.com.cn"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML文档,对schema的引用:
<?xml version="1.0" encoding="UTF-8"?>
<note
xmlns="http://www.w3school.com.cn"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com.cn note.xsd">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
3.使用dom4j解析xml文件
首先导入dom4j.jar(jar包已上传)
然后创建测试文件:
public class ParseXML {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/NewFile.xml"));
Element rootElement = document.getRootElement();//获取解析的XML文件的根元素
// System.out.println(name);
Iterator<Element> iterator = rootElement.elementIterator();//获取根元素下的子元素
while (iterator.hasNext()) {
Element next = iterator.next();
if(next.getName().equals("good")) {
Element element = next.element("name");//获取good标签里的name标签内容
if(element!=null) {
System.out.println(element.getText());
}
}
System.out.println(next.getName());
Iterator<Attribute> attributeIterator = next.attributeIterator();//获取元素属性
while(attributeIterator.hasNext()) {
Attribute next2 = attributeIterator.next();
System.out.println(next2.getName()+":"+next2.getValue());
}
}
}
}
4.利用dom4j生成xml文档
public class createXML {
public static void main(String[] args) throws Exception {
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");//创建根节点
Element author1 = root.addElement("author")//创建子节点和对应的属性
.addAttribute("name", "James")
.addAttribute("location", "UK")
.addText("James Strachan");//创建子节点的内容
author1.addElement("sikiedu.com");
Element author2 = root.addElement("author")
.addAttribute("name", "Bob")
.addAttribute("location", "US")
.addText("Bob McWhirter");
FileWriter out = new FileWriter("foo.xml");
document.write(out);
out.close();
}
}