简体   繁体   English

如何从xml文件中删除节点?

[英]How to delete nodes from xml file?

I want to manipulate a data from an XML file with Appcelerator's app. 我想使用Appcelerator的应用程序处理XML文件中的数据。

So I'm able to read the information from this xml file with this code: 因此,我可以使用以下代码从此xml文件中读取信息:

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

With this code, I can read every node of my XML file. 使用此代码,我可以读取XML文件的每个节点。 Now I want to delete some section from this XML file. 现在,我想从此XML文件中删除某些部分。

How can I do this? 我怎样才能做到这一点?

EDIT 编辑

This is the code that I used to remove a child from my XML file. 这是我用来从XML文件中删除子级的代码。

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. 现在,我不知道如何确认此修改,然后保存修改文件。

After you've used parseString you get an XML Document which has tons of methods you can use. 使用parseString之后,您将获得一个XML文档,其中包含大量可以使用的方法。 Look at the documentation what you can use. 查看文档 ,您可以使用什么。

For example, you can use the removeChild method to remove a child element which basically is what you're asking. 例如,您可以使用removeChild方法删除基本上要求的子元素。

Once you've removed it, parse the XML document back to a string and update the file using the filesystem API's 删除后,将XML文档解析回字符串,然后使用文件系统 API的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM