Javascript, xml dom, Object

Source: Internet
Author: User
Tags tag name xml attribute

 

Java SDK supports xml dom, which is similar to any other feature in browser compatibility.

Xml dom in IE
1. Microsoft provides support through the ActiveX MSXML Library, through:
VaR oxmldom = new activexobject ("msxml2.domdocument. 5.0 ") Get an xml dom object, which is in IE6. If your IE version is older, you can use the following function. If MSXML is not installed, cannot get:

Function createxmldom (){

VaR arrsignatures = ["msxml2.domdocument. 5.0", "msxml2.domdocument. 4.0 ",
"Msxml2.domdocument. 3.0", "msxml2.domdocument ",
"Microsoft. xmldom"];

For (VAR I = 0; I <arrsignatures. length; I ++ ){
Try {

VaR oxmldom = new activexobject (arrsignatures [I]);

Return oxmldom;

} Catch (oerror ){
// Ignore
}
}

Throw new error ("MSXML is not installed in your system ");
}
Of course, if you use the prototype library, you can use the try. These function.

2. xml dom objects can be loaded into XML files or strings through the load and loadxml methods:
Oxmldom. Load ("test. xml ");
Oxmldom. loadxml ("<root> </root>"); then this oxmldom can use all DOM object methods, such as documentelement. tagname, see:
DOM technology in JavaScript (1)
DOM technology in JavaScript (II)

3. xml dom loads XML files asynchronously by default. You can set the async value to synchronous or asynchronous:
Oxmldom. async = true;

4. xml dom of IE has a readystate value to indicate the state of the loaded file:

0 -- prepare for loading
1 -- Loading
2 -- Load completed
3 -- load is complete and available, but some data may be unavailable
4 -- fully loaded and fully available.

There is an onreadystatechange event. When the status changes, we can monitor this event to determine the availability of the xml dom object.
Oxmldom. onreadystatechange = function (){
If (oxmldom. readystate = 4 ){
Alert ("load test. xml done! ");
Alert ("Tag Name of the root element is" + oxmldom.doc umentelement. tagname );
Alert ("the root element has this has children:" + oxmldom.doc umentelement. childnodes. Length );

}
};
5. the xml dom object of IE has an XML Attribute that is used to return the string form of the XML file, such
Oxmldom. async = false;
Oxmldom. Load ("test. xml ");
Alert (oxmldom. XML );
Alert output: <root> <child/> </root>

6. ie: When an XML file or string is incorrectly parsed, A parseerror object is generated. The following Code demonstrates the attributes of this object:
Oxmldom. async = false;
Oxmldom. Load ("errors. xml ");

// 0 indicates no error
If (oxmldom. parseerror! = 0 ){
VaR oerror = oxmldom. parseerror;

Alert ("an error occurred:/n Error Code :"
+ Oerror. errorcode + "/N"
+ "Number of rows:" + oerror. Line + "/N"
+ "Number of columns:" + oerror. linepos + "/N"
+ "Cause:" + oerror. Reason );

}

2. Mozilla's xml dom object
1. Create an xml dom object that complies with the DOM standard. Use the document. Implementation. createdocument () method. For example:
VaR oxmldom = Document. Implementation. createdocument ("", "", null );
These three parameters are the document namespace, the tag name of the document element, and a document type object (always null), for example:
VaR oxmldom = Document. Implementation. createdocument ("Http://www.rubyeye.net "," root ", null);
This Code creates a <A0: Root xmlns ="Http://www.rubyeye.net "/> Xml dom object

