search
HomeWeb Front-endJS TutorialUsing Dom to operate Xml_javascript techniques in Javascript

one. Xml file
2. Introduction to IXMLDOMDocument/DOMDocument
2.1 Properties
2.1.1 parseError
2.1.2 async.
2.1.3 xml
2.1.4 text 3
2.1.5 attributes
2.1.6 nodeName
2.1.7 documentElement
2.1.8 nextSibling
2.1.9 childNodes
2.1 . 10 firstChild
2.1.11 lashChild
2.2 method
2.2.1 loadXML
2.2.2 load
2.2.3 selectSingleNode
2 .2.4 selectNodes
2.2.5 getElementsByTagName
2.2.6 hasChildNodes
3. Example
1. Xml file


c
20

1


2


Implement the code under asp.net:
string str = Server.MapPath("test1.xml");
XmlTextWriter xmlWriter = new XmlTextWriter(str,null);
xmlWriter.Formatting = System.Xml.Formatting.Indented;
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("book");
xmlWriter.WriteAttributeString("level","1");
xmlWriter.WriteElementString("Name","c ");
xmlWriter.WriteElementString ("Price","20");
xmlWriter.WriteStartElement("info");
xmlWriter.WriteElementString("k","1");
xmlWriter.WriteEndElement();
xmlWriter.WriteStartElement("info");
xmlWriter.WriteElementString("k","2");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument( );
xmlWriter.Close();
2. Introduction to IXMLDOMDocument/DOMDocument
2.1 Properties
2.1.1 parseError
Returns an IXMLDOMParseError object that contains information about the last parsing error
Returns an object when parsing the error.
The important ones are parseError.errorCode, parseError.reason
If the path is wrong when loading, the error "The system did not find the specified object" will be returned
2.1.2 async
Specifies whether asynchronous download is permitted
Whether asynchronous downloading is allowed, Boolean value
2.1.3 xml
Contains the XML representation of the node and all its descendants. Read-only.
This point and all derived below All information of the point, read-only. If the xml of the book point is requested, return "c 2012”, if Name is xml, return “c
2.1.4 text
Represents the text content of the node or the concatenated text representing the node and its descendants. Read/write
All node values ​​of this point and all points derived below, readable and writable
20
then text is 20
The text of the "Name" node is "c"
2.1.5 attributes
Contains the list of attributes for this node
Returns the collection of attributes.
2.1.6 nodeName
Returns the qualified name for attribute, document type, element, entity, or notation nodes. Returns a fixed string for all
other node types. Read-only
The Node name
The nodeName of the "Name" node is "Name", and the nodeName of the "book" node is "book"
2.1.7 documentElement
Contains the root element of the document
xml The root node of the xml above the root node
is "book"
2.1.8 nextSibling
Contains the next sibling of the node in the parent's child list. Read-only.
Next Sibling Node, read-only
2.1.9 childNodes
Contains a node list containing the child nodes
All child nodes.
2.1.10 firstChild
Contains the first child of the node
The first child node
2.1.11 lastChild
Returns the last child node
The last child Node
2.2 Methods
2.2.1 loadXML
Loads an XML document using the supplied string
2.2.2 load
Loads an XML document from the specified locati
The path of the parameter is server-side and is a relative path
2.2.3 selectSingleNode
Applies the specified pattern-matching operation to this node's context and returns the first matching node
Returns the first matching item
2.2.4 selectNodes
Applies the specified pattern-matching operation to this node's context and returns the list of matching nodes as IXMLDOMNodeList
All items that meet the conditions.
2.2.5 getElementsByTagName
Returns a collection of elements that have the specified name
Returns a collection of nodes that match the element name
2.2.6 hasChildNodes
Provides a fast way to determine whether a node has children
Determine whether it contains child nodes
The return value is a bool value
Three. Example
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.load("test\test1.xml");
if (xmlDoc .parseError.errorCode!=0)
{
var error = xmlDoc.parseError;
alert(error.reason)
return;
}
var root = xmlDoc.documentElement; //Root node
Form1.test1.value = root.xml;
/*The results are as follows:
c 2012*/
Form1.test1.value = root.nodeName; //The result is "book"
var att = root.attributes; //Get the set of all attributes at this point
var str = "";
for (var i=0; i{
str = att.item(i).nodeName ":" att.item(i).text;
}
Form1.test1.value = str; //There is only one attribute, so the result is "level: 1 ”
var fNode;
var lNode;
var nextSibling;
fNode = root.firstChild; //First child node Name
lNode = root.lastChild; //Last child Node info
nextSibling = fNode.nextSibling; //The next sibling node of the first child node Name, that is, Price
str = fNode.nodeName ":" fNode.text; //Result: "Name:c "
str = lNode.nodeName ":" lNode.text; //The result is: "info:2"
str = nextSibling.nodeName ":" nextSibling.text; //The result is: "Price:20 "
var nodeList;
str = "";
nodeList = xmlDoc.selectNodes("//info"); //Find the node with the element name "info"
for (var j= 0; j{
var infoNode = nodeList.item(j);
var cldNodes = infoNode.childNodes; //Children of the info node Node set
for (var k=0; k{
str = cldNodes.item(k).nodeName ":" cldNodes.item(k).text " " ;
}
//Result "k:1 k:2 "
}
str = "";
var sNode;
sNode = xmlDoc.selectSingleNode("//info "); //Find the first
var scldNodes = sNode.childNodes; //The set of child nodes of the info node
for (var t=0; t{
str = scldNodes.item(t).nodeName ":" scldNodes.item(t).text " ";
}
//Result "k:1"
Form1.test1.value = str;

Statement
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
Replace String Characters in JavaScriptReplace String Characters in JavaScriptMar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup TutorialCustom Google Search API Setup TutorialMar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Example Colors JSON FileExample Colors JSON FileMar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

10 jQuery Syntax Highlighters10 jQuery Syntax HighlightersMar 02, 2025 am 12:32 AM

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

Build Your Own AJAX Web ApplicationsBuild Your Own AJAX Web ApplicationsMar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

8 Stunning jQuery Page Layout Plugins8 Stunning jQuery Page Layout PluginsMar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

What is 'this' in JavaScript?What is 'this' in JavaScript?Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

10  JavaScript & jQuery MVC Tutorials10 JavaScript & jQuery MVC TutorialsMar 02, 2025 am 01:16 AM

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!