import java.io.FileOutputStream;
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;
//使用dom方式对xml文档进行crud
public class Demo2 {
@Test
public void read() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
NodeList list = document.getElementsByTagName("书名");
Node node = list.item(1);
String content = node.getTextContent();
System.out.println(content);
}
@Test
public void read2() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.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 x=0;x<list.getLength()-1;x++){
Node child = list.item(x);
list(child);
//System.out.println(child.getTextContent());
}
}
//得到xml文档中标签属性值:<书名 name="xxxx">Java就业培训教程</书名>
@Test
public void read3() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
//得到根节点
Element bookname = (Element) document.getElementsByTagName("书名").item(0);
String value = bookname.getAttribute("name");
System.out.println(value);
}
//向xml文档中添加节点:<售价>59.00元</售价>
@Test
public void add() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.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();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
}
//向xml文档中指定位置上添加节点:<售价>59.00元</售价>
@Test
public void add2() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
//创建节点
Element price =document.createElement("售价");
price.setTextContent("59.00元");
//得到参考节点
Element refNode = (Element) document.getElementsByTagName("作者").item(1);
//得到要挂载的节点
Element book = (Element) document.getElementsByTagName("书").item(1);
//往book节点的指定位置插崽
book.insertBefore(price, refNode);
//把更新后内存写回xml中
TransformerFactory tffactory = TransformerFactory.newInstance();
Transformer tf = tffactory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
}
//向xml文档节点上添加属性:<书名>java就业培训教程</书名>上添加name="zero"属性
@Test
public void addAttr() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
Element bookname = (Element) document.getElementsByTagName("书名").item(0);
bookname.setAttribute("name", "zero");
//把更新后内存写回xml中
TransformerFactory tffactory = TransformerFactory.newInstance();
Transformer tf = tffactory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
}
@Test
public void delete() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
//得到要删除节点
Element e = (Element) document.getElementsByTagName("售价").item(1);
System.out.println(e);
//得到要删除的节点
Element book = (Element) document.getElementsByTagName("书").item(1);
System.out.println(book);
//父节点删子节点
book.removeChild(e);
//把更新后内存写回xml中
TransformerFactory tffactory = TransformerFactory.newInstance();
Transformer tf = tffactory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
}
//delete
@Test
public void delete2() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
//得到要删除节点
Element e = (Element) document.getElementsByTagName("售价").item(1);
e.getParentNode().getParentNode().removeChild(e.getParentNode());
//把更新后内存写回xml中
TransformerFactory tffactory = TransformerFactory.newInstance();
Transformer tf = tffactory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
}
//update
@Test
public void update() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/book.xml");
Element e = (Element) document.getElementsByTagName("售价").item(1);
e.setTextContent("168元");
//把更新后内存写回xml中
TransformerFactory tffactory = TransformerFactory.newInstance();
Transformer tf = tffactory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream("src/book.xml")));
}
}