常见节点类型:
新建分析器实例: var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")-----------javascript
set xmlDoc=CreateObject("Microsoft.XMLDOM")--------------------vbscript
将一个xml文件引入:<script type="text/javascript">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
do some thing!
</script>
将一个xml格式的字符串引入分析器:
dom对象:
Attr对象:返回属性的名字和值:xmlDoc.documentElement.attributes[0].name//documentElement指根点,
实际使用的时候要先找到某一个点,再取属性。
xmlDoc.documentElement.attributes[0].value
判断属性值是否来自默认值:document.write(xmlDoc.documentElement.attributes[0].specified)
NodeList对象:
属性:length
方法:item(index): 返回指定节点
nextNode(): 返回下一节点,将指针下移
reset(): 将指针返回至列表开始
举例:length应用 xmlDoc.documentElement.childNodes.length
item(index)应用 xmlDoc.documentElement.childNodes.item(2).nodeName
nextNode()应用(遍历用): var x=xmlDoc.documentElement.childNodes
var y=x.nextNode()
reset()应用: var x=xmlDoc.documentElement.childNodes
var y=x.nextNode()
x.reset()
Node对象:
属性:
举例:nodeName: for(var i=0;i<xmlDoc.documentElement.childNodes.length;i++)
{
var x=xmlDoc.documentElement.childNodes.item(i);
alert(x.nodeName);
}
nodeValue: var nodeli= xmlDoc.documentElement.childNodes;
alert(nodeli.length);
for(var j=0 ;j<nodeli.length;j++){
var valueli=nodeli.item(j).childNodes(0).nodeValue;//attention,nodeli.item(j)已经指向需要访问的节点,后面再加childNodes(0),才能访问到文本节点。
alert(valueli);
}
nextSibling: document.write(xmlDoc.documentElement.childNodes(0).nodeName);
document.write(xmlDoc.documentElement.childNodes(0).nextSibling.nodeName);
| Node Type | Example |
|---|---|
| Processing instruction | <?xml version="1.0"?> |
| Element | <drink type="beer">Carlsberg</drink> |
| Attribute | type="beer" |
| Text | Carlsberg |
新建分析器实例: var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")-----------javascript
set xmlDoc=CreateObject("Microsoft.XMLDOM")--------------------vbscript
将一个xml文件引入:<script type="text/javascript">
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
do some thing!
</script>
将一个xml格式的字符串引入分析器:
<script type="text/javascript">
var txt="<note>" txt=txt+"<to>Tove</to><from>Jani</from>" txt=txt+"<heading>Reminder</heading>" txt=txt+"<body>Don't forget me this weekend!</body>" txt=txt+"</note>"
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(txt)
do some thing!
</script>
下面的例子是典型的利用xml+javascript将页面和数据分离
<html>
<head>
<script type="text/javascript">
var xmlDoc
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
getmessage()
}
// code for Mozilla, etc.
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc= document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=getmessage
}
else
{
alert('Your browser cannot handle this script');
}
}
function getmessage()
{
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].firstChild.nodeValue
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].firstChild.nodeValue
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue
}
</script>
</head>
<body onload="loadXML()" bgcolor="yellow">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span>
<hr />
<b>Message:</b> <span id="message"></span>
</p>
</body>
</html>
节点类型常量:
| NodeType | Named Constant |
|---|---|
| 1 | ELEMENT_NODE |
| 2 | ATTRIBUTE_NODE |
| 3 | TEXT_NODE |
| 4 | CDATA_SECTION_NODE |
| 5 | ENTITY_REFERENCE_NODE |
| 6 | ENTITY_NODE |
| 7 | PROCESSING_INSTRUCTION_NODE |
| 8 | COMMENT_NODE |
| 9 | DOCUMENT_NODE |
| 10 | DOCUMENT_TYPE_NODE |
| 11 | DOCUMENT_FRAGMENT_NODE |
| 12 | NOTATION_NODE |
dom对象:
Attr对象:返回属性的名字和值:xmlDoc.documentElement.attributes[0].name//documentElement指根点,
实际使用的时候要先找到某一个点,再取属性。
xmlDoc.documentElement.attributes[0].value
判断属性值是否来自默认值:document.write(xmlDoc.documentElement.attributes[0].specified)
NodeList对象:
属性:length
方法:item(index): 返回指定节点
nextNode(): 返回下一节点,将指针下移
reset(): 将指针返回至列表开始
举例:length应用 xmlDoc.documentElement.childNodes.length
item(index)应用 xmlDoc.documentElement.childNodes.item(2).nodeName
nextNode()应用(遍历用): var x=xmlDoc.documentElement.childNodes
var y=x.nextNode()
reset()应用: var x=xmlDoc.documentElement.childNodes
var y=x.nextNode()
x.reset()
Node对象:
属性:
| Property | Description | IE | F | N | W3C |
|---|---|---|---|---|---|
| attributes | Returns a NamedNodeMap that contains all attributes of a node | 5 | Yes | ||
| basename | Returns the name of a node (without namespaces) | 5 | No | ||
| childNodes | Returns a node list that contains all children of a node | 5 | Yes | ||
| dataType | Returns the data type of a node | 5 | No | ||
| definition | Returns the definition of a node in the DTD/Schema | 5 | No | ||
| firstChild | Returns the first child node of a node | 5 | Yes | ||
| lastChild | Returns the last child node of a node | 5 | Yes | ||
| namespaceURI | Returns the URI of the namespace of a node | 5 | Yes | ||
| nextSibling | Returns the node immediately following a node. Two nodes are siblings if they have the same parent node | 5 | Yes | ||
| nodeName | Returns the name of the node (depending on the node type) | 5 | Yes | ||
| nodeType | Returns the node type as a number | 5 | Yes | ||
| nodeTypedValue | Returns the value of a node expressed in its defined data type | 5 | No | ||
| nodeTypeString | Returns the node type as a string | 5 | No | ||
| nodeValue | Returns the value of the node | 5 | Yes | ||
| ownerDocument | Returns the Document object of a node (returns the root node of the document) | 5 | Yes | ||
| parentNode | Returns the parent node of a node | 5 | Yes | ||
| parsed | Returns true if the node and all of its descendants have been parsed. Otherwise it returns false | 5 | No | ||
| prefix | Returns the namespace prefix of a node | 5 | Yes | ||
| previousSibling | Returns the node immediately preceding a node. Two nodes are siblings if they have the same parent node | 5 | Yes | ||
| specified | Returns true if the node is specified or derived from a default value in the DTD/Schema. Otherwise it returns false | 5 | No | ||
| text | Returns the text content of a node and all its children | 5 | No | ||
| xml | Returns the XML of a node and all its children | 5 | No |
方法
| Method | Description | IE | F | N | W3C |
|---|---|---|---|---|---|
| appendChild(newnode) | Appends a new child node to a node | 5 | Yes | ||
| cloneNode(boolean) | Creates an exact clone node of a node. If the boolean parameter is set to true, the cloned node clones all the child nodes of the original node as well | 5 | Yes | ||
| hasChildNodes() | Returns true if a node has child nodes. Otherwise it returns false | 5 | Yes | ||
| insertBefore(newnode,refnode) | Inserts a new node (newnode) before the existing node (refnode) | 5 | Yes | ||
| removeChild(nodename) | Removes the specified node and returns it | 5 | Yes | ||
| replaceChild(newnode,oldnode) | Replaces the oldnode with the newnode, and returns the oldnode | 5 | Yes | ||
| selectNodes(pattern) | Creates a node list of all the matching descendant nodes returned by the specified pattern | 5 | No | ||
| selectSingleNode(pattern) | Returns a Node object for the first descendant node to match the specified pattern | 5 | No | ||
| transformNode(stylesheet) | Processes a node and its descendants using the specified XSL stylesheet, and returns the result | 5 | No | ||
| transformNodeToObject(stylesheet,object) | Processes a node and its descendants using the specified XSL stylesheet, and returns the result in the specified object | 5 | No |
{
var x=xmlDoc.documentElement.childNodes.item(i);
alert(x.nodeName);
}
nodeValue: var nodeli= xmlDoc.documentElement.childNodes;
alert(nodeli.length);
for(var j=0 ;j<nodeli.length;j++){
var valueli=nodeli.item(j).childNodes(0).nodeValue;//attention,nodeli.item(j)已经指向需要访问的节点,后面再加childNodes(0),才能访问到文本节点。
alert(valueli);
}
nextSibling: document.write(xmlDoc.documentElement.childNodes(0).nodeName);
document.write(xmlDoc.documentElement.childNodes(0).nextSibling.nodeName);
浙公网安备 33010602011771号