一、XML
1、XML 指可扩展标记语言(eXtensible Markup Language),XML 被设计用来传输和存储数据,而非显示数据。
XML 很重要,也很容易学习,XML标签没被预定义,需要自行定义标签,XML是一种标记语言,很类似HTML。
XML和HTML的区别:
XML与HTML的区别_liu_yujie2011com的博客-CSDN博客_xml和html的区别
2、XML例子
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
二、DOM编程
1、基于树和节点的文档对象模型(Document Object Module)称为DOM,利用Document类来实现XML的解析与创建。
3、DOM编程过程
DocumentBuilder db = dbf.newDocumentBuilder();
(3)建立文档树模型Document:
a、通过xml文档
Document doc = db.parse("bean.xml");
b、将需要解析的xml文档转化为输入流
InputStream is = new FileInputStream("bean.xml");
Document doc = db.parse(is);
NodeList nodelist = document.getElementsByTagName(tagName);
Element node = (Element) nodelist.item(i);
String id = node.getAttribute(“id”) ;
4、DOM实例
1、加载XML文件
以student.xml 为例:
<?xml version="1.0" encoding="UTF-8"?><!--文档声明-->
<students>
<student>
<id>6</id>
<name>李四</name>
<birthday>1996-10</birthday>
<email>ls@163.com</email>
</student>
<student>
<id>8</id>
<name>李娟</name>
<birthday>1992-10</birthday>
<email>lj@163.com</email>
</student>
</students>
DOM解析程序:
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class ParseXML {
public static void main(String[] args) {
try{
//得到DOM解析器的工厂实例
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//从DOM工厂中获得DOM解析器
DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
//把要解析的xml文档读入DOM解析器
Document document = dbBuilder.parse(new File("E:\\guigu\\test\\src\\io\\student.xml"));
System.out.println("处理该文档的DomImplementation对象 = "+ document.getImplementation());
//得到文档名称为Student的元素的节点列表
NodeList nList = document.getElementsByTagName("student");
//遍历该集合,显示结合中的元素及其子元素的名字
for(int i = 0; i< nList.getLength(); i ++){
Element node = (Element)nList.item(i);
System.out.println("id: "+node.getElementsByTagName("id").item(0).getFirstChild().getNodeValue());
System.out.println("name: "+ node.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
System.out.println("birthday: "+ node.getElementsByTagName("birthday").item(0).getFirstChild().getNodeValue());
System.out.println("email: "+ node.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
2、利用XML文件存储信息
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreatXML {
/*创建xml文档*/
public static void main(String[] args) {
Document doc;
Element students,student;
Element name = null;
Element birthday = null;
Element email = null;
try{
//得到DOM解析器的工厂实例
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//从DOM工厂中获得DOM解析器
DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
//创建文档树模型对象
doc = dbBuilder.newDocument();
if(doc != null){
//创建students元素
students = doc.createElement("students");
//创建student元素
student = doc.createElement("student");
//设置元素Student的属性值为1203
student.setAttribute("id", "5");
//创建名称为Name的元素
name = doc.createElement("Name");
//创建名称为张三的文本节点并作为子节点添加到name元素中
name.appendChild(doc.createTextNode("张三"));
//将name子元素添加到student中
student.appendChild(name);
//创建名称为birthday的元素
birthday = doc.createElement("birthday");
//创建名称为 1990-3 的文本节点并作为子节点添加到birthday元素中
birthday.appendChild(doc.createTextNode("1990-3"));
//将birthday子元素添加到student中
student.appendChild(birthday);
//创建名称为Email的元素
email = doc.createElement("email");
//创建名称为zs@163.com 的文本节点并作为子节点添加到Email元素中
email.appendChild(doc.createTextNode("zs@163.com"));
//将email子元素添加到student中
student.appendChild(email);
//将student作为子元素添加到树的根节点school
students.appendChild(student);
//添加到文档树中
doc.appendChild(students);
//创建一个转换的TransformerFactory
TransformerFactory tFactory = TransformerFactory.newInstance();
//建立一个转换的实例,该转换可以将原树转换为结果树。
Transformer transformer = tFactory.newTransformer();
//设置XML的版本和编码以及自动分行,如果不设置,则为默认值。
transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//创建带有DOM节点的新输入源
DOMSource source = new DOMSource(doc);
//建立一个输出结果对象,并设置输出文件地址
StreamResult result = new StreamResult(new File("E:\\guigu\\test\\src\\io\\student2.xml"));
//执行从 Source 到 Result 的输出
transformer.transform(source, result);
System.out.println("创建成功");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
结果:
在指定目录生成student2.xml文件,内容如下:
参考链接:
1、XML与HTML的区别_liu_yujie2011com的博客-CSDN博客_xml和html的区别
2、JAVA操作XML的完整例子——W3C DOM篇_cds27的博客-CSDN博客