Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of DOM Operation XML document in XML

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the XML DOM operation XML document example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

In DOM's eyes, HTML, like XML, is a tree-structured document. Each node is treated as various types of Node objects. Each Node object has its own properties and methods that allow you to traverse the entire document tree. DOM defines nodeType to represent the type of node

Interface

NodeType constant

NodeType value

Remarks

Element

Node.ELEMENT_NODE

one

Element node

Text

Node.TEXT_NODE

three

Text node

Document

Node.DOCUMENT_NODE

nine

Document

Comment

Node.COMMENT_NODE

eight

The text of the comment

DocumentFragment

Node.DOCUMENT_FRAGMENT_NODE

eleven

Document fragment

Attr

Node.ATTRIBUTE_NODE

two

Node attribute

The root node of the DOM tree is a Document object, and sometimes the document points to the entire document.

Most of the methods defined by Document are production methods, which are mainly used to create various types of nodes that can be inserted into the document. The commonly used Document methods are:

Method

Description

CreateAttribute ()

Creates a new Attr node with the specified name.

CreateComment ()

Creates a new Comment node with the specified string.

CreateElement ()

Creates a new Element node with the specified tag name.

CreateTextNode ()

Creates a new TextNode node with the specified text.

GetElementById ()

Returns the Element node in the document with the specified id attribute.

GetElementsByTagName ()

Returns all Element nodes in the document with the specified tag name.

Common methods for Element:

Method

Description

GetAttribute ()

Returns the value of the specified property as a string.

GetAttributeNode ()

Returns the value of the specified property as an Attr node.

GetElementsByTabName ()

Returns an Node array containing the descendant nodes of all Element nodes with the specified tag name, in the order in which they appear in the document.

HasAttribute ()

Returns true if the element has an attribute with the specified name.

RemoveAttribute ()

Removes the specified attribute from the element.

RemoveAttributeNode ()

Removes the specified Attr node from the list of attributes of the element.

SetAttribute ()

Sets the specified property to the specified string value, or adds a new property if it does not exist.

SetAttributeNode ()

Adds the specified Attr node to the list of attributes for the element.

Common properties of Node objects:

Attribute

Description

Attributes

If the node is an Element, the attributes of the element are returned as NamedNodeMap.

ChildNodes

Stores the children of the current node in the form of Node []. If there are no child nodes, an empty array is returned.

FirstChild

Returns the first child of the current node as a Node. Null if there are no child nodes.

LastChild

Returns the last child of the current node as a Node. Null if there are no child nodes.

NextSibling

Returns the next node of the sibling of the current node as a Node. If there is no such node, null is returned.

NodeName

The name of the node, and the Element node represents the tag name of the Element.

NodeType

Represents the type of node.

ParentNode

Returns the parent node of the current node as a Node. Null if there is no parent node.

PreviousSibling

Returns the sibling node immediately before the current node as Node. If there is no such node, null is returned.

Common methods for Node objects:

Method

Description

AppendChild ()

Add nodes to the document tree by adding a node to the childNodes [] group of the current node.

CloneNode ()

Copy the current node, or copy the current node and all its descendant nodes.

HasChildNodes ()

If the current node has child nodes, true is returned.

InsertBefore ()

Inserts a node into the document tree before the specified child node of the current node. If the node already exists, delete it and insert it into its location.

RemoveChild ()

Removes from the document tree and returns the specified child node.

ReplaceChild ()

Removes and returns the specified child node from the document tree and replaces it with another node.

Get the value of the element

The following code retrieves the text value of the first element:

X=xmlDoc.getElementsByTagName ("title") [0] .childNodes [0]; txt=x.nodeValue; / / should determine whether the node exists first.

Result: txt = "Harry Potter"

Get the value of the attribute

The following code retrieves the text value of the "lang" attribute of the first element:

Txt=xmlDoc.getElementsByTagName ("title") [0] .getAttribute ("lang")

Result: txt = "en"

Change the value of an element

The following code changes the text value of the first element:

X=xmlDoc.getElementsByTagName ("title") [0] .childNodes [0]; x.nodeValue = "Easy Cooking"; change the value of the property

The setAttribute () method can be used to change the value of an existing property or to create a new one.

The following code adds a new attribute named "edition" to each element (the value is "first"):

X=xmlDoc.getElementsByTagName ("book"); for (ID attribute in the iTun0utteri too node

For (I = 0; I < x.resume; iTunes +) {

If (x [I] .getElementsByTagName ("too") [0]! = null) {

If (x [I] .getElementsByTagName ("too") [0] .getAttribute ("id")! = null) {

X [I] .getElementsByTagName ("too") [0] .removeAttribute ("id")

}

}

}

Var xmlHttp

Try {

/ / Firefox, Opera 8.0, Safari

XmlHttp = new XMLHttpRequest ()

}

Catch (e) {

/ / Internet Explorer

Try {

XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP")

}

Catch (e) {

Try {

XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP")

}

Catch (e) {

Alert ("your browser does not support AJAX!")

Return false

}

}

}

XmlHttp.onreadystatechange = function () {/ / onreadystatechange attribute contains functions for handling server responses

If (xmlHttp.readyState = = 4) {/ / readyState attribute stores the status information of the server response

Document.getElementById ("to") [xss_clean] = xmlHttp.responseText; / / retrieve the data returned by the server through the responseText attribute

}

}

XmlHttp.open ("POST", "receive.aspx?type=xmlsave", true)

The / / open () method requires three parameters. The first parameter defines the method used to send the request (GET or POST). The second parameter specifies the URL of the server-side script. The third parameter specifies that the request should be processed asynchronously.

The xmlHttp.send (xmlDoc) / / send () method sends the request to the server

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report