import java.io.File;
import java.io.FileInputStream;
import javax.imageio.stream.FileImageInputStream;
import javax.xml.crypto.dsig.Transform;
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;
//这里如果你的users是建立在当前文件夹相同的位置,就可以像现在这样写直接就把文件名写成users.xml---》在src同目录下的程序,
//如果不是就要写把文件地址写详细;
public class docs {
@Test
public void dos() throws Exception{
DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document dom=db.parse(new File("users.xml"));
Node root=dom.getFirstChild();
System.out.println(root.getNodeName());
System.out.println("------------------------");
Element en=(Element)dom.getElementsByTagName("user").item(1);//这里是获得第二个user标签
String id=en.getAttribute("id");//这里是获取标签的属性
System.out.println(id);
String age=en.getElementsByTagName("age").item(0).getTextContent();//这里是获取age标签里面的文本内容
System.out.println("age="+age);
System.out.println("<------------------------>");
}
private final String FILE_NAME="users.xml";
//修改;
@Test
public void dess() throws Exception{
DocumentBuilder dbs=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document dom=dbs.parse(new FileInputStream(FILE_NAME));
Element el=(Element) dom.getFirstChild();
NodeList list=el.getElementsByTagName("user");
Element es=(Element)list.item(list.getLength()-1);
es.getElementsByTagName("age").item(0).setTextContent("60");
//setTextContent这里是设置文本内容;
System.out.println("value="+ es.getElementsByTagName("age").item(0).getTextContent());
//这里是将改动过发dom树存储到外面的磁盘文件
Transformer tfm=TransformerFactory.newInstance().newTransformer();
tfm.transform( new DOMSource(dom),new StreamResult(FILE_NAME));
}
//这里是增加结点
@Test
public void creat() throws Exception{
DocumentBuilder bdg= DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document dm= bdg.parse(new FileInputStream(FILE_NAME));//这里是获取了xml文档对象
Element root=(Element) dm.getFirstChild();
Element eUser=dm.createElement("user");
eUser.setAttribute("id", "A009");
Element ename=dm.createElement("name");
ename.setTextContent("小敏");
Element eage=dm.createElement("age");
eage.setTextContent("22");
//构建一个user子树
eUser.appendChild(ename);
eUser.appendChild(eage);
//将user子树挂到users上去;
root.appendChild(eUser);
Transformer tfd=TransformerFactory.newInstance().newTransformer();
tfd.transform(new DOMSource(dm), new StreamResult( FILE_NAME));
}
//删除最后一个结点;
@Test
public void delect() throws Exception{
DocumentBuilder dom=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document dm=dom.parse(FILE_NAME);
Element root=(Element) dm.getFirstChild();
NodeList list=root.getElementsByTagName("user");
Node eUser=list.item(list.getLength()-1);
eUser.getParentNode().removeChild(eUser);
Transformer tfg=TransformerFactory.newInstance().newTransformer();
tfg.transform(new DOMSource(dm), new StreamResult(FILE_NAME));
}
}