XML Starter Tutorial: XML parser

Source: Internet
Author: User
Tags array contains functions getmessage insert string window access
xml| Tutorials | Getting Started

XML parsers are required to read, update, create, or manipulate an XML document.

Instance

parsing XML files-cross-browser instances

This example is a cross browser instance that loads an XML document ("Note.xml") into an XML parser.

<body>
<script type= "Text/vbscript" >
Set Xmldoc=createobject ("Microsoft.XMLDOM")
Xmldoc.async= "false"
Xmldoc.load ("/example/xmle/xmle_xml_note.xml")
document.write ("For each x in XmlDoc.documentElement.childNodes
document.write ("<b>" & X.nodename & "</b>")
document.write (":")
document.write (X.text)
document.write ("<br><br>")
Next
</script>
</body>

parsing an XML string-a cross-browser instance

This example is a cross browser instance that shows how to load and parse an XML string.

<body>
<script type= "Text/javascript" >
var xmldoc = new ActiveXObject ("Microsoft.XMLDOM")
Xmldoc.async= "false"
Xmldoc.load ("/example/xmle/xmle_xml_note.xml")
document.write
("The" the "the" the "the" file contains: ")
document.write
(XmlDoc.documentElement.childNodes.item (0). Text)
</script>
</body>

Parsing an XML document

If you want to manipulate an XML document, you need an XML parser. The parser loads the document into the computer's memory. Once a document is loaded, its data can be manipulated using the DOM. DOM handles XML as a tree.

Microsoft's XML parser differs from the parser used in Mozilla browsers. In this tutorial, we'll show you how to create Cross-browser scripts that work in IE and Mozilla browsers.

Microsoft's XML Parser

Microsoft's XML parser is a COM component that exists in IE 5.0 or later. Once you have installed IE, you can use your script to use the parser.

Microsoft's XML parser supports all the necessary functions to traverse the node tree, access nodes and their property values, insert and delete nodes, and convert the number of nodes back to XML.

To create an instance of the Microsoft XML Parser, use the following code:

Javascript:
var xmldoc=new activexobject ("Microsoft.XMLDOM");
VBScript:
Set Xmldoc=createobject ("Microsoft.XMLDOM")
Asp:
Set Xmldoc=server.createobject ("Microsoft.XMLDOM")

The following code snippet can load an existing XML document ("Note.xml") into Microsoft's XML parser:

var xmldoc=new activexobject ("Microsoft.XMLDOM");
Xmldoc.async= "false";
Xmldoc.load ("Note.xml");

The first line of the script above creates an instance of the XML parser. The second line closes the synchronization load, which ensures that the parser does not continue to execute until the document is fully loaded. The third row tells the parser to load the XML document named "Note.xml."

The XML parser in Mozilla, Firefox, and opera

The Mozilla browser's XML parser supports all the necessary functions to traverse the node tree, access nodes and their property values, insert and delete nodes, and convert the node tree back to XML.

To create an instance of the XML parser for Mozilla browsers, use the following code:

Javascript:
var xmldoc=document.implementation.createdocument ("ns", "root", null);

The first parameter, NS, defines the namespace (namespace) that is used for the XML document. The second argument, root, is the XML root element in the XML file. The third argument, NULL, is always NULL, because this parameter is not currently used.

The following code snippet loads an existing XML document ("Note.xml") into the XML parser of the Mozilla browser:

var xmldoc=document.implementation.createdocument ("", "", null);
Xmldoc.load ("Note.xml");

The first line of the script above creates an instance of the XML parser. The second row tells the parser to load the XML document named "Note.xml."

Parsing an XML file-a cross-browser instance

The following example is a cross browser instance that loads an existing XML document ("Note.xml") into an XML parser:

<script type= "Text/javascript" >
var xmldoc;
function Loadxml ()
{
Code for IE:
if (window. ActiveXObject)
{
Xmldoc=new ActiveXObject ("Microsoft.XMLDOM");
Xmldoc.async=false;
Xmldoc.load ("Note.xml");
GetMessage ();
}
For Mozilla, Firefox, Opera, and other browsers code:
else if (document.implementation &&
Document.implementation.createDocument)
{
Xmldoc=document.implementation.createdocument ("", "", null);
Xmldoc.load ("Note.xml");
Xmldoc.onload=getmessage;
}
Else
{
Alert (' Your browser cannot handle this script ');
}
}
function GetMessage ()
{
document.getElementById ("to"). innerhtml=
Xmldoc.getelementsbytagname ("to") [0].childnodes[0].nodevalue;
document.getElementById ("from"). innerhtml=
Xmldoc.getelementsbytagname ("from") [0].childnodes[0].nodevalue;
document.getElementById ("message"). innerhtml=
Xmldoc.getelementsbytagname ("Body") [0].childnodes[0].nodevalue;
}
</script>
<body >
<p><b>To:</b> <span id= "to" ></span><br/>
<b>From:</b> <span id= "from" ></span><br/>
<b>Message:</b> <span id= "message" ></span>
</p>
</body>

Output:

W3Schools Internal Note
To:tove
From:jani
Message:don ' t forget me this weekend!

Important Notes

To extract text (such as Jani) from an XML element (such as a <from>Jani</from>), use the following syntax:

getElementsByTagName ("from") [0].childnodes[0].nodevalue

Important: getElementsByTagName will return an array of nodes. This array contains all the elements in the XML document that have the specified name. In this example, there is only one "from" element, but you still need to set the subscript for the array ([0]).

Parsing an XML string-a cross-browser instance

The following code is a cross browser instance that shows us how to load and parse an XML string:

<body>
<script type= "Text/javascript" >
var text= "<note>";
text=text+ "<to>Tove</to>";
text=text+ "<from>Jani</from>";
text=text+ "text=text+ "<body>don ' t forget me this weekend!</body>";
text=text+ "</note>";
Code for IE:
if (window. ActiveXObject)
{
var doc=new activexobject ("Microsoft.XMLDOM");
Doc.async= "false";
Doc.loadxml (text);
}
For Mozilla, Firefox, Opera, and other browsers code:
Else
{
var parser=new domparser ();
var doc=parser.parsefromstring (text, "Text/xml");
}
DocumentElement commit the root element:
var x=doc.documentelement;
document.write ("Text of the" of the ' A-child element: ");
document.write (X.childnodes[0].childnodes[0].nodevalue);
document.write ("<br/>");
document.write ("Text of second child element:");
document.write (X.childnodes[1].childnodes[0].nodevalue);
</script>
</body>

Output:

Text of the Element:tove
Text of second child Element:jani

Note: Internet Explorer uses the Loadxml () method to parse XML strings, while Mozilla browsers use Domparser objects.



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.