The previous article describes the structure of XML and the relationship between nodes
This article describes the browser-built XML parser and how JavaScript is loading XML.
Most browsers have built-in XML parsers that read and manipulate XML.
The parser (XML Parser) transforms XML into an object that JavaScript can access.
The parser loads the XML into memory and then transforms it into an XML DOM object that can be accessed through JavaScript.
There are some differences between Microsoft's XML parser and the parser in other browsers. Microsoft's parser supports the loading of XML files and XML strings (text), while other browsers use separate parsers. However, all parsers contain functions that traverse the XML tree, access the INSERT and delete nodes (elements), and their attributes.
Note: When we talk about XML parsing, we often use terminology about XML elements: nodes.
First, IE through Microsoft's XML parser to load XML
Microsoft's XML parser is built in Internet Explorer 5 and later.
The following JavaScript fragment loads an XML document into the parser:
var xmldoc=new activexobject ("Microsoft.XMLDOM"); xmldoc.async= "false"; Xmldoc.load ("Note.xml");
Example Explanation:
- The first line of the above code creates an empty Microsoft XML Document object.
- The second line turns off asynchronous loading, which ensures that the parser does not continue the execution of the script until the document is fully loaded.
- The third line tells the parser to load an XML document named "Note.xml."
The following JavaScript fragment loads the string txt into the parser:
var xmldoc=new activexobject ("Microsoft.XMLDOM"); xmldoc.async= "false"; xmldoc.loadxml (TXT);
Note: theLoadXML () method is used to load the string (text), and load() is used to load the file.
Second, the XML parser in Firefox and other browsers
The following JavaScript fragment loads the XML document ("Note.xml") into the parser:
var xmldoc=document.implementation.createdocument ("", "", null); xmldoc.async= "false"; Xmldoc.load ("Note.xml");
Example Explanation:
- The first line of the above code creates an empty XML Document object.
- The second line turns off asynchronous loading, which ensures that the parser does not continue the execution of the script until the document is fully loaded.
- The third line tells the parser to load an XML document named "Note.xml."
The following JavaScript fragment loads the string txt into the parser:
var parser=new domparser (), var xmldoc=parser.parsefromstring (txt, "text/xml");
Example Explanation:
- The first line of the above code creates an empty Microsoft XML Document object.
- The second line tells the parser to load a string named TXT.
Note:the Parsefromstring () method of the Domparser object is used to load the string (text), and load() is used to load the file.
Third, cross-domain access
For security reasons, modern browsers do not allow cross-domain access.
If you are going to use the above example on your own web page, you must put the XML file on your server. Otherwise, xmldoc.load () will produce the error "Access is denied".
JavaScript operations XML (II)