XMLDOM and Parsexml Parsing XML implementation code in JavaScript

Source: Internet
Author: User

Usually we have the following requirements:

First step:


We can define a method: (return directly to the parser only with someone's call)

The code is as follows Copy Code
function Parsexml (file) {
Try//internet Explorer---IE browser's parser creation method is as follows:
{
xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
}
catch (e) {
Try//firefox, Mozilla, Opera, etc. Firefox and other browsers to create the way.
{
xmldoc = Document.implementation.createDocument ("", "", null);
}
catch (e) {
alert (e.message);
Return If the creation is unsuccessful, return directly and do not go down.
}
}
Xmldoc.async = false;
Xmldoc.load (file);
return xmldoc; Returns the created parser, passed to the caller.
}

Step Two:

Call the method directly in JS to get the parser:

The code is as follows Copy Code

<script language= "JavaScript" >

Window.onload = function () {

var xmldoc = parsexml ("File.xml");

Call the method we defined above, give the method a parameter, the parameter is the XML file you want to parse, and the object of the file is the equivalent of wrapping the XML file into a document.

}


Parsexml Reference


XML parsing

The code is as follows Copy Code

Xml.newdocument = function (Roottagname, namespaceurl) {
if (!roottagname) roottagname = "";
if (!namespaceurl) Namespaceurl = "";

if (document.implementation && document.implementation.createDocument) {
This is the "standard way to do it"
Return Document.implementation.createDocument (Namespaceurl,
Roottagname, NULL);
}
else {//This are the IE way to do it
Create a empty document as an ActiveX object
If there is no root element, this is all we have to do
var doc = new ActiveXObject ("MSXML2. DOMDocument ");

       //If There is a root tag, initialize the document
&NBSP;&NBSP;&NBSP;&N bsp;    if (roottagname) {
           // Look for a namespace prefix
            var prefix = "";
            var tagname = roottagname;
            var p = roottagname.indexof (': ');
            if (p!=-1) {
                 prefix = roottagname.substring (0, p);
                tagname = Roottagname.substring (p+1);
           }

If we have a namespace, we must have a namespace prefix
If we don ' t have a namespace, we discard any prefix
if (Namespaceurl) {
if (!prefix) prefix = "A0"; What Firefox uses
}
else prefix = "";

Create the root element (with optional namespace) as a
string of text
var text = "<" + (prefix? prefix+ ":"): "") + tagname +
(Namespaceurl
? ("xmlns:" + prefix + ' = "' + Namespaceurl + '")
:"") +
"/>";
and parse that text into the empty document
Doc.loadxml (text);
}
return doc;
}
};

function Loadfromurl (URL) {
Create a new document with the previously defined function
var xmldoc = xml.newdocument ();
Xmldoc.async = false; We want to load synchronously
Xmldoc.load (URL); Load and Parse
return xmldoc; Return the document
}

function xmltostring (xmldoc)
{
var xmlstring;
Try
{
if (navigator.appname = = "Microsoft Internet Explorer")
{
xmlstring = Xmldoc.xml;
}
Else
{
xmlstring = new XMLSerializer (). serializetostring (xmldoc);
}
}
catch (E)
{
xmlstring = null;
}
return xmlstring;
}

function Stringtoxmldoc (str)
{
var xmldoc = null;
Try
{
var xmldomobj = new ActiveXObject ("Microsoft.XMLDOM");
Xmldomobj.async = false;
Xmldomobj.loadxml (str);
xmldoc = Xmldomobj;
}
catch (E)
{
Try
{
var domparser = new Domparser;
xmldoc = domparser.parsefromstring (str, ' text/xml ');
}
catch (E)
{
xmldoc = null;
}
}
return xmldoc;

}

function Stringtoxmldoc (str) {
if (typeof domparser!= "undefined") {
Mozilla, Firefox, and related browsers
Return (new Domparser ()). parsefromstring (Text, "Application/xml");
}
else if (typeof activexobject!= "undefined") {
Internet Explorer.
var doc = xml.newdocument (); Create an empty document
Doc.loadxml (text); Parse text into it
return doc; return it
}
else {
As a resort, try loading the document from a Data:url
This is supposed to work in Safari. To Manos Batsis and
His Sarissa library (sarissa.sourceforge.net) is for this technique.
var url = "Data:text/xml;charset=utf-8," + encodeuricomponent (text);
var request = new XMLHttpRequest ();
Request.open ("Get", url, false);
Request.send (NULL);
return request.responsexml;
}
}


</script>

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.