Java知识积累3-XML的DOM解析修改和删除方法

import java.io.File;

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.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
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;
import org.xml.sax.SAXException;

public class RemoveNodeTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc=builder.parse(RemoveNodeTest.class.getResourceAsStream("student.xml"));
Element studentElenent=doc.createElement("student");
studentElenent.setAttribute("id", "jredu003");
Element name=doc.createElement("name");
Text nameValue=doc.createTextNode("xiaohuang");
name.appendChild(nameValue);
Element age=doc.createElement("age");
Text ageValue=doc.createTextNode("20");
age.appendChild(ageValue);
studentElenent.appendChild(name);
studentElenent.appendChild(age);
Element root = doc.getDocumentElement();
NodeList studentNodes = root.getChildNodes();
NodeList haha=root.getElementsByTagName("name");
for (int i = 0; i < studentNodes.getLength(); i++) {
Node node = studentNodes.item(i); // 因为空格也算是元素,所以要处理,不输出空格元素
Node node1=haha.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
String id = node.getAttributes().getNamedItem("id")
.getNodeValue();
/* if(id.equals("003")){
doc.getDocumentElement().replaceChild(studentElenent, node); //修改
doc.getDocumentElement().removeChild(node); //删除
}*/
//删除中间的结点方式相同
System.out.println(id);
NodeList childs = node.getChildNodes();

for (int j = 0; j < childs.getLength(); j++) {
Node nodee = childs.item(j);
if (nodee.getNodeType() == Node.ELEMENT_NODE) {
String Aname = nodee.getFirstChild().getNodeValue();
if(Aname.equals("张三")){
nodee.setTextContent("hahah");
nodee.getParentNode().removeChild(nodee); //删除单个中间结点的方法
} //修改中间值的方法
System.out.println(Aname);
} else if (nodee.getNodeName().equals("age")) {
String Aage = nodee.getFirstChild().getNodeValue();
System.out.println(Aage);
}
}

}
}


Transformer transformer=TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(doc),
new StreamResult(new File("c:\\a.xml")));


} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

你可能感兴趣的:(java)