Besides the posibility to create, load and access XML data, E4X also has functions to edit, change and delete the XML data in ActonScript.

Changing the Contents of an Element and an Attribute Value

The content of an element and the value of an attribute can be easily changed by accesing these elements with the dot (.) operator, when the structure of the XML content is well known.
Just access the respective element /attribute, and assign that element any new value (other than an XMLList or XML object). The value is converted to a string and replaces the element's content.
See this example:

// define an XML instance to store the XML content
var marplo:XML = <site>
    <courses>
      <course id="1" title="PHP-MySQL">http://coursesweb.net/php-mysql/</course>
      <course id="2" title="JavaScript">http://coursesweb.net/javascript/</course>
      <course id="3" title="Flash ActionScript">http://coursesweb.net/flash/</course>
    </courses>
  </site>;

// modify the content in the second tag <course> (in <courses>)
marplo.courses.course[1] = 'www.marplo.net/engleza/';

// Modify the "title" attribute in the second tag <course>
marplo.courses.course[1].@title = 'English Language';

// Verify if the data was changed
trace(marplo);

/* It will display:
  <site>
    <courses>
      <course id="1" title="PHP-MySQL">http://coursesweb.net/php-mysql/</course>
      <course id="2" title="Limba Engleza">www.marplo.net/engleza/</course>
      <course id="3" title="Flash ActionScript">http://coursesweb.net/flash/</course>
    </courses>
  </site>
*/
- See the comments in code.

• Tthe content of an element and the value of an attribute can also be changed using the methods of the XML class.
Here's the same changes to the XML content, but this time using XML class's methods.
// define an XML instance to store the XML content
var marplo:XML = <site>
    <courses>
      <course id="1" title="PHP-MySQL">http://coursesweb.net/php-mysql/</course>
      <course id="2" title="JavaScript">http://coursesweb.net/javascript/</course>
      <course id="3" title="Flash ActionScript">http://coursesweb.net/flash/</course>
    </courses>
  </site>;

// Add in a XMLList instance the element that will be modified
  // (it is easier when you work multiple times with the same element)
// the tag "course" in the first child of the root tag
var curss:XMLList = marplo.children()[0].child('course');

// Modify the content of the second element stored in the variable "curss" 
curss[1] = 'www.marplo.net/engleza/';

// Change the value of the 'title' attribute in the second element stored in the variable "curss"
curss[1].attribute("title")[0] = 'English Language';

// Verify if the data changed
trace(marplo);
- In Output panel will display the same result as the first example.

Either of these two options is good (the dot operator and @ character, or XML class methods), depends on how well the content and hierarchy of the XML format is known, as well as the context in which the modifications are made.
They can also be combined, for example:       marplo.*.child('course')[1].@title = 'English Language';

Adding new tags and attributes

When new tags must be added in a XML object, the XML class methods are usually used (or combined with the (.) operator), because the XML methods offer a better control over the place where the item is addded (before or after other existing elements in an object).
For attributes, if their order in the tag doesn't matter, they are added mostly by using the (.) dot operator and the character '@'.


in the following example we add a new <course> tag, at the beginning of the elements in <courses> (with the "prependChild()" method). Then, with a "for()" instruction we iterate over every <course> tags, and add to each one a "nr" attriburte (see the explanations in code).
// define an XML instance to store the XML content
var marplo:XML = <site>
    <courses>
      <course id="1" title="PHP-MySQL">http://coursesweb.net/php-mysql/</course>
      <course id="2" title="JavaScript">http://coursesweb.net/javascript/</course>
      <course id="3" title="Flash ActionScript">http://coursesweb.net/flash/</course>
    </courses>
  </site>;

// Create a new XML instance with the new "course" element, then add it in <courses>
var new_course:XML = <course id="0" title="HTML">www.marplo.net/html/</course>;
marplo.courses[0].prependChild(new_course);

// Add in a XMLList instance all the elements with the tags <course>
var curss:XMLList = marplo.courses[0].course;

// define a for() loop to iterate over every item in "curss"
for(var i:int=0; i<curss.length(); i++)
{
  // Add a "nr" attribute, with the value of "i"
  curss[i].@nr = i;
}

