JavaScript operations XML (II)

Source: Internet
Author: User
Tags xml parser

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:
    1. The first line of the above code creates an empty Microsoft XML Document object.
    2. 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.
    3. 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:
    1. The first line of the above code creates an empty XML Document object.
    2. 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.
    3. 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:
    1. The first line of the above code creates an empty Microsoft XML Document object.
    2. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.