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

May 16, 2016 pm 07:01 PM
dom xml

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);

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1238
24
Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Handling errors and exceptions in XML using Python Handling errors and exceptions in XML using Python Aug 08, 2023 pm 12:25 PM

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

How to handle XML and JSON data formats in C# development How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Python parsing special characters and escape sequences in XML Python parsing special characters and escape sequences in XML Aug 08, 2023 pm 12:46 PM

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Using Python to implement data verification in XML Using Python to implement data verification in XML Aug 10, 2023 pm 01:37 PM

Using Python to implement data validation in XML Introduction: In real life, we often deal with a variety of data, among which XML (Extensible Markup Language) is a commonly used data format. XML has good readability and scalability, and is widely used in various fields, such as data exchange, configuration files, etc. When processing XML data, we often need to verify the data to ensure the integrity and correctness of the data. This article will introduce how to use Python to implement data verification in XML and give the corresponding

See all articles