trace(marplo);
- In output displays the following XML format (notice the new <course> tag and the "nr" attribute):.
<site>
    <courses>
        <course id="0" title="HTML" nr="0">www.marplo.net/html/</course>
        <course id="1" title="PHP-MySQL" nr="1">http://coursesweb.net/php-mysql/</course>
        <course id="2" title="JavaScript" nr="2">http://coursesweb.net/javascript/</course>
        <course id="3" title="Flash ActionScript" nr="3">http://coursesweb.net/flash/</course>
    </courses>
</site>

• The XML class's methods used to add new elements are:

Deleting elements and attributes

To delete elements and attributes from an XML object, use the delete operator as follows:
delete XMLobject.element_or_attribute
- For example:

delete xmlObj.elm[0];              // Delete the first tag <elm> in the root
delete xmlObj.elm[0].tag[1];       // Delete the second <tag> in the first <elm> in root
delete xmlObj.elm[0].@atr;         // Delete the "atr" attribute of the first tag <elm> in root
delete xmlObj.elm.tag.@atr;        // Delete the attribute "atr" of all the elements <tag> included in each <elm> in root
delete xmlObj.*;                   // Delete all the elements
delete xmlObj.*.tag.@*;            // Delete all the attributes of each element <tag>


In the following example we delete the last <course> tag and the "id" attribute from all the "course" elements.
// define an XML instance to store the XML content
var marplo:XML = <site>
    <courses>
      <course id="1" title="PHP-MySQL">http://coursesweb.net/php-mysql/</course>
      <course id="2" title="JavaScript">http://coursesweb.net/javascript/</course>
      <course id="3" title="Flash ActionScript">http://coursesweb.net/flash/</course>
    </courses>
  </site>;

// gets the number of <course> elements in <courses> (to know the index of the last element)
var nr_c:* = marplo.courses[0].course.length();

// Delete the last <course> tag (its index is [nr_c-1])
delete marplo.courses.course[nr_c-1];

// Delete the "id" attribute from all the <course> tags
delete marplo.courses.course.@id;

trace(marplo);
- After applying the delete instructions, the remained XML content (displayed in Output) is:
<site>
    <courses>
        <course title="PHP-MySQL">http://coursesweb.net/php-mysql/</course>
        <course title="JavaScript">http://coursesweb.net/javascript/</course>
    </courses>
</site>

• The editing operations can be also combined with filtering instructions.
For example:
            delete marplo.courses.course.(hasOwnProperty('@id') && @id==2).*;
- Delete the content of the "course" element which has the attribute id="2".

The following instruction:
            marplo.*.course.(hasOwnProperty('@id') && @id==2).setChildren(<newtag>Text</newtag>);
- Replaces the content of the "course" element which has the attribte id="2" with "<newtag>Text</newtag>".

• To convert the XML data into a String object, use the toXMLString() method as folows:
ObiectXML.toXMLString()
- This method allows to use the XML content as a String.

• To convert a String with XML data into a XML object, add the string as argument to a new XML instance:
var ObiectXML:XML = new XML(stringXMLdata);

To download the FLA file with the examples from this lesson, click: Editing, Changing XML - E4X.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag create a highlighted bolded text?
<q> <strong> <em>
<p>Address: <strong>http://CoursesWeb.net/</strong> - Tutorials.</p>
Which of these CSS codes displays the text bolded?
text-size: 18px; font-style: italic; font-weight: 800;
#id {
  font-weight: 800;
}
What JavaScript function can be used to call another function multiple times, to a specified time interval?
setInterval() setTimeout() push()
function someFunction() { alert("CoursesWeb.net"); }
setInterval("someFunction()", 2000);
Click on the correctly defined variable in PHP.
var vname = 8; $vname = 8; $vname == 8;
$vname = 8;
echo $vname;
Editing, Changing XML - E4X

Last accessed pages

  1. Bubbles3 (25088)
  2. Zuma Honey (3507)
  3. Ajax-PHP Chat Script (29240)
  4. Backgammon (5617)
  5. CSS3 - new Background properties (937)

Popular pages this month

  1. Qwop (4286)
  2. Bubbles3 (3791)
  3. Butterfly Kyodai (2697)
  4. Zuma Deluxe (2492)
  5. Backgammon (1288)