使用java如何修改某个xml文件中的某项内容 有现成的源代码吗?因为我对xml一窍不通,但还要尽快完成任务。我没时间研究啦。谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用你先去搞个JDOM的包,让后运行我写的代码 ,假设你的配置文件文件名为test.xml是放在C盘根目录下的,代码如下:import org.jdom.*;import org.jdom.output.*;import org.jdom.input.*;import java.io.*;public class xml{ public void toXml() throws JDOMException,IOException{ SAXBuilder saxBuilder=new SAXBuilder(false); saxBuilder.setExpandEntities(false); File file = new File("c:\\test.xml"); Document doc =saxBuilder.build(new File("c:\\test.xml")); Element elem=doc.getRootElement(); //System.out.println(elem.toString()); elem.getChild("Collectors").getChild("Collector").getAttribute("HostIP").setValue("192.168.0.1"); elem.getChild("Collectors").getChild("Collector").getAttribute("PortID").setValue("100000"); Element elem1 = (Element)elem.clone(); Document Doc=new Document(elem1); XMLOutputter XMLOut = new XMLOutputter(); XMLOut.setEncoding("BIG5"); XMLOut.setNewlines(true); file.delete(); XMLOut.output(Doc,new FileOutputStream("c:\\test"+".xml")); } public static void main(String args[]){ xml x = new xml(); try{ x.toXml(); }catch(Exception e){} }}别忘了给分哦~:) 用jdom类似的问题用google搜索一下就有了 eclipse设置格式化代码样式?? 我用hdmi线连接电视无信号 哪里有免费,好用的JSP论坛源代码下载啊? 关于用记事本写程序的问题! 求救jbuilder7的序列号 怎样用JAVA得到中文姓名的首字母缩写?李四返回的结果为LS 快来看!!jcom2.23(java-com bridge) + websphere3.5 + 线程的问题。 数据库的问题在线等待 几个小问题? 问个最基本的问题,int的最大值为多少?65535? 如何调用外部的.jar文件 没分的兄弟可以借我的帖子问问题.
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
public class xml{
public void toXml() throws JDOMException,IOException{
SAXBuilder saxBuilder=new SAXBuilder(false);
saxBuilder.setExpandEntities(false);
File file = new File("c:\\test.xml");
Document doc =saxBuilder.build(new File("c:\\test.xml"));
Element elem=doc.getRootElement();
//System.out.println(elem.toString());
elem.getChild("Collectors").getChild("Collector").getAttribute("HostIP").setValue("192.168.0.1");
elem.getChild("Collectors").getChild("Collector").getAttribute("PortID").setValue("100000");
Element elem1 = (Element)elem.clone(); Document Doc=new Document(elem1);
XMLOutputter XMLOut = new XMLOutputter();
XMLOut.setEncoding("BIG5");
XMLOut.setNewlines(true);
file.delete();
XMLOut.output(Doc,new FileOutputStream("c:\\test"+".xml"));
}
public static void main(String args[]){ xml x = new xml();
try{
x.toXml();
}catch(Exception e){}
}
}别忘了给分哦~:)