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

查看:402
本文介绍了如何从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.

我该怎么做?

EDIT

这是我用来从XML文件中删除子代的代码。 / p>

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);
}

如果我尝试检查section的子节点,我可以看到一个节点已移除。现在,我不知道如何确认此修改,然后保存修改文件。

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天全站免登陆