import javax.xml.transform.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult ;
import javax.xml.transform.stream.StreamSource ;
import javax.xml.transform.dom.*;
import java.util.*;
import java.io.*;
public class DomReadDR{
DocumentBuilderFactory factory;
DocumentBuilder builder;
Document doc;
public DomReadDR()throws Exception{
factory=DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
doc = builder.parse("test.xml");
}
public void Read() throws Exception{
doc.normalize();
NodeList links=doc.getElementsByTagName("song");
for(int i=0;i<links.getLength();i++){
Element link = (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());
}
}
//替换节点
public int replaceNode(String nodeID,String nodeValue){
doc.normalize();
NodeList links=doc.getElementsByTagName("song");
//查找要替换的Node
boolean blfindNode=false;
Element link=null;
for(int i=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)
{
return 1;//没有找到合适的节点
}
try{
TransformerFactory tFactory=TransformerFactory.newInstance();
Transformer transformer=tFactory.newTransformer();
//设置输出的encoding为改变gb2312
transformer.setOutputProperty("encoding","gb2312");
DOMSource source= new DOMSource(doc);
StreamResult result = new StreamResult("test.xml");
transformer.transform(source,result);
}catch(javax.xml.transform.TransformerConfigurationException e){
e.printStackTrace();
System.out.println(e.getMessage());
return 3;//写文件错误
}catch(javax.xml.transform.TransformerException ex){
ex.printStackTrace();
return 3;
}
return 0;//替换成功
}
//删除节点
public int deleteNode(String nodeID){
doc.normalize();
NodeList links=doc.getElementsByTagName("song");
//查找要替换的Node
boolean blfindNode=false;
Element link=null;
for(int i=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(DOMException e){
e.printStackTrace();
return 2;//Dom错误
}
break;
}
}
if (!blfindNode)
{
return 1;//没有找到合适的节点
}
try{
TransformerFactory tFactory=TransformerFactory.newInstance();
Transformer transformer=tFactory.newTransformer();
//设置输出的encoding为改变gb2312
transformer.setOutputProperty("encoding","gb2312");
DOMSource source= new DOMSource(doc);
StreamResult result = new StreamResult("test.xml");
transformer.transform(source,result);
}catch(javax.xml.transform.TransformerConfigurationException e){
e.printStackTrace();
System.out.println(e.getMessage());
return 3;//写文件错误
}catch(javax.xml.transform.TransformerException ex){
ex.printStackTrace();
return 3;
}
return 0;//替换成功
}
//添加节点
public void addNode(String mId,String name,String musicby){
Text textseg;
Element link = doc.createElement("song");
Element emId = doc.createElement("mId");
textseg=doc.createTextNode(mId);
emId.appendChild(textseg);
link.appendChild(emId);
Element ename = doc.createElement("name");
textseg=doc.createTextNode(name);
ename.appendChild(textseg);
link.appendChild(ename);
Element emusicby = doc.createElement("music-by");
textseg = doc.createTextNode(musicby);
emusicby.appendChild(textseg);
link.appendChild(emusicby);
Element root = doc.getDocumentElement();
root.normalize();
root.appendChild(link);
System.out.println(doc.getDoctype());
try{
TransformerFactory tFactory=TransformerFactory.newInstance();
Transformer transformer=tFactory.newTransformer();
//设置输出的encoding为改变gb2312
transformer.setOutputProperty("encoding","gb2312");
DOMSource source= new DOMSource(doc);
StreamResult result = new StreamResult("test.xml");
transformer.transform(source,result);
}catch(javax.xml.transform.TransformerConfigurationException e){
e.printStackTrace();
System.out.println(e.getMessage());
}catch(javax.xml.transform.TransformerException ex){
ex.printStackTrace();
}
}
public static void main(String[] args){
try{
DomReadDR dr = new DomReadDR();
dr.Read();
//dr.addNode("5","卡本特1","yestodayfdgdrtyrtyrtyrtyfgdfgdfgdfg once more");
System.out.println(dr.replaceNode("4","天上人间第死的克己复礼亏损大家法律亏损代理费"));
System.out.println(dr.deleteNode("1"));
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
posted on 2007-05-25 09:21
李大嘴 阅读(4114)
评论(1) 编辑 收藏