如何从xml文件中删除节点? [英] How to delete nodes from xml file?

查看:38
本文介绍了如何从xml文件中删除节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Appcelerator 的应用程序操作 XML 文件中的数据.

I want to manipulate a data from an XML file with Appcelerator's app.

所以我可以使用以下代码从这个 xml 文件中读取信息:

So I'm able to read the information from this xml file with this code:

var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'/XML/file.xml'); 
var xmltext = file.read().text;
var doc = Ti.XML.parseString(xmltext); 
var nodes = doc.documentElement.getElementsByTagName("code");
for (var i=0;i<nodes.length;i++) {
    var element = nodes.item(i);
    //LA SECTION CODE DEL SOCIAL HISTORY, DEVE ESSERE
    //29762-2
    if(element.getAttribute('code') == '29762-2'){
        var section = element.parentNode;   
        if(section.nodeName =='section'){
            var entries = section.getElementsByTagName("entry");
        }
    }
}

使用此代码,我可以读取 XML 文件的每个节点.现在我想从这个 XML 文件中删除一些部分.

With this code, I can read every node of my XML file. Now I want to delete some section from this XML file.

我该怎么做?

编辑

这是我用来从 XML 文件中删除子项的代码.

This is the code that I used to remove a child from my XML file.

var section = element.parentNode;   
var entries = section.getElementsByTagName("entry");
for(var e=0; e< entries.length; e++){
   var entry = entries.item(e);
   var nodiRimasti = section.removeChild(entry);
}

如果我尝试检查部分的子节点,我可以看到一个节点被删除.现在我不知道如何确认这个修改,然后保存修改文件.

If I try to inspect on a child of section I can see that one node is removed. Now I don't know how can I confirm this modify, then saved the modify the file.

推荐答案

使用 parseString 后,您将获得一个 XML 文档,其中包含大量可以使用的方法.查看您可以使用的文档.

After you've used parseString you get an XML Document which has tons of methods you can use. Look at the documentation what you can use.

例如,您可以使用 removeChild 方法删除基本上就是您所要求的子元素.

For example, you can use the removeChild method to remove a child element which basically is what you're asking.

删除后,将 XML 文档解析回字符串并使用 文件系统 API 的

Once you've removed it, parse the XML document back to a string and update the file using the filesystem API's

这篇关于如何从xml文件中删除节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