Home > Web Front-end > JS Tutorial > A more detailed explanation of XMLDOM object methods_javascript skills

A more detailed explanation of XMLDOM object methods_javascript skills

PHP中文网
Release: 2016-05-16 19:01:37
Original
1004 people have browsed it

Mainly introduces some methods of using xml dom objects

Abort method
Function
The abort method cancels an asynchronous download in progress
Basic syntax
xmlDocument.abort();

Explanation
If this method is asynchronous Called during download, all parsing operations will stop, and the file in memory will be released.

Example
xmlDocument
--------------------------------------------- --------------------------------------------------
AppendChild method
functions
to add a node as the last child node of the specified node.
Basic syntax
xmlDocumentNode.appendChild(newChild);

Explanation
newChild is the address of the appended child node.

Example
docObj = xmlDoc.documentElement;
alert(docObj.xml);
objNewNode = docObj.appendChild(xmlDoc.documentElement.firstChild);
alert(docObj.xml );
------------------------------------------------ -------------------------------------
cloneNode method
Function

Basic syntax
xmlDocumentNode.cloneNode(deep);

Explanation
deep is a Boolean value. If true, this node will copy all nodes developed from the specified node. If false, only the specified node and its attributes are copied.

Example
currNode = xmlDoc.documentElement.childNodes.item(1);
objClonedNode = currNode.cloneNode(1);
alert(objClonedNode.xml);
-- -------------------------------------------------- ----------------------------------
createAttribute method
functions
to create an attribute with a specified name property.
Basic syntax
xmlDocument.createAttribute(name);

Explanation
name is the name of the attribute being created.

Example
objNewAtt = xmlDoc.createAttribute("encryption");
alert(objNewAtt.xml);
---------------- -------------------------------------------------- -------------------
createCDATASection method
Function

Basic syntax
xmlDocument.createCDATASection(data);

Explanation
date is a string and contains the data placed in CDATA.

Example
objNewCDATA = xmlDoc.createCDATASection("This is a CDATA Section");
alert(objNewCDATA.xml);
------------ -------------------------------------------------- -----------------------
createComment method
Function

Basic syntax
xmlDocument.createComment(data);

Explanation
data is a string and contains the data placed in the annotation.

Example
objNewComment = xmlDoc.createComment("This is a comment");
alert(objNewComment.xml);
------------- -------------------------------------------------- --------------------------
createDocumentFragment method
functions
to create an empty file fragment object.
Basic syntax
xmlDocument.createDocumentFragment();

Explanation
A new file fragment is created but not added to the file tree. To add a fragment to the file tree, you must use an insert method such as insertBefore, replaceChild, or appendChild.

Example
objNewFragment = xmlDoc.createDocumentFragment();
alert(objNewFragment.xml);
----------------- -------------------------------------------------- ----------------
createElement method
functions
to create an element with a specified name.
Basic syntax
xmlDocument.createElement(tagName);

Explanation
tagName is a case-sensitive string to specify the new element name.

Example
objNewElement = xmlDoc.createElement("TO");
alert(objNewElement.xml);
---------------- -------------------------------------------------- -------------------
createEntityReference method
functions
to create an entity with a reference to the specified name.
Basic syntax
xmlDocument.createEntityReference(name);

Description
name is a case-sensitive string to specify the name of the new entity reference. A new entity reference is created but not added to the file tree. To add an entity reference to the file tree, you must use an insertion method, such as insertBefore, replaceChild, or appendChild.
Example
objNewER = xmlDoc.createEntityReference("eRef");
alert(objNewER.xml);
----------------- -------------------------------------------------- ----------------

load method
Function
represents a file loaded from the specified location.
Basic syntax
boolValue = xmlDocument.load(url);

Description
url is a string containing the URL of the file to be loaded. If the file is loaded successfully, the returned value is true. If loading fails, the returned value is false.

Example
boolValue = xmlDoc.load("LstA_1.xml");
alert(boolValue);
---------------- -------------------------------------------------- -------------------
loadXML method
Function
Loads an XML file or a fragment of a string.
Basic syntax
boolValue = xmlDocument.loadXML(xmlString);

Explanation
xmlString is a string containing XML literal code.

Example
xmlString = "Hello!";
boolValue = xmlDoc.loadXML(xmlString);
alert(boolValue);
--------------------------------------------- ---------------------------------------------
nodeFromID method
Function
Returns the node whose node ID matches the specified value.
Basic syntax
xmlDocumentNode = xmlDocument.nodeFromID(idString);

Explanation
idString is a string containing the ID value. The matching node must be of ID type. If it matches, an object will be returned; if the operation fails, null will be returned.

Example
objDocumentNode = xmlDoc.nodeFromID("TO");
alert(objDocumentNode);
---------------- -------------------------------------------------- ------------------
parsed method
function
will verify whether the specified node (node) and its derived child nodes (descendants) have been Parsed.
Basic syntax
boolValue = xmlDocumentNode.parsed();

Explanation
If all nodes have been parsed, the returned value is true; if any node has not yet been is parsed, the returned value is false.

Example
currNode = xmlDoc.documentElement.childNodes.item(0);
boolValue = currNode.parsed();
alert(boolValue);
----- -------------------------------------------------- -------------------------------
removeChild method
Function
will remove the specified node from the node list removed.
Basic syntax
objDocumentNode = xmlDocumentNode.removeChild(oldChild);

Explanation
oldChild is an object containing the node to be removed.

