<?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- xml 语言用于描绘数据之间的关系 可以用来保存有关系的数据,也可以作为软件配置文件。 cdata区,不会被解析引擎解析的区域 --><书架> <书 name="xxxx"> <书名 name="xxx"/> <作者>张</作者> <价格>34</价格> <售价>59.00元</售价></书> <书> <作者>a2</作者> <价格>book</价格> </书> </书架>
xml 规范之一DTD,对于属性,和元素等的分类,详见w3c DTD教学。@H_404_5@
实体类型:
引用实体 语法格式 <!ENTITY 实体名称 “试题内容”>
引用方式:&实体名称
举例 <!ENTITY copyright “...”>
©right
xml中处理指令,都需要<? 作为开头 > 作为结尾
xml文档可以使用xml-stylesheet指令,通知xml解析引擎,应用css文件显示xml@H_404_5@
外联,
<?xml-stylesheet href="style.css"type="text/css"?>
内联:
<?xml-stylesheet href="#style"type="text/css"?>
@H_404_5@
文档内容。
<?xml-stylesheet-type="text/css" href = "..."?>
参数实体 被dtd文件自身使用
语法格式 <!ENTITY %实体名称 “试题内容”>
dom 和sax解析的区别
DOM解析会把xml文档了文件放在内存中作为一个document对象,每一个节点变成内存中一个对象。Root—element。Head—element
中间节点--Title对象文本—text 属性—attribute。
Xml文档太大,会造成内存溢出。优点增删改查比较容易(删除某一个节点时,通过document找到你要删除的节点,节点的boby方法可以进行修改)
Sax解析对内存需要较小,适合读取,不适合增删改查。读一行解析一行。@H_404_5@
package test; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; //jaxp 开发包属于j2se开发包,所以不需要进行额外的导包 import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.w3c.dom.Node; public class Demo9 { //读取xml文档中的数据,Xml解析开发包:jaxp(sun),dom4j。下面的是jaxp方式 @Test public void read() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/catalog.xml"); //将xml进行解析获得一个document对象。 NodeList list = document.getElementsByTagName("作者"); Node node = list.item(1); //1从第2个开始 String context = node.getTextContent(); System.out.println(context); } // @Test public void read2() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/catalog.xml"); Node root = document.getElementsByTagName("书架").item(0); list(root); } private void list(Node node){ if(node instanceof Element){ System.out.println(node.getNodeName()); } NodeList list = node.getChildNodes(); for(int i=1;i<list.getLength();i++){ Node child = list.item(i); list(child); } } // @Test public void read3() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/catalog.xml"); //node 作为父类,没有可以根据属性名称获得单一属性内容的方法 Element root = (Element)document.getElementsByTagName("书架").item(0);//强转是因为知道可以(即知道所获得的数据可以被转为element) String value = root.getAttribute("name"); System.out.println(value); } //增加节点到xml文档 @Test public void add() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/catalog.xml"); //创建节点 Element price = document.createElement("售价"); price.setTextContent("59.00元"); //把创建的节点挂在。。。 Element book = (Element)document.getElementsByTagName("书").item(0); book.appendChild(price); //把更新后的内存写回到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); //transform是一个接口,不能直接new。 //transform 方法接受一个源和一个目的地,source 是一个接口,DOMsource是实现了该接口的类 result也是一个接口,StreamResualt是实现了该接口的类 tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/catalog.xml"))); //添加会默认添加到末尾。 } //增加节点到xml文档指定位置 @Test public void add2() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/catalog.xml"); //创建节点 Element price = document.createElement("售价"); price.setTextContent("59.00元"); //得到参考节点 Element refNode = (Element)document.getElementsByTagName("书").item(0); //把创建的节点挂在。。。 Element book = (Element)document.getElementsByTagName("书").item(0); //把节点插在指定位置 book.insertBefore(price,refNode); //把更新后的内存写回到xml文档 TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/catalog.xml"))); } //想xml文档上添加属性 @Test public void addAttr() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/catalog.xml"); Element bookName = (Element)document.getElementsByTagName("书").item(0); bookName.setAttribute("name","xxxx"); TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/catalog.xml"))); } public void delete() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/catalog.xml"); Element e = (Element)document.getElementsByTagName("售价").item(0); //e.getParentNode().removeChild(e); Element book = (Element)document.getElementsByTagName("书").item(0); book.removeChild(e); TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/catalog.xml"))); } public void update() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse("src/catalog.xml"); Element e = (Element)document.getElementsByTagName("售价").item(0); e.setTextContent("109元"); TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/catalog.xml"))); } }原文链接:https://www.f2er.com/xml/298826.html