标签:stand parent att 解析 ttf src 位置 parser cto
1
2
3
4
5
6
7
8
9
Dom和sax解析方法的区别:
1,dom解析的优点是对文档crud比较方便,缺点是占用内存比较大
2,sax解析的优点是占用内存少,解析熟读快,缺点是只适合做文档的读取,不适合crud
<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
<书>
<书名 name="小黄书">Java就业培训教程</书名>
<作者>张兆祥</作者>
<售价>59yuan</售价>
<售价>39</售价>
<售价>59yuan</售价></书>
<书>
<书名>JavaScript网页开发</书名>
<作者>张兆祥</作者>
<售价>12</售价>
</书>
</书架>
package day2;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
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.Node;
import org.w3c.dom.NodeList;
//使用dom方式对xml文档进行crud
public class bookdemo {
@Test
public void read1()throws Exception {
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdocument=(Document)builder.parse("src/day2/book.xml");
NodeListlist=document.getElementsByTagName("书名");
Nodenode=list.item(1);
Stringcontent=node.getTextContent();
System.out.println(content);
}
//得到xml文件中所有标签
@Test
public void read2()throws Exception {
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdocument=(Document)builder.parse("src/day2/book.xml");
//得到根节点
Noderoot=document.getElementsByTagName("书架").item(0);
list(root);
}
private void list(Nodenode) {
if(node instanceof Element) {
System.out.println(node.getNodeName());
}
NodeListlist=node.getChildNodes();
for(int i=0;i<list.getLength();i++) {
Nodechild=list.item(i);
list(child);
}
}
//得到xml文档中标签属性的值:<书名 name="hahaha">Java就业培训教程</书名>
@Test
public void read3()throws Exception {
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdocument=(Document)builder.parse("src/day2/book.xml");
Elementbookname=(Element)document.getElementsByTagName("书名").item(0);
Stringvalue=bookname.getAttribute("name");
System.out.println(value);
}
//向xml文档中添加节点:<售价>59yuan</售价>
@Test
public void add()throws Exception {
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdocument=(Document)builder.parse("src/day2/book.xml");
//创建节点
Elementprice=document.createElement("售价");
price.setTextContent("59yuan");
//把创建的节点挂到第一本书上
Elementbook=(Element)document.getElementsByTagName("书").item(0);
book.appendChild(price);
//把更新的内存写回到xml文档中
TransformerFactoryttfactory=TransformerFactory.newInstance();
Transformertf=ttfactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/day2/book.xml")));
}
//向xml文档指定位置上添加节点:<售价>59yuan</售价>
@Test
public void add1()throws Exception {
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdocument=(Document)builder.parse("src/day2/book.xml");
//创建节点
Elementprice=document.createElement("售价");
price.setTextContent("59yuan");
//得到参考节点
ElementrefNode=(Element)document.getElementsByTagName("售价").item(0);
//得到想要挂载的节点//把创建的节点挂到第一本书上
Elementbook=(Element)document.getElementsByTagName("书").item(0);
book.appendChild(price);
//往book节点的指定位置插
book.insertBefore(price,refNode);
//把更新的内存写回到xml文档中
TransformerFactoryttfactory=TransformerFactory.newInstance();
Transformertf=ttfactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/day2/book.xml")));
}
//向xml文档节点中添加属性:<书名 >Java就业培训教程</书名> name="小黄书"
@Test
public void addAtrr()throws Exception {
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
Documentdocument=(Document)builder.parse("src/day2/book.xml");
Elementbookname=(Element)document.getElementsByTagName("书名").item(0);
bookname.setAttribute("name","小黄书");
//把更新的内存写回到xml文档中
TransformerFactoryttfactory=TransformerFactory.newInstance();
Transformertf=ttfactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/day2/book.xml")));
}
//删除节点:<售价>39</售价>
@Test
public void delete()throws Exception {
Documentdocument=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("src/day2/book.xml");
//得到要删除的节点
Elementprice=(Element)document.getElementsByTagName("售价").item(1);
price.getParentNode().removeChild(price);
/*//得到要删除节点的爸爸
Element book=(Element) document.getElementsByTagName("书").item(0);
book.removeChild(price);*/
//把更新的内存写回到xml文档中
TransformerFactoryttfactory=TransformerFactory.newInstance();
Transformertf=ttfactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/day2/book.xml")));
}
//更新售价
@Test
public void update()throws Exception {
Documentdocument=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse("src/day2/book.xml");
//得到要删除的节点
Elementprice=(Element)document.getElementsByTagName("售价").item(1);
price.setTextContent("一百元");
//把更新的内存写回到xml文档中
TransformerFactoryttfactory=TransformerFactory.newInstance();
Transformertf=ttfactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/day2/book.xml")));
}
}
标签:stand parent att 解析 ttf src 位置 parser cto
原文地址:http://www.cnblogs.com/free20019/p/7637075.html