Example
objRemoveNode = xmlDoc.documentElement.childNodes.item(3);
alert(xmlDoc.xml);
xmlDoc.documentElement.removeChild(objRemoveNode);
alert( xmlDoc.xml);
--------------------------------------------- ---------------------------------------------
replaceChild method
Function
Replace the specified old child node with the provided new child node.
Basic syntax
objDocumentNode = xmlDocumentNode.replaceChild(newChild,oldChild);

Explanation
newChild is an object containing new child nodes. If this parameter is null, the old child node will be removed but not replaced. oldChild is an object containing old child nodes.

Example
objOldNode = xmlDoc.documentElement.childNodes.item(3);
objNewNode = xmlDoc.createComment("I've replaced the BCC element.");
alert(xmlDoc .xml);
xmlDoc.documentElement.replaceChild(objNewNode,objOldNode);
alert(xmlDoc.xml);
----------------- -------------------------------------------------- ----------------

selectNodes method
Function
Returns all nodes that match the provided pattern (pattern).
Basic syntax
objDocumentNodeList = xmlDocumentNode.selectNodes(patternString);

Explanation
patternString is a string containing XSL style. This method returns a node list object containing nodes that match the style. If there are no matching nodes, an empty manifest list is returned.

Example
objNodeList=xmlDoc.selectNodes("/");
alert(objNodeList.item(0).xml);
---------- -------------------------------------------------- --------------------------
createNode method
Function
Create a new node with the specified type, name, and namespace .
Basic syntax
xmlDocument.createNode(type, name, nameSpaceURI);

Explanation
type is used to confirm the node type to be created, name is a string to confirm the new node The name, namespace prefix is ​​optional. nameSpaceURI is a string that defines the namespace URI. If a prefix is ​​included in the name parameter, the node will be created with the specified prefix in the context of the nameSpaceURI. If no prefix is ​​included, the specified namespace is treated as the default namespace.

Example
objNewNode = xmlDoc.createNode(1, "TO", "");
alert(objNewNode.xml);
---------- -------------------------------------------------- --------------------------
createProcessingInstruction method
Function
Create a new processing instruction, containing the specified target and data .
Basic syntax
xmlDocument.createProcessingInstruction(target, data);

Explanation
target is a string representing the target, name or processing instruction. Data is a value representing a processing instruction. A new processing instruction is created but not added to the file tree. To add processing instructions to the file tree, you must use insert methods, such as insertBefore, replaceChild, or appendChild.

Example
objNewPI =xmlDoc.createProcessingInstruction('XML', 'version="1.0"');
alert(objNewPI.xml);
-------- -------------------------------------------------- -----------------------------
createTextNode method
Function
Create a new text node and contain the specified data.
Basic syntax
xmlDocument.createTextNode(data);

Explanation
data is a string representing a new text node. A new text node is created but not added to the file tree. To add nodes to the file tree, you must use the insertion method, such as insertBefore, replaceChild or appendChild.

Example
objNewTextNode = xmlDoc.createTextNode("This is a text node.");
alert(objNewTextNode.xml);
---------- -------------------------------------------------- --------------------------

getElementsByTagName method
Function
returns a collection of elements with the specified name.
Basic syntax
objNodeList = xmlDocument.getElementsByTagName(tagname);

Explanation
tagname is a string representing the tag name of the found element. Use tagname "*" to return all found elements in the file.

Example
objNodeList = xmlDoc.getElementsByTagName("*");
alert(objNodeList.item(1).xml);
---------- -------------------------------------------------- --------------------------
haschildnodes method
Function
If the specified node has one or more child nodes, return The value is true.
Basic syntax
boolValue = xmlDocumentNode.hasChildNodes();

Explanation
If this node has child nodes, the return value is true, otherwise it returns a false value.

Example
boolValue = xmlDoc.documentElement.hasChildNodes();
alert(boolValue);
----------------- -------------------------------------------------- ----------------

insertBefore method
Function
Insert a child node before the specified node.
Basic syntax
objDocumentNode = xmlDocumentNode.insertBefore(newChild,refChild);

Explanation
newChild is an object containing the address of the new child node, and refChild is the address of the reference node. The new child node is inserted before the reference node. If the refChild parameter is not included, the new child node will be inserted at the end of the child node list.

Example
objRefNode = xmlDoc.documentElement;
alert(xmlDoc.xml);
objNewNode = xmlDoc.createComment("This is a comment");
xmlDoc.insertBefore( objNewNode, objRefNode);
alert(xmlDoc.xml);

-------------------------------- -------------------------------------------------- ------
selectSingleNode returns the first node that matches the style.
Function
Returns the first node that matches the style.
Basic syntax
objDocumentNode = xmlDocumentNode.selectSingleNode(patternString);

Explanation
patternString is a string containing XSL style. This method will return the first matching node object, or null if there is no matching node.

Example
objNode = xmlDoc.selectSingleNode("EMAIL/BCC");
alert(objNode.xml);
------------- -------------------------------------------------- --------------------------

transformNode method
Function
Use the provided style sheet to process the node and its child nodes.
Basic syntax
strTransformedDocument = xmlDocumentNode.transformNode(stylesheet);

Explanation
stylesheet is an XML file or fragment that contains XSL elements responsible for node transformation. This method returns a string containing the conversion result.

Example
var style = new ActiveXObject("Microsoft.XMLDOM");
style.load("LstA_49.xsl");
strTransform = xmlDoc.transformNode(style.documentElement) ;
alert(strTransform);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template