xml学习笔记(4)使用Jaxp对xml文档进行dom解析
1.dom解析方式:
DOM(Document Object Model,即文档对象模型),是W3C组织推荐的解析XML的一种方式。
在使用DOM解析时,需要读取整个XML文档,在内存中构架代表整个DOM树的Document对象,从而再对XML文档进行操作。
优点: 对XML文档CRUD比较方便.
缺点: 如果XML文档比较大,内存占用就比较大,容易导致内存溢出。
2.调整jvm大小:
如果xml文件比较大,又需要做增删改,需要使用dom方式解析,可能需要调整虚拟机jvm内存大小。
jvm默认64M内存
eclipse: java程序点右键菜单Run As/Open Run Dialog,Arguments选项,VM arguments输入:-Xmx80m (设置内存为80M).
3.使用Jaxp对xml文档进行dom解析
Jaxp开发包是JAVASE的一部分,它由javax.xml、org.w3c.dom、org.xml.sax包及其子包组成。
在javax.xml.parsers包中,定义了几个工厂类,程序员调用这些工厂类,可以得到xml文档的DOM或SAX的解析器,从而实现对xml文档的解析。
·javax.xml.parsers包中的DocumentBuilderFactory,用于创建DOM模式的解析器对象,
·DocumentBuilderFactory是一个抽象工厂类,它不能直接实例化,
·但该类提供了一个newInstance方法,这个方法会根据本地平台默认安装的解析器,自动创建一个工厂的对象并返回。
4.使用jaxp对xml进行增删改查的案例:
public class demo{
public static void main(String[] args) throws Exception{
//1.创建工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.得到dom解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//3.解析xml稳当,得到代表文档的document.
Document document = builder.parse("src/book.xml");
}
//读一个节点
public void read1(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
NodeList list = document.getElementsByTacName("书名");
Node node = list.item(1);
String content = node.getTextContent();
System.out.println(content);
}
//遍历节点
public void read2(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
Node root = document.getElementsByTacName("书架").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=0;i<list.getLength(),i++){
Node child = list.item(i);
list(child);
}
}
//得到标签属性值.
public void read3(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
Element bookname = (Element) document.getElementsByTacName("书名").item(0);
String value = bookname.getAttribute("name");
System.out.println(value);
}
//在xml文档中添加节点.
public void add(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
//创建节点
Element price = document.createElements("售价");
price.setTextContent("59.00元");
//把创建的节点挂到一个节点下.
Element book = (Element) document.getElementsByTacName("书名").item(0);
book.appendChild(price);
//把更新后的内存写回到xml文档.
TransformerFactory tffactory = TransformerFactory.newInstance();
Transforment tf = tffactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml"));
}
//删除节点.
public void delete(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
//得到要删除的节点
Element e = (Element) document.getElementsByTacName("售价").item(0);
//得到要删除节点的父节点
Element book = (Element) document.getElementsByTacName("书").item(0);
book.removeChild(e);
//或者 e.getParentNode().removeChild(e);
//把更新后的内存写回到xml文档.
TransformerFactory tffactory = TransformerFactory.newInstance();
Transforment tf = tffactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml"));
}
//更新节点.
public void delete(){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
//得到节点
Element e = (Element) document.getElementsByTacName("售价").item(0);
e.setTextContent("109元")
//把更新后的内存写回到xml文档.
TransformerFactory tffactory = TransformerFactory.newInstance();
Transforment tf = tffactory.newTransformer();
tf.transform(new DOMSource(document),new StreamResult(new FileOutputStream("src/book.xml"));
}
}