明月松間照 清泉石上流


                                                  ——— 兵臨城下   貓科動物
          posts - 70, comments - 137, trackbacks - 0, articles - 23
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          JS parse xml

          Posted on 2006-11-30 17:48 兵臨城下 閱讀(1066) 評論(0)  編輯  收藏 所屬分類: JavaScript

          //XML 解析

          XML.newDocument = function(rootTagName, namespaceURL) {
          ??? if (!rootTagName) rootTagName = "";
          ??? if (!namespaceURL) namespaceURL = "";

          ??? if (document.implementation && document.implementation.createDocument) {
          ??????? // This is the W3C standard way to do it
          ??????? return document.implementation.createDocument(namespaceURL,
          ????????????????????????????????????????????????????? rootTagName, null);
          ??? }
          ??? else { // This is the IE way to do it
          ??????? // Create an 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
          ??????? 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 last resort, try loading the document from a data: URL
          ??????? // This is supposed to work in Safari. Thanks to Manos Batsis and
          ??????? // his Sarissa library (sarissa.sourceforge.net) 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;
          ??? }
          }

          主站蜘蛛池模板: 博湖县| 陆良县| 南康市| 郸城县| 壶关县| 汤原县| 渝北区| 永川市| 泸州市| 星子县| 东至县| 广灵县| 乌鲁木齐县| 梅河口市| 东光县| 同心县| 广东省| 巢湖市| 新和县| 方城县| 萨迦县| 舒兰市| 南城县| 习水县| 甘肃省| 兴隆县| 宁武县| 泰来县| 定南县| 金溪县| 诸暨市| 辽阳县| 东山县| 海林市| 延庆县| 兴海县| 小金县| 梅河口市| 大城县| 中江县| 任丘市|