Entry
How can I access the child nodes of an XML object representing an element?
How can I access the child element nodes of an XML object representing an element?
How can I access the child element nodes of an XML object representing an element?
Mar 24th, 2009 20:06
chat alarab, Martin Honnen,
You can access the child element nodes of an XML object representing an
element the same way you access properties of a normal object in
JavaScript, using the dot notation
object.propertyName
or the bracket notation
object['propertyName']
Thus if you have the following XML object:
var xmlObject = <god>
<name>Kibo</name>
<power>42</power>
<home>http://www.kibo.com/</home>
</god>;
then you can access the child element nodes as follows:
alert(xmlObject.name); // shows 'Kibo'
alert(xmlObject.power); // shows '42'
alert(xmlObject.home); // shows 'http://www.kibo.com/'
or as follows:
alert(xmlObject['name']); // shows 'Kibo'
alert(xmlObject['power']); // shows '42'
alert(xmlObject['home']); // shows 'http://www.kibo.com/'
As with normal objects you use the dot notation if the property name is
an identifier and known at the time when you write the script while you
use the bracket notation when the property name is not an identifier or
when you need to dynamically determine the property name from a
JavaScript expression.
However while a normal JavaScript object can only have exactly one
property with a certain property name an XML element can have several
child elements with the same name. For example the following XML object
has two child elements of the same name ('god'):
var xmlObject = <gods>
<god>Kibo</god>
<god>Xibo</god>
</gods>;
If you now access the property named 'god' then you get an XMLList
object containing the two child elements:
var godList = xmlObject.god;
alert(godList.length()); // shows 2
alert(godList);
/* shows
'<god>Kibo</god>
<god>Xibo</god>'
*/
XML knows different kind of nodes (besides element nodes there can be
text nodes, comment nodes, processing instructions nodes for example) so
there needs to be a way to access other child nodes as well: to access
all types of child nodes you can use the property name '*' as a wild
card to get an XMLList with all child nodes:
XML.ignoreComments = false;
XML.ignoreProcessingInstructions = false;
var xmlObject = <gods>
<!-- Kibology for all -->
<god>Kibo</god>
<god>Xibo</god>
<?process Kibology="on"?>
</gods>;
var godList = xmlObject.*;
alert(godList.length()); // shows 4
alert(godList);
/* shows
'<!-- Kibology for all -->
<god>Kibo</god>
<god>Xibo</god>
<?process Kibology="on"?>'
*/
There is no property name to access text nodes or comment nodes or
processing instruction nodes only but for all these kind of nodes there
is a method of XML objects to return all child nodes of the kind. To get
an XMLList with all text child nodes you call the method named text:
var paragraph = <p>Kibology for <b>all</b>. All for <b>Kibology</b>.</p>;
var textNodes = paragraph.text();
alert(textNodes.length()); // shows 3
alert(textNodes); // shows 'Kibology for. All for.'
To get an XMLList with all comment list nodes you call the method named
comments:
XML.ignoreComments = false;
var gods = <gods>
<!-- Kibology for all -->
<god>Kibo</god>
<god>Xibo</god>
<!-- All for Kibology -->
</gods>;
var commentList = gods.comments();
alert(commentList.length()); // shows 2
alert(commentList.toXMLString());
/* shows
'<!-- Kibology for all -->
<!-- All for Kibology -->'
*/
To get an XMLList with all processing instruction child nodes you call
the method named processingInstructions:
XML.ignoreProcessingInstructions = false;
var gods = <gods>
<?process Kibology="on"?>
<god>Kibo</god>
<god>Xibo</god>
<?process Kibology="off"?>
</gods>;
var processingInstructionList = gods.processingInstructions();
alert(processingInstructionList.length()); // shows 2
(processingInstructionList.toXMLString());
/* shows
'<?process Kibology="on"?>
<?process Kibology="off"?>'
*/
http://www.ksa-123.com
http://www.ksa-2000.com
http://www.chat-kuwait.com
http://www.vip-kuwait.com
http://www.chat-3rb.com
http://www.vip-3rb.com
http://www.3rb-chat.com
http://www.vipgulf.com
http://www.chat-gulf.com
http://www.vip-gulf.com