Sie sind auf Seite 1von 2

XML Syntax – Tree Structure return xmlDoc; //XMLDOM Object

<?xml version="1.0" encoding="ISO-8859-1"?> }


<!-- This is a comment -->
<root> XMLDOM
<child1> … </child1>
<subchild> … </subchild> Programming interface for XML
<child2> … </child2> Everything in XML is a node
</root> x- XMLDOM Object created by server
XML Rules Accessing Nodes
All XML elements must have a closing tag 1) getElementsByTagName() e.g.
XML tags are case sensitive node.getElementsByTagName("tagname"); //returns
Elements must be properly nested node list
XML Documents must have a root element
2) traversing through the tree (x.length)
XML Attribute values must be quoted 3) navigating using node relationships (x.nodeType)
txt=xmlDoc.getElementsByTagName("title")[0].c
XML Entity References hildNodes[0].nodeValue
&lt; < less than
&gt; > greater than Use document.write and nodeValue to write to HTML
&amp; & ampersand Or use document.getElementById(“divid”).innerHTML =
&apos; ' apostrophe vartxt;
&quot; " quotation mark Where divid element receives the XML data

Attribute e.g. <tag attrib = “random”> Traversing Node Tree


Tip: Avoid attributes, use elements // documentElement always represents the root
node
Metadata: data about data; to be stored as attributes x=xmlDoc.documentElement.childNodes;
To link XML and CSS for (i=0;i<x.length;i++)
<?xml-stylesheet type="text/css" {
href="random.css"?> document.write(x[i].nodeName);
document.write(": ");
document.write(x[i].childNodes[0].nodeValue);
XML Parser – document.write("<br />");
}
converts XML to JavaScript accessible object (XML DOM)
Load XML Document
function loadXMLDoc(dname) //dname – XML File Note: Internet Explorer will NOT treat empty white-spaces,
{ or new lines as text nodes, while other browsers will.
if (window.XMLHttpRequest) //false for IE To Avoid empty text nodes traverse using (x.nodeType!=1).
{
xhttp=new XMLHttpRequest(); Move to the next node, until element node is not found.
} e.g.
else function get_firstChild(n)
{ {
xhttp=new y=n.firstChild;
ActiveXObject("Microsoft.XMLHTTP"); while (y.nodeType!=1)
} {
xhttp.open("GET",dname,false); y=y.nextSibling;
xhttp.send(""); }
return xhttp.responseXML; //XMLDOM Object return y;
} }

Load XML String


function loadXMLString(txt) //txt - String
{
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml")
;
}
else // Internet Explorer
{
xmlDoc=new
ActiveXObject("Microsoft.XMLDOM"); Compiled: Angad Singh
xmlDoc.async="false"; Use it, Forward it, Edit it
xmlDoc.loadXML(txt);
}
XML DOM Reference belongs.
Objects prefix Retrieves the namespace prefix of the node.
Attribute Represents an element attribute in the previousSibling Retrieves the previous sibling of the Node in
Document Object Model (DOM). document source order.
Attributes Represents a collection of attribute objects text Retrieves the contents of all the text nodes
in the Document Object Model (DOM). contained by the Node and any element nodes
CDATASection Represents an XML CDATA section in the that it contains. **
Document Object Model (DOM). value Retrieves the value of the Attribute.
Comment Represents an XML comment in the
Document Object Model (DOM).
Document Represents an XML document in the Methods
Document Object Model (DOM). getAttribute Retrieves the value of the specified
Element Represents an XML element node in the attribute.
Document Object Model (DOM). getAttributeNS Retrieves the value of the attribute
Node Represents a generic node object in an XML with the specified local name and
document. namespace URI.
NodeList Represents an ordered collection of node getAttributeNode Retrieves an Attribute object
objects in the Document Object Model encapsulating the named attribute.
(DOM). getAttributeNodeNS Retrieves an Attribute object with the
ProcessingInstruction Represents an XML processing instruction specified local name and namespace
in the Document Object Model (DOM). URI.
TextNode Represents an XML text node in the getElementById Returns the Node object that has an id
Document Object Model (DOM). attribute with the specified value.
getElementsByTagName Returns a NodeList collection of
descendant element nodes with the
Properties specified name.
attributes Retrieves an Attributes collection of Attribute getElementsByTagNameNS Returns a NodeList collection of
objects. descendant element nodes with the
childNodes Retrieves a NodeList collection of node objects. specified local name and namespace
URI.
data Retrieves the text stored within a TextNode,
CDATASection, or Comment. getNamedItem Retrieves from an Attributes collection
the attribute with the specified
documentElement Retrieves the root element node of the nodeName.
document.
getNamedItemNS Retrieves from an Attributes collection
firstChild Retrieves the first child of the Node in the attribute with the specified local
document source order. name and namespace URI.
lastChild Returns the last child of the Node object in hasAttribute Returns whether or not the Element
document source order. has the specified attribute.
length Retrieves the number of elements in the hasAttributeNS Returns whether or not the Element
collection. has an attribute with the specified local
localName Retrieves the local name of the node. name and namespace URI.
name Retrieves the name of the Attribute. hasAttributes Returns whether or not the Node has
namespaceURI Retrieves the namespace URI of the node. attributes.
nextSibling Retrieves the next sibling of the Node in hasChildNodes Returns whether or not the Node has
document source order. children.
nodeName Retrieves the name of the node. item Retrieves an object from the collection
by its index.
nodeType Retrieves the type of the node.
substringData Extracts a range of characters from the
nodeValue Retrieves the value of the node. contents of a TextNode, Comment, or
ownerDocument Retrieves a reference to the Document object CDATASection.
that contains the Node.
ownerElement Retrieves the Element to which the Attribute
belongs.
parentNode Retrieves the parent Node to which the node

Das könnte Ihnen auch gefallen