I have been working on javascript recently and it is inevitable to parse xml files. So I found many examples on the Internet and found that it is indeed better than what I wrote:
1) My original parsing class: JSDomParse
/**
* Description: JSDomParse parses the XMLDom object.
* JSDomParse
* 2008.11.17
*/
Function JSDomParse (){}
// Generate a dom object using a text-type xml string
JSDomParse. create_XMLDOM = function (vStrXML ){
Try {
If (typeof ActiveXObject! = "Undefined "){
Var uXMLDom = new ActiveXObject ("Microsoft. XMLDOM ");
UXMLDom. loadXML (vStrXML );
Return uXMLDom;
} Else {
Alert ("unable to create XML object ....");
Return null;
}
} Catch (e ){
Alert ("An error occurred while initializing the XML Parser! "+ E );
Return null;
}
}
// Obtain the Node Set Based on vStrNodeName
JSDomParse. get_Nodes = function (vNode, vStrNodeName ){
If (vNode = null) return "";
Var x = vNode. getElementsByTagName (vStrNodeName );
Return x;
}
// Obtain the subnode whose first node type is node
JSDomParse. get_FirstChildNode = function (vNode ){
If (vNode = null) return "";
Var x = vNode. firstChild;
While (x. nodeType! = 1)
{
X = x. nextSibling;
}
Return x;
}
// Obtain the subnode whose last node type is node
JSDomParse. get_LastChildNode = function (vNode ){
If (vNode = null) return "";
Var x = vNode. lastChild;
While (x. nodeType! = 1)
{
X = x. previussibling;
}
Return x;
}
// Obtain all text values in the node
JSDomParse. get_NodeValue = function (vNode ){
If (vNode = null) return "";
Var uNodeList = vNode. childNodes;
Var x = "";
For (var I = 0; I <uNodeList. length; I ++ ){
X + = uNodeList. item (I). nodeValue;
}
Return x;
}
// Obtain the value of the first subnode in the node Column
JSDomParse. get_FirstChildNodeValue = function (vNodeList ){
If (vNodeList = null) return "";
Else {// A NodeList
If (vNodeList. length> 0 ){
Return JSDomParse. get_NodeValue (vNodeList. item (0 ));
} Else {
Return "";
}
}
}
// Obtain the value of the last subnode in the node Column
JSDomParse. get_LastChildNodeValue = function (vNodeList ){
If (vNodeList = null) return "";
Else {// A NodeList
If (vNodeList. length> 0 ){
Return JSDomParse. get_NodeValue (vNodeList. item (vNodeList. length-1 ));
} Else {
Return "";
}
}
}
2) I found an example on the Internet and modified some of them:
// XmlDomParse. js
Var DOM_ELEMENT_NODE = 1;
Var DOM_ATTRIBUTE_NODE = 2;
Var DOM_TEXT_NODE = 3;
Var DOM_CDATA_SECTION_NODE = 4;
Var DOM_ENTITY_REFERENCE_NODE = 5;
Var DOM_ENTITY_NODE = 6;
Var DOM_PROCESSING_INSTRUCTION_NODE = 7;
Var DOM_COMMENT_NODE = 8;
Var DOM_DOCUMENT_NODE = 9;
Var DOM_DOCUMENT_TYPE_NODE = 10;
Var DOM_DOCUMENT_FRAGMENT_NODE = 11;
Var DOM_NOTATION_NODE = 12;
Var ez_xmlDomParse = {
/**
* Http://developer.mozilla.org/en/docs/DOMParser
* Parse an xml file into an xml document object.
* Catch errors using try {} catch (e) {} during the call. The error object has the parser xml message attribute.
* @ Param {String} xml string
* @ Return {Object} xml document object
*/
XmlDocument: function (/* String */xmlString ){
Var doc = null;
If (window. ActiveXObject ){
Var ActiveIds = ['msxml2. XMLDOM ', 'Microsoft. XMLDOM', 'msxml. XMLDOM ', 'msxml3. XMLDOM'];
For (var len = ActiveIds. length, I = 0; I <len; I ++ ){
Var id = ActiveIds [I];
Try {
Var doc = new ActiveXObject (id );
Doc. async = false;
Doc. setProperty ('selectionlinguistic ', 'xpath ');
Doc. loadXML (xmlString );
Break;
} Catch (e) {} finally {
If (doc & doc. parseError & doc. parseError. errorCode! = 0 ){
Throw {parser: 'msxml', message: doc. parseError. reason, xml: xmlString, func: 'xmldocument '};
}
}
}
} Else if (typeof DOMParser! = 'Undefined '){
Var parser = new DOMParser ();
Var doc = parser. parseFromString (xmlString, 'text/xml ');
If (doc.doc umentElement. nodeName = 'parsererror '){
Throw {parser: 'domparser ', message: doc.doc umentElement. firstChild. nodeValue, xml: xmlString, func: 'xmldocument '};
}
} Else {return false ;}
Return doc;
},
/**
* Get the xml content of the specified node or Document Object
* Port from ajaxslt xmlText ()
* @ Param {Object} xml DOM Node or xml Document
* @ Return {String} xml string
*/
XmlText: function (/* Document | Element */node ){
If (typeof node = 'string') {return node ;}
If (node. innerXML ){
Return node. innerXML;
} Else if (node. xml ){
Return node. xml;
} Else if (typeof XMLSerializer! = 'Undefined '){
Return (new XMLSerializer (). serializeToString (node );
} Else {return false ;}
},
/**
* Get the content of all nodes
* Port from ajaxslt xmlValue ()
* @ Param {Object} xml DOM Node or xml Document
* @ Return {String}
*/
XmlValue: function (/* Document | Element */node ){
Var val = '';
If (node. nodeType = DOM_TEXT_NODE |
Node. nodeType = DOM_CDATA_SECTION_NODE |
Node. nodeType = DOM_ATTRIBUTE_NODE ){
Val + = node. nodeValue;
} Else if (node. nodeType = DOM_ELEMENT_NODE |
Node. nodeType = DOM_DOCUMENT_NODE |
Node. nodeType = DOM_DOCUMENT_FRAGMENT_NODE ){
For (var len = node. childNodes. length, I = 0; I <len; I ++ ){
Val + = arguments. callee (node. childNodes [I]);
}
}
Return val;
},
/**
* Get node attributes and return them as objects
* @ Param {Object}
* @ Return {Object}
*/
XmlGetAttributes: function (/* Element */node ){
Var result = {};
Var nodeMap = node. attributes;
If (nodeMap ){
For (var len = nodeMap. length, I = 0; I <len; I ++ ){
Var aNode = nodeMap. item (I );
Result [aNode. nodeName] = aNode. nodeValue;
}
}
Return result;
},
/**
* Set node attributes
* @ Param {Object} xml DOM Node
* @ Param {Object} attribute data as object type
* @ Return {Void}
*/
XmlSetAttributes: function (/* Element */node,/* Object */attributes ){
Var attributes = attributes | | {};
For (key in attributes ){
Node. setAttribute (key, attributes [key]);
}
},
/**
* Use xpath to query in the xml tree
*
Http://developer.mozilla.org/en/docs/Introduction_to_using_XPath_in_JavaScript
* @ Param {Object} xml Document or xml DOM Node
* @ Param {String} xpath query string
* @ Param {Boolean} if set true, only return first result node
* @ Return {Mixed} return array include all nodes or first node
*/
XpathSelectNodes: function (/* Document | Element */root,/* String */query,/* Boolean */returnFirst ){
If (window. ActiveXObject ){
Return returnFirst? Root. selectSingleNode (query): root. selectNodes (query );
} Else if (document. evaluate ){
/**
* DOMParser has two return types: ordered and unordered.
* Ordered will arrange the results in the middle of the tree. unordered is not necessarily
* There is also a snapshots type. This result is a clone of the Tree node.
* That is to say, the original node will not change if the operation result Node
* The non-clone method is used here.
*/
Var returnType = returnFirst? XPathResult. FIRST_ORDERED_NODE_TYPE: XPathResult. ORDERED_NODE_ITERATOR_TYPE;
Var doc = (root. nodeType = DOM_DOCUMENT_NODE )? Root: root. ownerDocument;
Var root = (root. nodeType = DOM_DOCUMENT_NODE )? Root.doc umentElement: root;
Var result = doc. evaluate (query, root, null, returnType, null );
If (returnFirst ){
Var nodes = result. singleNodeValue;
} Else {
Var nextNode = result. iterateNext ();
Var nodes = [];
While (nextNode ){
Nodes. push (nextNode );
NextNode = result. iterateNext ();
}
}
}
Return nodes;
},
/**
* Http://developer.mozilla.org/en/docs/Transforming_XML_with_XSLT
*
Http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations
* @ Param {Mixed} source xml, xml string or xml Document or xml DOM Node
* @ Param {Mixed} xslt style sheet, xml string or xml Document or xml DOM Node
* @ Param {Boolean} if set true, return processed xml as Document
* @ Return {Mixed} return string or Document
*/
Effectprocess: function (/* String | Document | Element */srcDoc,/* String | Document | Element */stylesheet,/* Boolean */returnAsDoc ){
Var returnAsDoc = (typeof returnAsDoc = 'undefined ')? False: returnAsDoc;
Try {
Var srcDoc = (typeof srcDoc = 'string ')? XmlDocument (srcDoc): srcDoc;
Var stylesheet = (typeof stylesheet = 'string ')? XmlDocument (stylesheet): stylesheet;
} Catch (e) {e. func = 'effectprocess'; throw e ;}
If (typeof processing tprocessor! = 'Undefined '){
Var processor = new effectprocessor ();
Try {
Processor. importStylesheet (stylesheet );
Var dest = processor. transformToDocument (srcDoc );
Return returnAsDoc? Dest: xmlText (dest );
} Catch (e) {throw {func: 'effectprocess', processor: 'effectprocessor ', xml: xmlText (srcDoc), xslt: xmlText (stylesheet )};}
} Else if (window. ActiveXObject ){
Try {
Var dest = srcDoc. transformNode (stylesheet );
Return returnAsDoc? XmlDocument (dest): dest;
} Catch (e) {throw {func: 'effectprocess', processor: 'msxml', xml: xmlText (srcDoc), xslt: xmlText (stylesheet )};}
}
Return false;
}
}
Summary:
1) In JSDomParse and 2) ez_xmlDomParse is purely used as a namespace. Due to the fact that the company's previous Code has a namespace and many problems, it is shadow.
It is found that writing classes in json format (that is, the description style in 2) has many advantages. The ez_xmlDomParse at the outermost layer is equivalent to the package concept in java and has a sense of attention, use this method to define classes in the future.