java XML
packagecom.itztc.demo;
importjavax.xml.transform.dom.*;
importjavax.xml.parsers.*;
importorg.xml.sax.*;
importorg.xml.sax.helpers.*;
importorg.w3c.dom.*;
importjavax.xml.transform.*;
importjavax.xml.transform.stream.StreamResult;
importjavax.xml.transform.stream.StreamSource;
importjavax.xml.transform.dom.*;
importjava.util.*;
importjava.io.*;
publicclassDomReadDR{
DocumentBuilderFactoryfactory;
DocumentBuilderbuilder;
Documentdoc;
publicDomReadDR()throwsException{
factory=DocumentBuilderFactory.newInstance();
builder=factory.newDocumentBuilder();
//Return:DocumentMethod:parse(Filef)
//将给定文件的内容解析为一个XML文档,并且返回一个新的DOMDocument对象
doc=builder.parse("test.xml");
}
publicvoidRead()throwsException{
doc.normalize();
NodeListlinks=doc.getElementsByTagName("song");
for(inti=0;i<links.getLength();i++){
Elementlink=(Element)links.item(i);
System.out.println(link.getElementsByTagName("name").item(0)
.getFirstChild().getNodeValue());
System.out.println(link.getElementsByTagName("music-by").item(0)
.getFirstChild().getNodeValue());
}
}
//替换节点
publicintreplaceNode(StringnodeID,StringnodeValue){
doc.normalize();
NodeListlinks=doc.getElementsByTagName("song");
//查找要替换的Node
booleanblfindNode=false;
Elementlink=null;
for(inti=0;i<links.getLength();i++){
link=(Element)links.item(i);
if((link.getElementsByTagName("mId").item(0).getFirstChild()
.getNodeValue()).trim().equals(nodeID)){
blfindNode=true;
link.getElementsByTagName("music-by").item(0).getFirstChild()
.setNodeValue(nodeValue);//替换node的内容
break;
}
}
if(!blfindNode){
return1;//没有找到合适的节点
}
try{
TransformerFactorytFactory=TransformerFactory.newInstance();
Transformertransformer=tFactory.newTransformer();
//设置输出的encoding为改变gb2312
transformer.setOutputProperty("encoding","gb2312");
DOMSourcesource=newDOMSource(doc);
StreamResultresult=newStreamResult("test.xml");
transformer.transform(source,result);
}catch(javax.xml.transform.TransformerConfigurationExceptione){
e.printStackTrace();
System.out.println(e.getMessage());
return3;//写文件错误
}catch(javax.xml.transform.TransformerExceptionex){
ex.printStackTrace();
return3;
}
return0;//替换成功
}
//删除节点
publicintdeleteNode(StringnodeID){
doc.normalize();
NodeListlinks=doc.getElementsByTagName("song");
//查找要替换的Node
booleanblfindNode=false;
Elementlink=null;
for(inti=0;i<links.getLength();i++){
link=(Element)links.item(i);
if((link.getElementsByTagName("mId").item(0).getFirstChild()
.getNodeValue()).trim().equals(nodeID)){
blfindNode=true;
try{
link.getParentNode().removeChild(link);
}catch(DOMExceptione){
e.printStackTrace();
return2;//Dom错误
}
break;
}
}
if(!blfindNode){
return1;//没有找到合适的节点
}
try{
TransformerFactorytFactory=TransformerFactory.newInstance();
Transformertransformer=tFactory.newTransformer();
//设置输出的encoding为改变gb2312
transformer.setOutputProperty("encoding","gb2312");
DOMSourcesource=newDOMSource(doc);
StreamResultresult=newStreamResult("test.xml");
transformer.transform(source,result);
}catch(javax.xml.transform.TransformerConfigurationExceptione){
e.printStackTrace();
System.out.println(e.getMessage());
return3;//写文件错误
}catch(javax.xml.transform.TransformerExceptionex){
ex.printStackTrace();
return3;
}
return0;//替换成功
}
//添加节点
publicvoidaddNode(StringmId,Stringname,Stringmusicby){
Texttextseg;
Elementlink=doc.createElement("song");
ElementemId=doc.createElement("mId");
textseg=doc.createTextNode(mId);
emId.appendChild(textseg);
link.appendChild(emId);
Elementename=doc.createElement("name");
textseg=doc.createTextNode(name);
ename.appendChild(textseg);
link.appendChild(ename);
Elementemusicby=doc.createElement("music-by");
textseg=doc.createTextNode(musicby);
emusicby.appendChild(textseg);
link.appendChild(emusicby);
Elementroot=doc.getDocumentElement();
root.normalize();
root.appendChild(link);
System.out.println(doc.getDoctype());
try{
TransformerFactorytFactory=TransformerFactory.newInstance();
Transformertransformer=tFactory.newTransformer();
//设置输出的encoding为改变gb2312
transformer.setOutputProperty("encoding","gb2312");
DOMSourcesource=newDOMSource(doc);
StreamResultresult=newStreamResult("test.xml");
transformer.transform(source,result);
}catch(javax.xml.transform.TransformerConfigurationExceptione){
e.printStackTrace();
System.out.println(e.getMessage());
}catch(javax.xml.transform.TransformerExceptionex){
ex.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
try{
DomReadDRdr=newDomReadDR();
dr.Read();
//dr.addNode("5","卡本特1","yestodayfdgdrtyrtyrtyrtyfgdfgdfgdfgoncemore");
System.out.println(dr.replaceNode("5","天上人间第死的克己复礼亏损大家法律亏损代理费"));
System.out.println(dr.deleteNode("1"));
}catch(Exceptione){
System.out.println(e.getMessage());
}
}
}