Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
119 Cards in this Set
- Front
- Back
What is the DOM?
|
The DOM is a W3C (World Wide Web Consortium) standard.
The DOM defines a standard for accessing documents like XML and HTML: "The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document." |
|
The DOM is separated into 3 different parts / levels:
|
1)Core DOM - standard model for any structured document
2)XML DOM - standard model for XML documents 3)HTML DOM - standard model for HTML documents |
|
What is the XML DOM?
|
The XML DOM is:
A standard object model for XML A standard programming interface for XML Platform- and language-independent A W3C standard The XML DOM defines the objects and properties of all XML elements, and the methods (interface) to access them. In other words: The XML DOM is a standard for how to get, change, add, or delete XML elements. |
|
Which element is the first child, which is the last child, who are the subling
<bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> </bookstore> |
In the XML , the <title> element is the first child of the <book> element, and the <price> element is the last child of the <book> element.
Furthermore, the <book> element is the parent node of the <title>, <author>, <year>, and <price> elements. |
|
he XML DOM views an XML document as a node-tree.
All the nodes in the tree have a relationship to each other. The XML DOM Node Tree The XML DOM views an XML document as a tree-structure. The tree structure is called a node-tree. All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created. The node tree shows the set of nodes, and the connections between them. The tree starts at the root node and branches out to the text nodes at the lowest level of the tree: The image above represents the XML file books.xml. Node Parents, Children, and Siblings The nodes in the node tree have a hierarchical relationship to each other. The terms parent, child, and sibling are used to describe the relationships. Parent nodes have children. Children on the same level are called siblings (brothers or sisters). In a node tree, the top node is called the root Every node, except the root, has exactly one parent node A node can have any number of children A leaf is a node with no children Siblings are nodes with the same parent The following image illustrates a part of the node tree and the relationship between the nodes: Because the XML data is structured in a tree form, it can be traversed without knowing the exact structure of the tree and without knowing the type of data contained within. You will learn more about traversing the node tree in a later chapter of this tutorial. First Child - Last Child Look at the following XML fragment: <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> </bookstore> In the XML above, the <title> element is the first child of the <book> element, and the <price> element is the last child of the <book> element. Furthermore, the <book> element is the parent node of the <title>, <author>, <year>, and <price> elements. |
if (window.XMLHttpRequest)
{ xhttp=new XMLHttpRequest(); } else // IE 5/6 { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET","books.xml",false); xhttp.send(); xmlDoc=xhttp.responseXML; |
|
Internet Explorer uses the ___________ method to parse an XML string, while other browsers use the ______________.
|
method to parse an XML string
DOMParser object |
|
Load an XML String
|
if (window.DOMParser) {
parser=new DOMParser(); xmlDoc=parser.parseFromString(text,"text/xml"); } else // Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.loadXML(text); } |
|
Access Across Domains
|
For security reasons, modern browsers do not allow access across domains.
This means, that both the web page and the XML file it tries to load, must be located on the same server. The examples on W3Schools all open XML files located on the W3Schools domain. If you want to use the example above on one of your web pages, the XML files you load must be located on your own server. |
|
loadXMLDoc() Function
generally placed in <head> |
function loadXMLDoc(dname)
{ if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET",dname,false); xhttp.send(); return xhttp.responseXML; } |
|
External JavaScript for loadXMLDoc()
file is called "loadxmldoc.js", and will be loaded in the head section of an HTML page. Then, the loadXMLDoc() function can be called from a script in the page. |
<html> <head>
<script type="text/javascript" src="loadxmldoc.js"> </script> </head> <body> <script type="text/javascript"> xmlDoc=loadXMLDoc("books.xml"); code goes here..... </script> </body> </html> |
|
XML DOM Properties
|
1)x.nodeName - the name of x
2)x.nodeValue - the value of x x.parentNode - the parent node of x 3)x.childNodes - the child nodes of x 4)x.attributes - the attributes nodes of x |
|
XML DOM Methods
|
1)x.getElementsByTagName(name) - get all elements with a specified tag name
2)x.appendChild(node) - insert a child node to x 3)x.removeChild(node) - remove a child node from x |
|
The JavaScript code to get the text from the first <title> element in books.xml:
|
txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue
After the execution of the statement, txt will hold the value "Everyday Italian" |
|
xmlDoc
|
the XML DOM object created by the parser
|
|
getElementsByTagName("title")[0]
|
the first <title> element
|
|
childNodes[0]
|
the first child of the <title> element (the text node)
|
|
nodeValue
|
the value of the node (the text itself)
|
|
getElementsByTagName exampe
|
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title"); document.write(x[2].childNodes[0].nodeValue); </script> |
|
The getElementsByTagName() Method
|
getElementsByTagName() returns all elements with a specified tag name.
---Syntax node.getElementsByTagName("tagname"); ---Example The following example returns all <title> elements under the x element: x.getElementsByTagName("title"); |
|
x.getElementsByTagName("title");
Note that the example above only returns <title> elements under the x node. To return all <title> elements in the XML document use ???? |
xmlDoc.getElementsByTagName("title");
|
|
Node Types (3)
|
The documentElement property of the XML document is the root node.
The nodeName property of a node is the name of the node. The nodeType property of a node is the type of the node. |
|
Get the Value of an Element example
|
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue; |
|
Change the Value of an Element example
|
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking"; |
|
The nodeType property specifies the type of node.
nodeType is read only. The most important node types are: |
Node Type
Element 1 Attribute 2 Text 3 Comment 8 Document 9 |
|
DOM Attribute List (Named Node Map)
|
The attributes property of an element node returns a list of attribute nodes.
This is called a named node map, and is similar to a node list, except for some differences in methods and properties. A attribute list keeps itself up-to-date. If an attribute is deleted or added, the list is automatically updated. |
|
Traversing means ????
|
means looping through or traveling across the node tree.
|
|
Browser Differences in DOM Parsing
|
nternet Explorer will NOT treat empty white-spaces, or new lines as text nodes, while other browsers will.
|
|
Get an Attribute Value - getAttribute()
|
getAttribute() method returns an attribute value.
following code retrieves the text value of the "lang" attribute of the first <title> element: ---Example xmlDoc=loadXMLDoc("books.xml"); txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang"); ---Result txt = "en" |
|
Get an Attribute Value - getAttributeNode()
|
The getAttributeNode() method returns an attribute node.
The following code retrieves the text value of the "lang" attribute of the first <title> element: ---Example xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang"); txt=x.nodeValue; ---Result txt = "en" |
|
Remove an Element Node
|
The removeChild() method removes a specified node.
When a node is removed, all its child nodes are also removed. The following code fragment will remove the first <book> element from the loaded xml: xmlDoc=loadXMLDoc("books.xml"); y=xmlDoc.getElementsByTagName("book")[0]; xmlDoc.documentElement.removeChild(y); |
|
Remove Myself - Remove the Current Node
|
The removeChild() method is the only way to remove a specified node.
When you have navigated to the node you want to remove, it is possible to remove that node using the parentNode property and the removeChild() method: xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0]; x.parentNode.removeChild(x); |
|
Remove a Text Node
|
removeChild() method can also be used to remove a text node:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; y=x.childNodes[0]; x.removeChild(y); |
|
Clear a Text Node
|
nodeValue property can be used to change or clear the value of a text node:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue=""; |
|
Remove an Attribute Node by Name
|
removeAttribute(name) method is used to remove an attribute node by its name.
removeAttribute('category') The following code fragment removes the "category" attribute in the first <book> element: xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book"); x[0].removeAttribute("category"); |
|
Remove Attribute Nodes by Object
|
The removeAttributeNode(node) method is used to remove an attribute node, using the node object as parameter.
Example: removeAttributeNode(x) The following code fragment removes all the attributes of all <book> elements: xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book"); for (i=0;i<x.length;i++) { while (x[i].attributes.length>0) { attnode=x[i].attributes[0]; old_att=x[i].removeAttributeNode(attnode); } } |
|
Replace an Element Node
|
replaceChild() method is used to replace a node.
|
|
Replace Data In a Text Node
|
The replaceData() method is used to replace data in a text node.
The replaceData() method has three parameters: offset - Where to begin replacing characters. Offset value starts at zero length - How many characters to replace string - The string to insert --- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.replaceData(0,8,"Easy"); |
|
nodeValue Property it is easier to replace the data in a text node using the nodeValue property.
|
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Italian"; |
|
Create a New Element Node
|
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel); |
|
Create a New Attribute Node
|
xmlDoc=loadXMLDoc("books.xml");
newatt=xmlDoc.createAttribute("edition"); newatt.nodeValue="first"; x=xmlDoc.getElementsByTagName("title"); x[0].setAttributeNode(newatt); |
|
Create an Attribute Using setAttribute()
|
Since the setAttribute() method creates a new attribute if the attribute does not exist, it can be used to create a new attribute.
Example xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); x[0].setAttribute("edition","first"); |
|
createTextNode
|
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition"); newtext=xmlDoc.createTextNode("first"); newel.appendChild(newtext); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel); |
|
CDATA Section Node - createCDATASection()
|
xmlDoc=loadXMLDoc("books.xml");
newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newCDATA); |
|
appendChild
|
The appendChild() method adds a child node to an existing node.
The new node is added (appended) after any existing child nodes. Note: Use insertBefore() if the position of the node is important. |
|
Add a Node - appendChild()
|
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel); |
|
Insert a Node - insertBefore()
|
xmlDoc=loadXMLDoc("books.xml");
newNode=xmlDoc.createElement("book"); x=xmlDoc.documentElement; y=xmlDoc.getElementsByTagName("book")[3]; x.insertBefore(newNode,y); |
|
Add a New Attribute
|
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book'); x[0].setAttribute("edition","first"); |
|
Add Text to a Text Node - insertData()
|
The insertData() method inserts data into an existing text node.
The insertData() method has two parameters: offset - Where to begin inserting characters (starts at zero) string - The string to insert |
|
insertData()
|
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.insertData(0,"Easy "); |
|
Copy a Node
|
cloneNode() method creates a copy of a specified node.
The cloneNode() method has a parameter (true or false). This parameter indicates if the cloned node should include all attributes and child nodes of the original node. |
|
XMLHttpRequest Object
|
XMLHttpRequest object is used to exchange data with a server behind the scenes.
The XMLHttpRequest object is the developers dream, because you can: 1)Update a web page without reloading the page 2)Request data from a server after the page has loaded 3)Receive data from a server after the page has loaded 4)Send data to a server in the background |
|
Syntax for creating an XMLHttpRequest object:
|
xmlhttp=new XMLHttpRequest();
|
|
Old versions of Internet Explorer (IE5 and IE6) uses an ActiveX Object:
|
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
|
|
To handle all modern browsers, including IE5 and IE6, check if the browser supports the XMLHttpRequest object. If it does, create an XMLHttpRequest object, if not, create an ActiveXObject:
|
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } |
|
Send a Request To a Server
|
To send a request to a server, we use the open() and send() methods of the XMLHttpRequest object:
xmlhttp.open("GET","xmlhttp_info.txt",true); xmlhttp.send(); |
|
GET or POST?
|
GET is simpler and faster than POST, and can be used in most cases.
However, always use POST requests when: A cached file is not an option (update a file or database on the server) Sending a large amount of data to the server (POST has no size limitations) Sending user input (which can contain unknown characters), POST is more robust and secure than GET |
|
Server Response
|
get the response from a server, use the responseText or responseXML property of the XMLHttpRequest object.
responseText -get the response data as a string responseXML - get the response data as XML data |
|
Document W3C node type
|
Represents the entire document (the root-node of the DOM tree)
-----children----- Element (max. one), ProcessingInstruction, Comment, DocumentType -------- xmlDoc=loadXMLDoc("books.xml"); document.write("Nodename: " + xmlDoc.nodeName); document.write(" (nodetype: " + xmlDoc.nodeType + ")<br />"); Element (max. one), ProcessingInstruction, Comment, DocumentType |
|
DocumentFragment W3C node type
|
Represents a "lightweight" Document object, which can hold a portion of a document
----children---- Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
|
DocumentType W3C node type
|
Provides an interface to the entities defined for the document
|
|
EntityReference W3C node types
|
EntityReference
------ Represents an entity reference -----children---- Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
|
Attr
|
Represents an attribute
----children---- Text, EntityReference |
|
Text
|
Represents textual content in an element or attribute
|
|
CDATASection
|
Represents a CDATA section in a document (text that will NOT be parsed by a parser)
|
|
Commend
|
Represents a comment
|
|
Notation
|
Represents an entity
----children---- Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
|
Document Node type
nodeName returned nodeValue returned |
nodeName returned - #document
nodeValue returned - null |
|
DocumentFragment Node type
nodeName returned nodeValue returned |
nodeName returned - #document fragment
nodeValue returned - null |
|
DocumentType Node type
nodeName returned nodeValue returned |
nodeName returned - doctype name
nodeValue returned - null |
|
EntityReference Node type
nodeName returned nodeValue returned |
nodeName returned - entity reference name
nodeValue returned - null |
|
Element Node type
nodeName returned nodeValue returned |
nodeName returned - element name
nodeValue returned - null |
|
Attr Node type
nodeName returned nodeValue returned |
nodeName returned - attribute name
nodeValue returned - attribute value |
|
ProcessingInstruction Node type nodeName returned
nodeValue returned |
nodeName returned - target
nodeValue returned - content of node |
|
Text Comment Node type nodeName returned
nodeValue returned |
nodeName returned - #text
nodeValue returned - content of nde |
|
CDATASection Node type nodeName returned
nodeValue returned |
nodeName returned - #cdata-section
nodeValue returned - content of node |
|
Entity Node type
nodeName returned nodeValue returned |
nodeName returned - entity name
nodeValue returned - null |
|
Notation Node type
nodeName returned nodeValue returned |
nodeName returned - notation name
nodeValue returned - null |
|
Text Node type
nodeName returned nodeValue returned |
nodeName returned - #text
nodeValue returned - content of node |
|
NodeType
1 2 3 4 |
Named Constant
1 - ELEMENT_NODE 2 - ATTRIBUTE_NODE 3 - TEXT_NODE 4 - CDATA_SECTION_NODE |
|
NodeType
5 6 7 8 |
5 - ENTITY_REFERENCE_NODE
6 - ENTITY_NODE 7 - PROCESSING_INSTRUCTION_NODE 8 - COMMENT_NODE |
|
NodeType
9 10 11 12 |
9 - DOCUMENT_NODE
10 - DOCUMENT_TYPE_NODE 11 - DOCUMENT_FRAGMENT_NODE 12 - NOTATION_NODE |
|
previousSibling
|
Returns the node immediately before a node
|
|
parentNode
|
Returns the parent node of a node
|
|
ownerDocument
|
Returns the root element (document object) for a node
|
|
nodeValue
|
Sets or returns the value of a node, depending on its type
|
|
nodeType
|
Returns the type of a node
|
|
nodeName
|
Returns the name of a node, depending on its type
|
|
nextSibling
childNodes |
nextSibling - Returns the node immediately following a node
childNodes - Returns a NodeList of child nodes for a node |
|
lastChild
firstChild |
lastChild - Returns the last child of a node
firstChild - Returns the first child of a node |
|
removeNamedItem()
|
Removes the specified node (by name)
|
|
item()
|
Returns the node at the specified index
----- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); for(i=0;i<x.length;i++) { att=x.item(i).attributes.getNamedItem("category"); document.write(att.value + "<br />"); } |
|
getElementsByTagName()
|
Returns a NodeList of all elements with a specified name
----- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0] y=x.childNodes[0]; document.write(y.nodeValue); |
|
getElementById(id)
|
Returns the element that has an ID attribute with the given value. If no such element exists, it returns null
|
|
createTextNode()
createElement() |
createTextNode() - Creates a text node
createElement() - Creates an element node |
|
createDocumentFragment()
|
Creates an empty DocumentFragment object, and returns it
|
|
createComment()
|
Creates a comment node
---- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); var newComment,newtext; newtext="Revised April 2008"; for (i=0;i<x.length;i++) { newComment=xmlDoc.createComment(newtext); x[i].appendChild(newComment); } |
|
XML DOM attributes Property
|
The attributes property returns a NamedNodeMap (attribute list) containing the attributes of the selected node
If the selected node is not an element, this property returns NULL. |
|
XML DOM childNodes Property
|
The childNodes property returns a NodeList containing the child nodes of the selected node
If the selected node has no children, this property returns a NodeList containing no nodes. ------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0] y=x.childNodes[0]; document.write(y.nodeValue); |
|
XML DOM firstChild Property
|
The firstChild property returns the first child node of the selected element if the selected node has no children, this property returns NULL.
|
|
XML DOM nextSibling Property function example
|
//check if the next sibling node is an element node
function get_nextsibling(n) { x=n.nextSibling; while (x.nodeType!=1) { x=x.nextSibling; } return x; } |
|
XML DOM nodeName Property
|
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0]; document.write(x.nodeName); |
|
XML DOM nodeType Property
|
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0]; document.write(x.nodeType); |
|
XML DOM ownerDocument Property
|
he ownerDocument property returns the document object the selected element belongs to.
------------------------------------------ xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; x=x.ownerDocument; document.write("Nodename: " + x.nodeName); document.write(" (nodetype: " + x.nodeType + ")"); |
|
XML DOM parentNode Property
|
parentNode property returns the parent node of the specified element.
-------------------------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; parent=x.parentNode; document.write("Parent node: " + parent.nodeName); |
|
XML DOM previousSibling Property
|
The previousSibling property returns the previous sibling node (the previous node in the same tree level) of the selected element
If there is no such node, this property returns null. //check if the previous sibling node is an element node function get_previoussibling(n) { x=n.previousSibling; while (x.nodeType!=1) { x=x.previousSibling; } return x; } |
|
XML DOM tagName Property
|
tagName property returns the tag name of the selected element.
-------------------------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; document.write(x.tagName); |
|
XML DOM appendChild() Method
|
appendChild() method adds a node after the last child node of the specified element node.
---------------------------------------- xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel); document.write(x.getElementsByTagName("edition")[0].nodeName); |
|
XML DOM cloneNode() Method
|
The cloneNode() method creates an exact copy of a specified node.
----------------------------------------- xmlDoc=loadXMLDoc("books.xml"); oldNode=xmlDoc.getElementsByTagName('book')[0]; newNode=oldNode.cloneNode(true); xmlDoc.documentElement.appendChild(newNode); //Output all titles y=xmlDoc.getElementsByTagName("title"); for (i=0;i<y.length;i++) { document.write(y[i].childNodes[0].nodeValue); document.write("<br />"); } |
|
XML DOM getAttribute() Method
|
getAttribute() method gets an attribute value by name.
----------------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); for (i=0;i<x.length;i++) { document.write(x[i].getAttribute('category')); document.write("<br />"); } |
|
XML DOM getAttributeNode() Method
|
getAttributeNode() method gets an attribute node by name from the current element.
-------------------------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); for(i=0;i<x.length;i++) { attnode=x.item(i).getAttributeNode("category"); document.write(attnode.name); document.write(" = "); document.write(attnode.value); document.write("<br />"); } |
|
XML DOM hasAttribute() Method
|
hasAttribute() method returns TRUE if the current element node has the attribute specified by name, and FALSE otherwise.
---------------------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0]; document.write(x.hasAttribute("category")); |
|
XML DOM hasAttributes() Method
|
hasAttributes() method returns TRUE if the current element node has any attributes, and FALSE otherwise
------------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book')[0]; document.write(x.hasAttributes()); |
|
XML DOM hasChildNodes() Method
|
The hasChildNodes() method returns TRUE if the current element node has child nodes, and FALSE otherwise.
--------------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0]; document.write(x.hasChildNodes()); |
|
XML DOM insertBefore() Method
|
The insertBefore() method inserts a new child node before an existing child node.
This method returns the new child node. ----------------------------------- xmlDoc=loadXMLDoc("books.xml"); newNode=xmlDoc.createElement("book"); x=xmlDoc.documentElement; y=xmlDoc.getElementsByTagName("book")[3]; x.insertBefore(newNode,y); |
|
XML DOM removeAttribute() Method
|
The removeAttribute() method removes a specified attribute.
If a default value for the attribute is defined in a DTD, a new attribute immediately appears with the default value ----------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); document.write(x[0].getAttribute('category')); document.write("<br />"); x[0].removeAttribute('category'); document.write(x[0].getAttribute('category')); |
|
XML DOM removeAttributeNode() Method
|
The removeAttributeNode() method removes a specified attribute node.
If a default value for the attribute is defined in a DTD, a new attribute immediately appears with the default value. This function returns the removed attribute node. ---------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); for(i=0;i<x.length;i++) { attnode=x.item(i).getAttributeNode("category"); old_att=x.item(i).removeAttributeNode(attnode); document.write("Removed attribute: " + old_att.name + "<br />"); } |
|
XML DOM removeChild() Method
|
The removeChild() method removes a child node.
-------------------------------------- xmlDoc=loadXMLDoc("books.xml"); y=xmlDoc.getElementsByTagName("book")[0]; x=xmlDoc.documentElement.removeChild(y); document.write("Removed node: " + x.nodeName); |
|
XML DOM replaceChild() Method
|
The replaceChild() method replaces a child node with another.
This function returns the replaced node on success, or NULL on failure. ----------------------------------------- xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement; //create a book element newNode=xmlDoc.createElement("book"); y=xmlDoc.getElementsByTagName("book")[0] //replace the first book node with the new node x.replaceChild(newNode,y); |
|
XML DOM setAttributeNode() Method
|
setAttributeNode() method adds a new attribute node.
If an attribute with that name already exists in the element, it is replaced by the new one. If the new attribute replaces an existing attribute, the replaced attribute node is returned, otherwise it returns null. |