2. load XML. Unlike IE, Mozilla only provides a load () method for loading XML files, but does not provide the loadxml () method for loading XML strings. The code for synchronously loading XML files is the same as that for IE:
Oxmldom. async = false;
Oxmldom. Load ("test. xml ");
Asynchronous loading is slightly different because Mozilla does not support the readystate attribute and there is no onreadystatechange event. It only has one onload event, which is triggered when loading is complete; or the readystate attribute equivalent to IE is equal to 4.
Oxmldom. onload = function (){
Alert ("done ");
}
Oxmldom. Load ("test. xml ");
To resolve an XML string to a DOM object, you must use the domparser object:
VaR oparser = new domparser ();
VaR oxmldom = oparser. parsefromstring ("<root> <child/> </root>," text/XML ");
Two parameters: the XML string to be parsed and the content type of the string (only text/XML or application/XML ).
However, we can implement our own loadxml method:
Document. Prototype. loadxml = function (sxml ){

VaR oparser = new domparser ();
VaR oxmldom = oparser. parsefromstring (sxml, "text/XML ");

// Delete the original document content
While (this. firstchild ){
This. removechild (this. firstchild );
}
// Import new document content
For (VAR I = 0; I <oxmldom. childnodes. length; I ++ ){
VaR onewnode = This. importnode (oxmldom. childnodes [I], true );
This. appendchild (onewnode );
}

};

3. Mozilla does not provide XML attributes for IE to return XML document content. You can only use the xmlserializer object:
VaR oserializer = new xmlserializer ();
VaR sxml = oserializer. serializetostring (oxmldom, "text/XML"); the same two parameters: xml dom object and converted document type.

Similarly, we can define an attribute XML for Mozilla's xml dom object by using the definegetter method:
Node. Prototype. _ definegetter _ ("XML", function (){
VaR oserializer = new xmlserializer ();
VaR sxml = oserializer. serializetostring (this, "text/XML ");
});
In the future, you can use oxmldom. XML in IE mode to obtain the XML document content.

4. Unlike IE, Mozilla returns a piece of code when parsing an error. The label <prasereoor> includes the code to explain the cause and location of the error, we only need to parse this code segment through regular expressions to extract error messages.
VaR reerror =/> ([/S] *?) Location :( [/S] *?) Line number (/d +), column (/d +): <sourcetext> ([/S] *?) (? :/-*/^ )/;

// The label of the returned code is parsererror, indicating that an error occurs.
If (oxmldom.doc umentelement. tagname = "parsererror "){
Reerror. Test (oxmldom. XML );
Alert ("an error occurred:/n Description :"
+ Regexp. $1 + "/N"
+ "File name:" + Regexp. $2 + "/N"
+ "Number of rows:" + Regexp. $3 + "/N"
+ "Number of columns:" + Regexp. $4 + "/N"
+ "Reason:" + Regexp. $5 );
}
3. provides a cross-browser xml dom object solution from JavaScript Advanced Programming

Function xmldom (){
// Use the object/attribute detection method to determine whether IE is Mozilla
If (window. activexobject ){
VaR arrsignatures = ["msxml2.domdocument. 5.0", "msxml2.domdocument. 4.0 ",
"Msxml2.domdocument. 3.0", "msxml2.domdocument ",
"Microsoft. xmldom"];

For (VAR I = 0; I <arrsignatures. length; I ++ ){
Try {

VaR oxmldom = new activexobject (arrsignatures [I]);

Return oxmldom;

} Catch (oerror ){
// Ignore
}
}

Throw new error ("MSXML is not installed on your system .");

// Same as above
} Else if (document. Implementation & document. Implementation. createdocument ){

VaR oxmldom = Document. Implementation. createdocument ("", "", null );

// Create a parseerror object for Mozilla
Oxmldom. parseerror = {
Valueof: function () {return this. errorcode ;},
Tostring: function () {return this. errorcode. tostring ()}
};

// Initialize the parseerror object
Oxmldom. _ initerror __();


Oxmldom. addeventlistener ("LOAD", function (){
This. _ checkforerrors __();
This. _ changereadystate _ (4 );
}, False );

Return oxmldom;

} Else {
Throw new error ("your browser doesn't support an xml dom object .");
}
}

// A browser system in this book is used to detect JS files.
If (ismoz ){
Document. Prototype. readystate = 0;
Document. Prototype. onreadystatechange = NULL;

Document. Prototype. _ changereadystate _ = function (ireadystate ){
This. readystate = ireadystate;

If (typeof this. onreadystatechange = "function "){
This. onreadystatechange ();
}
};
// Initialize the parseerror object
Document. Prototype. _ initerror _ = function (){
This. parseerror. errorcode = 0;
This. parseerror. filepos =-1;
This. parseerror. Line =-1;
This. parseerror. linepos =-1;
This. parseerror. Reason = NULL;
This. parseerror. srctext = NULL;
This. parseerror. url = NULL;
};

Document. Prototype. _ checkforerrors _ = function (){
If (this.doc umentelement. tagname = "parsererror "){
VaR reerror =/> ([/S] *?) Location :( [/S] *?) Line number (/d +), column (/d +): <sourcetext> ([/S] *?) (? :/-*/^ )/;
Reerror. Test (this. XML );
This. parseerror. errorcodes =-999999;
This. parseerror. Reason = Regexp. $1;
This. parseerror. url = Regexp. $2;
This. parseerror. Line = parseint (Regexp. $3 );
This. parseerror. linepos = parseint (Regexp. $4 );
This. parseerror. srctext = Regexp. $5;
}
};

// Define Mozilla's loadxml Method
Document. Prototype. loadxml = function (sxml ){
This. _ initerror __();
This. _ changereadystate _ (1 );
VaR oparser = new domparser ();
VaR oxmldom = oparser. parsefromstring (sxml, "text/XML ");
While (this. firstchild ){
This. removechild (this. firstchild );
}
For (VAR I = 0; I <oxmldom. childnodes. length; I ++ ){
VaR onewnode = This. importnode (oxmldom. childnodes [I], true );
This. appendchild (onewnode );
}
// Check for errors after loading
This. _ checkforerrors __();
// No problem. Set the readystate attribute to 4.
This. _ changereadystate _ (4 );

};
Document. Prototype. _ load _ = Document. Prototype. load;
Document. Prototype. load = function (Surl ){
This. _ initerror __();
This. _ changereadystate _ (1 );
This. _ load _ (Surl );
};
Node. Prototype. _ definegetter _ ("XML", function (){
VaR oserializer = new xmlserializer ();
Return oserializer. serializetostring (this, "text/XML ");

 

Related Article

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.