package com.zhangrui.qa;
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.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class TestJaxp {
public static void main(String[] args) throws Exception {
//selectAll();
//selectSingle();
//add();
//removeN();
selectElement();
}
//遍历
private static void selectElement() throws Exception{
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("src/xml_01.xml");
diGui(document);
}
public static void diGui(Node node){
if(node.getNodeType()==Node.ELEMENT_NODE){
System.out.println(node.getNodeName());
}
NodeList list=node.getChildNodes();
for(int i=0;i<list.getLength();i++){
Node secondNode=list.item(i);
diGui(secondNode);
}
}
private static void removeN() throws Exception{
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("src/xml_01.xml");
NodeList list=document.getElementsByTagName("sex");
Node sex0=list.item(0);
Node parentNode=sex0.getParentNode();
parentNode.removeChild(sex0);
TransformerFactory transformerFactory=TransformerFactory.newInstance();
Transformer transformer=transformerFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src/xml_01.xml"));
}
private static void add() throws Exception{
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("src/xml_01.xml");
NodeList list=document.getElementsByTagName("p2");
Node p1=list.item(0);
Element sex=document.createElement("sex");
Text nv=document.createTextNode("nan");
sex.appendChild(nv);
p1.appendChild(sex);
//把创建的元素节点回写到xml文件中
TransformerFactory transformerFactory=TransformerFactory.newInstance();
Transformer transformer=transformerFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src/xml_01.xml"));
}
private static void selectSingle() throws Exception{
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("src/xml_01.xml");
NodeList list=document.getElementsByTagName("name");
Node node1=list.item(0);
String st=node1.getTextContent();
System.out.println(st);
}
private static void selectAll() throws Exception {
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderFactory.newDocumentBuilder();
Document document=builder.parse("src/xml_01.xml");
NodeList list=document.getElementsByTagName("name");
for(int i=0;i<list.getLength();i++){
Node node=list.item(i);
String str=node.getTextContent();
System.out.println(str);
}
}
} |
|