package com.wangzhiquan.xml;
import java.io.BufferedReader;
import java.io.FileReader;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import com.sun.org.apache.xerces.internal.parsers.DOMParser;
public class xmldemo {
public static Document getInstance(String xmlPath){
Document doc = null;
try {
BufferedReader bf = new BufferedReader(new FileReader(xmlPath));
DOMParser ps = new DOMParser();//xml解析器
ps.parse(new InputSource(bf));//解析xml
doc = ps.getDocument();//获取Document对象
} catch(Exception e) {
e.printStackTrace();
}
return doc;
}
public static void printAll(Document doc) {
NodeList nl = doc.getElementsByTagName("*");
Node n;
for(int i=0;i<nl.getLength();i++) {
n = nl.item(i);
System.out.println(n.getNodeName()+"");
}
}
public static void printAttr(Document doc) {
NodeList nl = doc.getElementsByTagName("*");
Element e;
Attr att;//属性对象
NamedNodeMap nnm;
String attrname;
String attrval;
for(int i=0;i<nl.getLength();i++) {
e =(Element)nl.item(i);
System.out.println(e.getTagName()+":"+e.getFirstChild().getNodeValue());
nnm = e.getAttributes();
if(nnm != null) {
for(int j=0;j<nnm.getLength();j++) {
att = (Attr)nnm.item(j);
attrname = att.getName();
attrval = att.getValue();
System.out.println("属性是:"+attrname+"="+attrval);
}
}
}
}
//此为添加方法
public static void addElement(Document dc) {
Element estu = dc.createElement("student");
Element ename = dc.createElement("name");
Element esex = dc.createElement("sex");
Element eage = dc.createElement("age");
Element escore = dc.createElement("score");
Text tname=dc.createTextNode("张三");
Text tsex=dc.createTextNode("男");
Text tage=dc.createTextNode("19");
Text tscore=dc.createTextNode("80");
estu.appendChild(ename).appendChild(tname);
estu.appendChild(esex).appendChild(tsex);
estu.appendChild(eage).appendChild(tage);
estu.appendChild(escore).appendChild(tscore);
dc.getDocumentElement().appendChild(estu);
}
//此为修改方法
public static void updateElement(Document dc,String stname,String stscore){
NodeList nl = dc.getElementsByTagName("name");
Element e;
for(int i=0;i<nl.getLength();i++) {
e = (Element)nl.item(i);
if(e.getFirstChild().getNodeValue().equals(stname)) {
Node n = e.getParentNode();
NodeList nd = n.getChildNodes();
for(int j=0;j<nd.getLength();j++) {
Node node = nd.item(j);
if(node.getNodeName().equals("score")) {
node.getFirstChild().setNodeValue(stscore);
}
}
}
}
}
//此方法为删除方法
public static void delElement(Document dc,String idvalue) {
NodeList nl = dc.getElementsByTagName("student");
Element e;
NamedNodeMap nnm;
Attr att;
for(int i=0;i<nl.getLength();i++) {
e=(Element)nl.item(i);
nnm=e.getAttributes();
if(nnm!=null) {
for(int j=0;j<nnm.getLength();j++) {
att=(Attr)nnm.item(j);
if((att.getName().equals("id"))&&(att.getValue().equals(idvalue))){
(e.getParentNode()).removeChild(e);
}
}
}
}
}
//此方法把程序中所做的操作保存于xml文档中
public static void saveXml(Document dom) {
TransformerFactory tff = TransformerFactory.newInstance();
try {
Transformer tf = tff.newTransformer();
tf.setOutputProperty("encoding","UTF-8");
tf.transform(new DOMSource(dom), new StreamResult("E://xml//xmlExample//src//com//wangzhiquan//xml//student.xml"));
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Document dc = getInstance("E://xml//xmlExample//src//com//wangzhiquan//xml//student.xml");
//注意路径
delElement(dc,"100");
updateElement(dc,"叶青霞","98");
saveXml(dc);
printAttr(dc);
}
}
---------------------------------------------------------------------------------
----student.xml
<?xml version="1.0" encoding="UTF-8"?><studentInfo>
<student>
<name id="100">aa</name>
<age>22</age>
<sex>nan</sex>
<score>85</score>
</student>
<student>
<name id="104">bb</name>
<age>23</age>
<sex>nan</sex>
<score>85</score>
</student>
<student>
<name id="106">cc</name>
<age>21</age>
<sex>nv</sex>
<score>95</score>
</student>
</studentInfo>