E4X provides you with ways to access any data in a XML content, such as: tag names, text content, attribute names and values, comments, or processing instructions.
// variable which stores the XML content var pics:XML = <imglist> <image id="1"> <url>dir/img1.jpg</url> <title>Titlu img1</title> </image> </imglist>; // Add in "datexml" (text area on the Stage) the data from XML datexml.text = 'Root tag: '+ pics.name(); // Name of the root tag datexml.text += '\n ID image: '+ pics.image.@id; // Value of 'id' in <image> datexml.text += '\n Url: '+ pics.image.url; // data in <url> datexml.text += '\n Image title: '+ pics.image.title; // data in <title>- '+=' adds the value of an expression to the value of a variable and assigns the result to the variable.
The expression object.* obtains all the elements (tags) in "object", and with @* you can obtain the values of all the attributes in an element.
// variable which stores the XML content var pics:XML = <imglist> <image id="1"> <url>dir/img1.jpg</url> <title>Title img1</title> </image> </imglist>; // Add in "datexml" (text area on the Stage) the data from XML datexml.text += 'Root tag: '+ pics.localName(); // Name of the root tag // Value of the 'id' attribute of the first child element (<image>) in root datexml.text += '\n ID image: '+ pics.children()[0].attribute('id'); // Data from the <url> element of the first child (<image>) in root datexml.text += '\n Url: '+ pics.children()[0].child('url'); // Data from the second element ([1] <title> ) of the first child (<image>) in root datexml.text += '\n Image Title: '+ pics.children()[0].children()[1];- This code displays in the Flash presentation the same data as the previous example, in the image above.
An instruction that is useful in E4X is the descendant accessor operator, which is written as a double dot (..), it gives the posibility to directly access all the descendants of an object (child nodes, grandchild nodes, ...).
With the "descendant accessor operator" (..) you can get a list with all the elements or attributes with a certain name from all the descendants included in the object to which it is applied.
The XMLList class has a method form of this operator called descendants(). This function behaves the same way as the double dot, but for attributes the method "attribute()" must also be added.
// variable which stores the XML content var pics:XML = <imglist> <image id="1"> <url>dir/img1.jpg</url> <title>Titlu img1</title> </image> <image id="2"> <url>dir/img2.jpg</url> <title>Title pt. img2</title> </image> </imglist>; trace(pics..title); // or trace(pics.descendants('title')); /* Returns all the tags <title> that are in any descendant in "pics" <title>Title img1</title> <title>Title pt. img2</title> */ trace(pics.descendants().attribute("id")); // or trace(pics..@id); // Returns 12 (value of the "id" attributes that are in each descendant in "pics") // Gets the second "id" attribute trace(pics..@id[1]); // 2- The expression after "// or" shows the equivalent way to get the same result.
<!-- Comment -->
<?app Data with instructions ?>
// makes the comments and processing instructions accesible XML.ignoreComments = false; XML.ignoreProcessingInstructions = false; // Create an XML fragment that contains both comments and processing instructions var coursesweb:XML = <site> <!-- https://coursesweb.net --> <?php PHP code processing instructions ?> <!-- Courses and Tutorials --> <courses> <course id="1" title="PHP-MySQL">coursesweb.net/php-mysql/</course> <course id="2" title="JavaScript">coursesweb.net/javascript/</course> <course id="3" title="FlashActionScript">coursesweb.net/flash/</course> </courses> <?php PHP code ?> </site>; // define 2 variables to store the comments and processing instructions found directly in root (the tag <site>) // Variables are Object type because the data is also taken with this type var coms:Object = coursesweb.comments(); var insp:Object = coursesweb.processingInstructions(); // Adds in 'datexml' the first Comment and the second processing instruction datexml.text = coms[0]; datexml.text += '\n'+ insp[1];- Notice that the variables "coms" and "insp" are defined as "Object" type, because in E4X the data is taken and stored as objects, with numeric indexing (starting from 0). The first object can be accessed with the index [0], the second object, with index [1], and so on.
To obtain an XMLList representing all comments and processing instructions within an entire XML tree, use the descendants operator in combination with the properties wildcard, as follows:
instance_xml..*.comments()
instance_xml.descendants().processingInstructions()
- They do not get the comments and processing instructions included directly in the root tag.
theXMLList.(conditional_expression)- For each item in theXMLList, the conditional_expression is executed once. If the conditional_expression yields true for an item, that item is added to an XMLList that is returned after all items have been processed.
// variable which stores the XML content var coursesweb:XML = <site> <!-- https://coursesweb.net --> <?php PHP code processing instructions ?> <!-- Courses and tutorials --> <courses> <course id="1" title="PHP-MySQL">coursesweb.net/php-mysql/</course> <course id="2" title="JavaScript">coursesweb.net/javascript/</course> <course id="3" title="FlashActionScript">coursesweb.net/flash/</course> </courses> <?php PHP code ?> </site>; // create an XMLList instance that contains all the elements from <site> var site:XMLList = coursesweb.* // gets only the "course" tags that have the attribute id>1 var elms:XMLList = site.course.(@id>1); trace(elms); /* In Output it will display: <course id="2" title="JavaScript">coursesweb.net/javascript/</curs> <course id="3" title="FlashActionScript">coursesweb.net/flash/</curs> */ // gets only the tag that has in text the word "flash" var tag1:XMLList = site.course.(text().search("flash") != -1); trace(tag1); // coursesweb.net/flash/ // gets the "title" attribute in the tag that has id<3 and contains the word "php" var tag2:XMLList = site.*.(@id<3 && text().search("php") != -1).@title; trace(tag2); // PHP-MySQL- The expression "site.course.(@id>1)" returns the elements from the tag <course> that has the "id" attribute higher than 1.
Notice that pretty complexe filterings can be created to obtain exactly the elements you want. But to filter a list in which not all the tags have the attribute or child element specified in the filtering condition, you must use the hasOwnProperty(), to verify the existance of that attribute or child-element. Otherwise it returns and error.
For example, the following code returns all the elements in "an_xml" that have the "nr" attribute higher than 7.
an_xml..*.(hasOwnProperty("@nr") && @nr>7)
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net