如何保存和保存更新xml文件中的值?
我正在从 SD 卡读取 xml 文件。在这里,我想更改 XML 文件的值,并且想将文件保存到 SD 卡。
我的代码如下所示......请指导我如何在更新值后将 XML 文件保存到 SD 卡。 。
public void modifyNodeval(){
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("/sdcard/sss.xml"));
//Get the staff element by tag name directly
Node nodes = doc.getElementsByTagName("Employee").item(0);
//loop the staff child node
NodeList list = nodes.getChildNodes();
for (int i =0; i<list.getLength();i++){
Node node = list.item(i);
//get the salary element, and update the value
if("Emp_Name".equals(node.getNodeName())){
node.setNodeValue("795796");
}
}
I am reading a xml file from the SD card. Here I want to change the values of the XML file and I want to save the file to the sd card..
My code is like below.... Please guide me how to save XML file to the sd card after updating the values..
public void modifyNodeval(){
try{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("/sdcard/sss.xml"));
//Get the staff element by tag name directly
Node nodes = doc.getElementsByTagName("Employee").item(0);
//loop the staff child node
NodeList list = nodes.getChildNodes();
for (int i =0; i<list.getLength();i++){
Node node = list.item(i);
//get the salary element, and update the value
if("Emp_Name".equals(node.getNodeName())){
node.setNodeValue("795796");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西:
Something like this:
如何修改xml节点值?
how to modify xml node value?
该方法将DOM文档写入SD卡上的文件中
如果您想在模拟器中对此进行测试,请确保您的 AVD 映像已使用 SD 卡映像进行设置(在映像创建期间完成)。
This method writes a DOM document to a file on the SD Card
If you want to test this in the emulator, make sure your AVD image is setup with an SD Card image (done during image creation).