XML processing functions

Source: Internet
Author: User
Tags xml dom document xslt
<! --
/*------------------------------------------------------------------------------
*
Javascript zxml Library
* Version 1.0.2
* By Nicolas C. zakas, http://www.nczonline.net/
* Copyright (c) 2004-2006 Nicholas
C. zakas. All rights reserved.
*
* This program is free software; you can
Redistribute it and/or modify
* It under the terms of the GNU lesser General
Public License as published
* The Free Software Foundation; either version
2.1 of the license, or
* (At your option) any later version.
*
* This
Program is distributed in the hope that it will be useful,
* But without any
Warranty; without even the implied warranty
* Merchantability or fitness
For a special purpose. See
* GNU lesser General Public License
More details.
*
* You shoshould have written ed a copy of the GNU lesser
General Public License
* Along with this program; if not, write to the free
Software
* Foundation, inc., 59 temple place, Suite 330, Boston,
Ma 02111-1307
USA
*------------------------------------------------------------------------------
*/

VaR
Zxml/*: object */= {
Useactivex: (typeof activexobject! =
"Undefined "),
Usedom: Document. Implementation &&
Document. Implementation. createdocument,
Usexmlhttp: (typeof XMLHttpRequest! = "Undefined ")
};

Zxml. arr_xmlhttp_vers/*: array */= ["msxml2.xmlhttp. 6.0", "msxml2.xmlhttp. 3.0"];

Zxml. arr_dom_vers/*: array */=
["Msxml2.domdocument. 6.0", "msxml2.domdocument. 3.0"];

/**
*
Static class for handling XMLHTTP
Creation.
* @ Class
*/
Function zxmlhttp ()
{
}

/**
* Creates an XMLHTTP
Object.
* @ Return an XMLHTTP object.
*/
Zxmlhttp. createrequest = Function
()/*: XMLHTTP */{

// If it natively supports XMLHTTPRequest object
If (zxml. usexmlhttp)
{
Return new XMLHttpRequest ();
} Else if (zxml. useactivex) {// IE
<7.0 = Use ActiveX

If (! Zxml. xmlhttp_ver ){
For (VAR I = 0; I <zxml. arr_xmlhttp_vers.length; I ++ ){
Try
{
New activexobject (zxml. arr_xmlhttp_vers [I]);
Zxml. xmlhttp_ver = zxml. arr_xmlhttp_vers [I];
Break;
}
Catch (oerror)
{
}
}
}

If
(Zxml. xmlhttp_ver ){
Return new activexobject (zxml. xmlhttp_ver );
} Else {
Throw new
Error ("cocould not create xml http request .");
}
} Else
{
Throw new error ("your browser doesn't support an XML HTTP
Request .");
}

};

/**
* Indicates if XMLHTTP
Is available.
* @ Return true if XMLHTTP
Is available, false if not.
*/
Zxmlhttp. issupported = Function
()/*: Boolean */{
Return zxml. usexmlhttp |
Zxml. useactivex;
};

/**
* Static class for handling XML DOM
Creation.
* @ Class
*/
Function zxmldom (){

}

/**
*
Creates an xml dom document.
* @ Return an XML DOM
Document.
*/
Zxmldom. createdocument = function ()/*: xmldocument */{

If (zxml. Usedom)
{

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

Oxmldom. parseerror
= {
Valueof: function () {return this. errorcode;
},
Tostring: function () {return this. errorcode. tostring ()
}
};

Oxmldom. _ initerror __();

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

Return oxmldom;

} Else if
(Zxml. useactivex ){
If (! Zxml. dom_ver ){
For (VAR I = 0;
I <zxml. arr_dom_vers.length; I ++ ){
Try
{
New
Activexobject (zxml. arr_dom_vers [I]);
Zxml. dom_ver =
Zxml. arr_dom_vers [I];
Break;
} Catch
(Oerror)
{
}
}
}

If
(Zxml. dom_ver ){
Return new
Activexobject (zxml. dom_ver );
} Else {
Throw new
Error ("cocould not create xml dom document .");
}
} Else
{
Throw new error ("your browser doesn't support an XML DOM
Document .");
}

};

/**
* Indicates if an XML DOM
Is available.
* @ Return true if XML DOM
Is available, false if not.
*/
Zxmldom. issupported = Function
()/*: Boolean */{
Return zxml. Usedom |
Zxml. useactivex;
};

// Code to make Mozilla Dom documents act more like
MS Dom documents.
VaR omozdocument = NULL;
If (typeof xmldocument! = "Undefined "){
Ow.document = xmldocument;
} Else if (typeof document! = "Undefined ")
{
Ow.document = document;
}

If (ow.document &&
! Window. Opera ){

Ow.document. Prototype. readystate =
0;
Ow.document. Prototype. onreadystatechange =
NULL;

Ow.document. Prototype. _ changereadystate _ = Function
(Ireadystate ){
This. readystate = ireadystate;

If
(Typeof this. onreadystatechange = "function ")
{
This. onreadystatechange ();
}
};

O1_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;
};

Ow.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. errorcode
=-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;
}
};

Ow.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 );
}

This. _ checkforerrors __();

This. _ changereadystate _ (4 );

};

O1_document. Prototype. _ load __
= Ow.document. Prototype. load;

Ow.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 ");
});

Node. Prototype. _ definegetter _ ("text ",
Function (){
VaR stext = "";
For (VAR I = 0; I <
This. childnodes. length; I ++ ){
If
(This. childnodes [I]. haschildnodes ()){
Stext + =
This. childnodes [I]. text;
} Else {
Stext + =
This. childnodes [I]. nodevalue;
}
}
Return
Stext;

});

}

/**
* Static class for handling XSLT
Transformations.
* @ Class
*/
Function zxslt (){
}

