<?xml version="1.0" encoding="UTF-8"?><!--文档声明必须写在第一行(注释也不能再第一行 )-->
<!-- 版本号 --> <!-- 指定字符集 -->
<!-- XML是树结构,用来存储数据,配置文件
命名注意点:
1.所有XML标签必须有关闭标签
2.XML标签对大小写敏感
3.XML文档必须要有根元素
4.XML的属性值加引号(单双都可)
XML中常用的实体引用(&...& <... < > > > "... "" &apos...'')
标签名称命名规范
1.名称可以包含字母,数字,其他字符,但不能以数字和标点符号开头
2.名称不能以xml,XML,Xml开头 ,但允许xML
3.名称不能包含空格
-->
<test>
<![CDATA[
<abc></abc>里面的内容不被解析,会原样显示
]]>
<a>
" ' 分号不能少
</a>
</test>
package xml2;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/* XML文档结构:XML是树形结构,以一种方式表示整个XML,用节点的方式表示整个XML。
* XML中的节点:
* 文档节点(整个XML文件)
* 元素节点
* 属性节点
* 文本节点(元素之间的内容)
* DOM(文档对象模型)解析特点:
* 在加载的时候,一次性(容易造成内存溢出)的把整个XML文档加载进内存,在内存中形成一棵树(Document对象)
* 以后使用代码操作Document对象,其实操作的是内存中的DOM树,和本地磁盘中的文件无关(因此在修改时要进行同步,让两边保持一致)
* */
//DoM解析基本步骤
public class DOMdemol1 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//1.创建解析器工厂
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
//2.通过解析器工厂得到解析器
DocumentBuilder documentbuilder = documentbuilderfactory.newDocumentBuilder();
//3.通过解析器解析得到Document对象(文档节点)
Document document = documentbuilder.parse("src\\xml2\\Students.xml");
}
}
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Domdemol2 {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("src\\xml2\\Students.xml");
getContent(doc);
//addElement(doc);
//modifyEle(doc);
//deletsEle(doc);
//操作属性是基于元素的(Element)
//addAttribute(doc);
deleteAttribute(doc);
}
//获取元素内容
public static void getContent(Document doc) {
NodeList stulist = doc.getElementsByTagName("student");
Node stuNode = stulist.item(0);
NodeList stuChildNodelist = stuNode.getChildNodes();
System.out.println(stuChildNodelist.getLength());
Node node = stuChildNodelist.item(1);//此处的集合stuChildNodelist.item(0)为空?关于这个,反正就是好像xml里面把空格、tab、回车都有可能当成node。
//所以api里面专门有个方法:ignorableWhitespace 是用来处理这个问题的
System.out.println(node.getTextContent());
}
//增加元素
public static void addElement(Document doc) throws Exception {
Element addEle = doc.createElement("address");
addEle.setTextContent("郑州大学");
NodeList stulist = doc.getElementsByTagName("student");
Node stuNode = stulist.item(0);
Node appendChild = stuNode.appendChild(addEle);
//内存与本地磁盘间的同步操作
TransformerFactory transformFactory = TransformerFactory.newInstance();
Transformer transformer = transformFactory.newTransformer();
//此句话的意思?待解决 应该是把内存中的doc写入磁盘文档
transformer.transform(new DOMSource(doc),new StreamResult("src\\xml2\\Students.xml"));
}
//修改元素内容
public static void modifyEle(Document doc) throws Exception {
NodeList addNodeList = doc.getElementsByTagName("address");
Node addNode = addNodeList.item(0);
addNode.setTextContent("郑州大学软件工程");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(doc),new StreamResult("src\\xml2\\Students.xml"));
}
//删除元素
public static void deletsEle(Document doc) throws Exception {
NodeList addressNodeList = doc.getElementsByTagName("address");
Node addressNode =addressNodeList.item(0);
Node parentNode = addressNode.getParentNode();
parentNode.removeChild(addressNode);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(doc),new StreamResult("src\\xml2\\Students.xml"));
}
//增加属性
public static void addAttribute(Document doc) throws Exception {
NodeList stuNodeList = doc.getElementsByTagName("student");
Node stuNode = stuNodeList.item(0);
/*Attr createAttribute = doc.createAttribute("属性");
createAttribute.setValue("属性值");*/
Element stuEle = (Element)stuNode;
stuEle.setAttribute("属性", "属性值");
TransformerFactory transformerfactory = TransformerFactory.newInstance();
Transformer transformer = transformerfactory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult("src\\xml2\\Students.xml"));
}
//删除属性
public static void deleteAttribute(Document doc) throws Exception {
NodeList stuNodeList = doc.getElementsByTagName("student");
Node stuNode = stuNodeList.item(0);
Element stuEle = (Element)stuNode;
stuEle.removeAttribute("属性");
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(doc),new StreamResult("src\\xml2\\Students.xml"));
}
}
package dom4j;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
//利用do4j读取信息
public class Dom4jDemol {
public static void main(String[] args) throws DocumentException {
//1.创建SAXReader并得到文档节点(Document)(xml文件)
SAXReader saxreader = new SAXReader();
Document doc = saxreader.read("src\\dom4j\\Students.xml");
//2.获取根节点
Element rootElement = doc.getRootElement();
//System.out.println(rootElement.getText());
//3.根据名称获取根元素下所有指定标签
List<Element> stuEles = rootElement.elements("student");
for(Element ele: stuEles) {
/*Element nameEle = ele.element("name");
System.out.println(nameEle.getText());*/
System.out.println(ele.elementText("name"));
System.out.println(ele.elementText("age"));
System.out.println(ele.elementText("sex"));
System.out.println("*************************");
}
}
}
package dom4j;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
//dom4j的相关操作
public class Dom4jDemol1 {
//添加属性
public static void main1(String[] args) throws Exception {
SAXReader saxreader = new SAXReader();
Document doc = saxreader.read("src\\dom4j\\Students.xml");
Element rootElement = doc.getRootElement();
List<Element> stuEleList = rootElement.elements("student");
Element stuEle = stuEleList.get(0);
stuEle.addAttribute("属性","值");
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
Writer wr = new OutputStreamWriter(new FileOutputStream("src\\dom4j\\Students.xml"),"UTF-8");
XMLWriter xmlwriter =new XMLWriter(wr, outputFormat);
xmlwriter.write(doc);
wr.close();
xmlwriter.close();
}
//添加元素
public static void main2(String[] args) throws Exception {
SAXReader saxreader = new SAXReader();
Document doc = saxreader.read("src\\dom4j\\Students.xml");
Element rootEle = doc.getRootElement();
List<Element> stuEles = rootEle.elements("student");
Element stuEle = stuEles.get(0);
Element addElement = stuEle.addElement("address");
addElement.setText("郑州大学");
//同步操作
//格式化输出
OutputFormat format = OutputFormat.createPrettyPrint(); //注意此处
Writer wr = new OutputStreamWriter(new FileOutputStream("src\\dom4j\\Students.xml"),"UTF-8");
XMLWriter xmlwriter = new XMLWriter(wr,format);
//此操作是把内存中的DOM树写入磁盘
xmlwriter.write(doc);//XMLWriter是处理流
wr.close();
xmlwriter.close();
}
//修改元素值
public static void main3(String[] args) throws Exception {
SAXReader saxreader = new SAXReader();
Document doc = saxreader.read("src\\dom4j\\Students.xml");
Element rootEle = doc.getRootElement();
//注意:此处是直接子类
List<Element> stuEles = rootEle.elements("student");
Element stuEle = stuEles.get(0);
List<Element> addressEles = stuEle.elements("address");
Element addressEle = addressEles.get(0);
addressEle.setText("郑州大学软件工程");
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
Writer wr = new OutputStreamWriter(new FileOutputStream("src\\dom4j\\Students.xml"),"UTF-8");
XMLWriter xmlWriter = new XMLWriter(wr,outputFormat);
xmlWriter.write(doc);
wr.close();
xmlWriter.close();
}
//修改属性值(利用setValue方法,修改属性名呢?)
public static void main4(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read("src\\dom4j\\Students.xml");
Element rootEle = doc.getRootElement();
List<Element> stuEles = rootEle.elements("student");
Element stuEle = stuEles.get(0);
Attribute attribute = stuEle.attribute(1);
System.out.println(attribute.getName());
attribute.setValue("new Value");
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
Writer wr = new OutputStreamWriter(new FileOutputStream("src\\dom4j\\Students.xml"),"UTF-8");
XMLWriter xmlWriter = new XMLWriter(wr,outputFormat);
xmlWriter.write(doc);
wr.close();
xmlWriter.close();
}
//删除属性
public static void main5(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read("src\\dom4j\\Students.xml");
Element rootEle = doc.getRootElement();
List<Element> stuEles = rootEle.elements("student");
Element stuEle = stuEles.get(0);
Attribute attribute = stuEle.attribute("属性");
stuEle.remove(attribute);
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
Writer wr = new OutputStreamWriter(new FileOutputStream("src\\dom4j\\Students.xml"),"UTF-8");
XMLWriter xmlWriter = new XMLWriter(wr,outputFormat);
xmlWriter.write(doc);
wr.close();
xmlWriter.close();
}
//删除元素
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read("src\\dom4j\\Students.xml");
Element rootEle = doc.getRootElement();
List<Element> stuEles = rootEle.elements("student");
Element stuEle = stuEles.get(0);
Element element = stuEle.element("address");
stuEle.remove(element);
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
Writer wr = new OutputStreamWriter(new FileOutputStream("src\\dom4j\\Students.xml"),"UTF-8");
XMLWriter xmlWriter = new XMLWriter(wr,outputFormat);
xmlWriter.write(doc);
wr.close();
xmlWriter.close();
}
}