Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-17-2008, 04:44 AM   #1
jrwalker2
Registered User
 
Join Date: Mar 2008
Posts: 14
Default Delete an element from an xml object

Is there a way to delete an xml element from an XML object based on the text of that element?

Example:
--------
<items>
<item>a<item>
<item>b<item>
<item>c<item>
<items>

I want to delete the element that has the value 'b' resulting in the following xml object:

<items>
<item>a<item>
<item>c<item>
<items>
jrwalker2 is offline   Reply With Quote
Old 03-17-2008, 04:56 AM   #2
bahamucio
Registered User
 
Join Date: Feb 2008
Posts: 13
Default

Code:
delete items.item;
or u can just give it new value.
bahamucio is offline   Reply With Quote
Old 03-17-2008, 05:04 AM   #3
jrwalker2
Registered User
 
Join Date: Mar 2008
Posts: 14
Default

How do I specify that I want to delete just <item>b<item>? Because the following code deletes all the <item> tags.

delete items.item;

Last edited by jrwalker2; 03-17-2008 at 05:07 AM.
jrwalker2 is offline   Reply With Quote
Old 03-17-2008, 05:36 AM   #4
bahamucio
Registered User
 
Join Date: Feb 2008
Posts: 13
Default

Code:
var xml:XML;
var xmlList:XMLList; //xmlList is more like indexed array
var xmlReq:String = "xmlfile.xml"; // your xml file
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("xmlfile.xml"));

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
   xml = XML(event.target.data); //gets the data in xml format
   xmlList = xml.children(); //xmlList gets all the childrens (all <item> in this case)
   delete xmlList[1]; //delete the desited element
   trace(xmlList);
}
with this u can delete the element from the xmlList but I think u cannot delete it from the file.

Last edited by bahamucio; 03-17-2008 at 05:54 AM.
bahamucio is offline   Reply With Quote
Old 03-17-2008, 05:56 AM   #5
jrwalker2
Registered User
 
Join Date: Mar 2008
Posts: 14
Default

In my situation, I will not be able to know the index of the element that I want to delete (only its value). Is there a way to do the following? Thanks

var xml:XML;
var xmlList:XMLList; //xmlList is more like indexed array
var xmlReq:String = "xmlfile.xml"; // your xml file
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("xmlfile.xml"));

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
xml = XML(event.target.data); //gets the data in xml format
xmlList = xml.children(); //xmlList gets all the childrens (all <item> in this case)
delete xmlList[index of the <item> element that has 'b' as its value]; //delete the desited element
trace(xmlList);
}
jrwalker2 is offline   Reply With Quote
Old 03-17-2008, 12:04 PM   #6
bahamucio
Registered User
 
Join Date: Feb 2008
Posts: 13
Default

in that case:
Code:
 
var xml:XML = 
        <order>
            <item>Rice</item>
            <item>Kung Pao Shrimp</item>
        </order>;
for each(var i in xml.item)
{
   if(xml.item[i].contains(<item>Rice</item>))
   {
      delete xml.item[i];
   }
}
if u use this
Code:
var xml:XML;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("xmlfile.xml"));

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
   xml = XML(event.target.data); //gets the data in xml format
   for each(var i in xml.item)
   {
     if(xml.item[i].contains(<item>Rice</item>))
     {
      delete xml.item[i];
     }
  }
}
bahamucio is offline   Reply With Quote
Old 03-17-2008, 06:11 PM   #7
jrwalker2
Registered User
 
Join Date: Mar 2008
Posts: 14
Default

Thanks bahamucio! This worked great. The only modification that I had to make was adding a type to the for-each loop variable.

for each(var i:int in xml.item)
{
if(xml.item[i].contains(<item>Rice</item>))
{
delete xml.item[i];
}
}
jrwalker2 is offline   Reply With Quote
Old 03-17-2008, 09:58 PM   #8
jrwalker2
Registered User
 
Join Date: Mar 2008
Posts: 14
Default

Question: The code below deletes the first <item> only, because the loop variable never increments. Is there a way to change this so that I can iterate through all the items so that if I want to delete Kung Pao Shrimp before Rice, I could do it? The loop var i always remains at 0. I tried the following in place of the for-each but without success:

for each(var i:int in xml.children())
jrwalker2 is offline   Reply With Quote
Old 03-18-2008, 12:25 AM   #9
jrwalker2
Registered User
 
Join Date: Mar 2008
Posts: 14
Default

Hi bahamucio...can you help me once more on this (see previous message)? Thanks
jrwalker2 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
exchanging xml between flash and jsp Giotika ActionScript 2.0 2 10-29-2008 11:16 AM
Deleting XML Element Sleeve ActionScript 3.0 6 10-14-2007 06:05 AM
How to delete an object? PowerfulWebsites ActionScript 2.0 18 08-03-2006 08:10 PM
MD5 - Explain ? Leebzie ActionScript 1.0 (and below) 4 03-12-2005 09:22 AM
when delete a line attached object still!!!!! issamneo ActionScript 2.0 5 06-04-2004 06:41 AM


All times are GMT. The time now is 03:11 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.