/**
*
Transforms an xml dom to text using an XSLT Dom.
* @ Param oxml the XML DOM
To transform.
* @ Param oxslt the XSLT Dom to use for the transformation.
*
@ Return the transformed version of the string.
*/
Zxslt. transformtotext =
Function (oxml/*: xmldocument */, oxslt/*: xmldocument */)/*: string */{
If (typeof processing tprocessor! =
"Undefined "){
VaR oprocessor = new
Effectprocessor ();
Oprocessor. importstylesheet (oxslt );

VaR
Oresultdom = oprocessor. transformtodocument (oxml );
VaR sresult =
Oresultdom. xml;

If
(Sresult. indexof ("<transformiix: result")>-1 ){
Sresult =
Sresult. substring (sresult. indexof (">") + 1,

Sresult. lastindexof ("<"));
}

Return
Sresult;
} Else if (zxml. useactivex ){
Return
Oxml. transformnode (oxslt );
} Else {
Throw new error ("No XSLT
Engine found .");
}
};

/**
* Static class for handling xpath
Evaluation.
* @ Class
*/
Function zxpath (){

}

/**
*
Selects the first node matching a given XPath expression.
* @ Param orefnode
The node from which to evaluate the expression.
* @ Param sxpath the xpath
Expression.
* @ Param oxmlns an object containing the namespaces used in
Expression. Optional.
* @ Return an XML Node
Matching the expression or null if no matches
Found.
*/
Zxpath. selectnodes = function (orefnode/*: node */, sxpath
/*: String */, oxmlns/*: object */){
If (typeof xpathevaluator! =
"Undefined "){

Oxmlns = oxmlns |
{};

VaR nsresolver = function (sprefix)
{
Return
Oxmlns [sprefix];
};

VaR oevaluator = new
Xpathevaluator ();
VaR oresult = oevaluator. Evaluate (sxpath, orefnode,
Nsresolver,

Xpathresult. ordered_node_iterator_type,

Null );

VaR anodes =
New array;

If (oresult! = NULL ){
VaR
Oelement = oresult. iteratenext ();
While (oelement)
{
Anodes. Push (oelement );
Oelement =
Oresult. iteratenext ();
}
}

Return
Anodes;

} Else if (zxml. useactivex ){
If
(Oxmlns ){
VaR sxmlns = "";
For (VAR sprop in
Oxmlns ){
Sxmlns + = "xmlns:" + sprop + "=/'" + oxmlns [sprop]
+ "/'
";
}
Orefnode. ownerdocument. setproperty ("selectionnamespaces ",
Sxmlns );
}
Return
Orefnode. selectnodes (sxpath );
} Else {
Throw new error ("No
XPath engine found .");
}

};

/**
* Selects the first node
Matching a given XPath expression.
* @ Param orefnode the node from which
Evaluate the expression.
* @ Param sxpath the XPath expression.
* @ Param
Oxmlns an object containing the namespaces used in the expression.
* @ Return
An XML node matching the expression or null if no matches
Found.
*/
Zxpath. selectsinglenode = function (orefnode/*: node */, sxpath
/*: String */, oxmlns/*: object */){
If (typeof xpathevaluator! =
"Undefined "){

Oxmlns = oxmlns |
{};

VaR nsresolver = function (sprefix)
{
Return
Oxmlns [sprefix];
};

VaR oevaluator = new
Xpathevaluator ();
VaR oresult = oevaluator. Evaluate (sxpath, orefnode,
Nsresolver,
Xpathresult. first_ordered_node_type,
Null );

If (oresult! = NULL ){
Return
Oresult. singlenodevalue;
} Else {
Return
NULL;
}

} Else if (zxml. useactivex)
{
If (oxmlns ){
VaR sxmlns =
"";
For (VAR sprop in oxmlns ){
Sxmlns + =
"Xmlns:" + sprop + "=/'" + oxmlns [sprop] + "/'
";
}
Orefnode. ownerdocument. setproperty ("selectionnamespaces ",
Sxmlns );
}
Return
Orefnode. selectsinglenode (sxpath );
} Else {
Throw new
Error ("No XPath engine found .")
}

};

/**
* General
Purpose XML serializer.
* @ Class
*/
Function zxmlserializer (){

}

/**
* Serializes
Given XML node into an XML
String.
* @ Param onode the XML Node
To serialize.
* @ Return an XML
String.
*/
Zxmlserializer. Prototype. serializetostring = function (onode
/*: Node */)/*: string */{

VaR sxml = "";

Switch
(Onode. nodetype ){
Case 1: // Element
Sxml = "<" +
Onode. tagname;

For (VAR I = 0; I <
Onode. Attributes. length; I ++ ){
Sxml + = "" +
Onode. attributes [I]. Name + "=/" "+ onode. attributes [I]. Value +
"/"";
}

Sxml + =
"> ";

For (VAR I = 0; I <
Onode. childnodes. length; I ++ ){
Sxml + =
This. serializetostring (onode. childnodes [I]);
}

Sxml
+ = "</" + Onode. tagname +
"> ";
Break;

Case 3: // text
Node
Sxml =
Onode. nodevalue;
Break;
Case 4:
// CDATA
Sxml = "<! [CDATA ["+ onode. nodevalue +
"]> ";
Break;
Case 7: // Processing
Instruction
Sxml = "<? "+ Onode. nodevalue +
"?> ";
Break;
Case 8: // comment
Sxml
= "<! -- "+ Onode. nodevalue +" --> ";
Break;
Case
9: // document
For (VAR I = 0; I <onode. childnodes. length;
I ++ ){
Sxml + =
This. serializetostring (onode. childnodes [I]);
}
Break;

}

Return
Sxml;
};
// -->

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.