
CHAPTER 9: Integrating XML into JavaScript The XML Object 241
The result is this XML:
<book category="LITERATURE, FANTASY">
<title lang="en">The Wonderful Wizard of Oz</title>
...
Deleting elements and attributes
To delete an element or attribute in the XML, use the JavaScript delete operator to delete the
corresponding element or attribute property. If there are multiple instances of an element, you can delete
all, or refer to a single one by its index.
Deletion examples
X This statement deletes all authors from the third book:
delete bookstoreXML.book[2].author;
X
This statement deletes only the second author from the third book:
delete bookstoreXML.book[2].author[1];
X
This statement deletes the category attribute from the third book:
delete bookstoreXML.book[2].@category;
Retrieving contained elements
The XML object provides methods that allow you to retrieve elements contained at various levels of the
tree:
X XML.children() gets the direct child elements, including text elements.
X XML.elements() gets the direct child elements that are XML tags, but does not get text.
X XML.descendants() allows you to match a specific tag, and gets all matching elements at any level of
nesting. You can also use a "double dot" notation to access descendants of an element. For example,
these statements are equivalent:
xml..title
xml.descendants("title")
For example, consider this XML code loaded into a top-level XML object named x:
<top>
<one>one text</one>
<two>
two text
<inside>inside text</inside>
</two>
top text
</top>
Here are the results of the different calls.
Comentários a estes Manuais