得到代表文档的document对象
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class demo {
public static void main(String[] args) throws Exception {
//创建工厂
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
//解析器
DocumentBuilder builder= builderFactory.newDocumentBuilder();
//document对象
Document document= builder.parse("day01/book.xml");
}
}
增删该查
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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;
import org.xml.sax.SAXException;
public class demo2 {
//利用递归遍历每一个节点
public void read()throws Exception {
DocumentBuilderFactory builderFactory= DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("day01/book.xml");
Node root=document.getElementsByTagName("书架").item(0);
list(root);
}
private void list(Node root) {
// TODO Auto-generated method stub
System.out.println(root.getNodeName());
//得到所有儿子节点
NodeList list=root.getChildNodes();
for(int i=0;i<list.getLength();i++)
{
//每一个儿子节点
if(list.item(i)instanceof Element)
{
System.out.println(list.item(i));
list(list.item(i));
}
}
}
//
public void add() throws Exception
{
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("day01/book.xml");
//创建节点
Node node=document.createElement("售价");
node.setTextContent("100");
Node bookName=document.getElementsByTagName("书名").item(0);
//增加节点
bookName.getParentNode().appendChild(node);
//更新
TransformerFactory factory=TransformerFactory.newInstance();
Transformer transformer=factory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream("day01/book.xml")));
}
//增加指定位置的节点,增加两步
public void add2() throws Exception
{
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("day01/book.xml");
//参考节点
Node canNode=document.getElementsByTagName("作者").item(0);
Node node=document.createElement("售价");
node.setTextContent("100");
Node bookName=document.getElementsByTagName("书").item(0);
bookName.getParentNode().appendChild(node);
//调用inserBefore
bookName.insertBefore(node, canNode);
//
TransformerFactory factory=TransformerFactory.newInstance();
Transformer transformer=factory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream("day01/book.xml")));
}
//增加属性,得到属性
@Test
public void addAttribute() throws Exception
{
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("day01/book.xml");
Element book=(Element) document.getElementsByTagName("书").item(0);
String name="评价";
String value="好书啊";
book.setAttribute(name, value);
String s=book.getAttribute("评价");
System.out.println(s);
TransformerFactory factory=TransformerFactory.newInstance();
Transformer transformer=factory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream("day01/book.xml")));
}
//删除标签
@Test
public void delete() throws Exception
{
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("day01/book.xml");
Element book=(Element) document.getElementsByTagName("作者").item(0);
book.getParentNode().removeChild(book);
TransformerFactory factory=TransformerFactory.newInstance();
Transformer transformer=factory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream("day01/book.xml")));
}
}
得到代表文档的document对象
36ppt
更新
40ppt