下面写了个对XML文档进行添
删
改
查的程序
先是xml文档代码如下:
<?xml version= encoding=utf?><studentInfo>
<student>
<name id=>aa</name>
<age></age>
<sex>男</sex>
<score></score>
</student>
<student>
<name id=>bb</name>
<age></age>
<sex>女</sex>
<score></score>
</student>
<student>
<name id=>cc</name>
<age></age>
<sex>女</sex>
<score></score>
</student>
</studentInfo>
然后就是java应用程序注解已说明首先要在外面下载个包xercesjar(当然也可用Eclipse中自带的只是代码稍稍不同)代码如下:
package comDome;
import javaioBufferedReader;
import javaioFileReader;
import javaxxmltransformTransformer;
import javaxxmltransformTransformerFactory;
import javaxxmltransformdomDOMSource;
import javaxxmltransformstreamStreamResult;
import orgapachexercesparsersDOMParser;
import orgwcdomAttr;
import orgwcdomDocument;
import orgwcdomElement;
import orgwcdomNamedNodeMap;
import orgwcdomNode;
import orgwcdomNodeList;
import orgwcdomText;
import orgxmlsaxInputSource;
public class DomeTest {
/*
* 此方法为得到Document对象实例
*/
public static Document getInstance(String xmlPath) {
Document doc = null;
try {
BufferedReader bf = new BufferedReader(new FileReader(xmlPath));
DOMParser ps = new DOMParser();// xml解析器
psparse(new InputSource(bf));// 解析xml
doc = psgetDocument();// 获得Document对象
} catch (Exception e) {
eprintStackTrace();
}
return doc;
}
/*
* 此方法用来打印元素等
*/
public static void printAll(Document doc) {
NodeList nl = docgetElementsByTagName(*);
Node n;
for (int i = ; i < nlgetLength(); i++) {
n = em(i);
Systemoutprintln(ngetNodeName() + );
}
}
/*
* 此方法用来打印属性
*/
public static void printAttr(Document doc) {
NodeList nl = docgetElementsByTagName(*);
Element e;
Attr att;// 属性对象
NamedNodeMap nnm;// 属性对象集合
String attrname;
String attrval;
for (int i = ; i < nlgetLength(); i++) {
e = (Element) em(i);
Systemoutprintln(egetTagName() + :
+ egetFirstChild()getNodeValue());
nnm = egetAttributes();
if (nnm != null) {
for (int j = ; j < nnmgetLength(); j++) {
att = (Attr) em(j);
attrname = attgetName();
attrval = attgetValue();
Systemoutprintln(属性是 + attrname + = + attrval);
}
}
}
}
/*
* 此为添加方法
*/
public static void addElement(Document dc) {
Element estu = dccreateElement(student);
Element ename = dccreateElement(name);
Element esex = dccreateElement(sex);
Element eage = dccreateElement(age);
Element escore = dccreateElement(score);
Text tname = dccreateTextNode(张三);
Text tsex = dccreateTextNode(男);
Text tage = dccreateTextNode();
Text tscore = dccreateTextNode();
estuappendChild(ename)appendChild(tname);
estuappendChild(esex)appendChild(tsex);
estuappendChild(eage)appendChild(tage);
estuappendChild(escore)appendChild(tscore);
dcgetDocumentElement()appendChild(estu);
}
/*
* 此为修改方法
*/
public static void updateElement(Document dc String stname String stscore) {
NodeList nl = dcgetElementsByTagName(name);
Element e;
for (int i = ; i < nlgetLength(); i++) {
e = (Element) em(i);
if (egetFirstChild()getNodeValue()equals(stname)) {
Node n = egetParentNode();
NodeList nd = ngetChildNodes();
for (int j = ; j < ndgetLength(); j++) {
Node node = em(j);
if (nodegetNodeName()equals(score)) {
nodegetFirstChild()setNodeValue(stscore);
}
}
}
}
}
/*
* 此方法为删除方法
*/
public static void delElement(Document dc String idValue) {
NodeList nl=dcgetElementsByTagName(student);
Element e;
NamedNodeMap nnm;//属性集合
Attr att;
for(int i=;i<nlgetLength();i++){
e=(Element)em(i);
nnm=egetAttributes();
if(nnm!=null){
for(int j=;j<nnmgetLength();j++){
att=(Attr)em(j);
if((attgetName()equals(id))&&(attgetValue()equals(idValue))){
(egetParentNode())removeChild(e);
}
}
}
}
}
/*
* 此方法把在程序中所做的操作保存于xml文档中
*/
public static void saveXml(Document dom) {
TransformerFactory tff = TransformerFactorynewInstance();
try {
Transformer tf = tffnewTransformer();
tfsetOutputProperty(encoding utf);
tftransform(new DOMSource(dom) new StreamResult(
E:\\xml\\xmlExample\\studentxml));
} catch (Exception e) {
eprintStackTrace();
}
}
/**
* main方法
*/
public static void main(String[] args) {
Document dc = getInstance(E:\\xml\\xmlExample\\studentxml);
// printAll(dc);
// addElement(dc);
//updateElement(dc aa );
delElement(dc);
saveXml(dc);
printAttr(dc);
}
}