隨筆 - 14, 文章 - 39, 評論 - 17, 引用 - 0
          數據加載中……

          資料:Javascript 操作XML[轉]

          ?

          一般從服務端的返回可以得到一個XML對象。
          例如服務器返回的:XMLHttpRequest.ResponseXML
          這里的XMLHttpRequest就是ajax的核心對象。
          在IE下可以這樣創建:xmlHttp?
          = ? new ?ActiveXObject( " Microsoft.XMLHTTP " ).
          javascript操作XML先創建一個XML?DOM對象:var?dom?
          = ? new ?ActiveXObject( " Microsoft.XMLDOM " );
          然后dom.loadXML(ResponseXML)就ok了。
          接下來就可以操作xml,獲取內容了。
          一些常用的函數如下(一些在網上收集的,一些時平時老大教的):
          Microsoft.XMLDOM?對象常用的屬性:
          1 、attributes?屬性,返回當前節點的屬性列表
          2 、childNodes?屬性,返回當前節點的所有子節點列表
          3 、documentElement?屬性,返回xml文件的根節點,通過Microsoft.XMLDOM對象名來調用
          4 、firstChild?屬性、lastChild?屬性,返回當前節點的第一個子(最后一個)元素(如果沒有子節點是不是返回
          第一個屬性?)
          5 、nextSibling?(previousSibling?)屬性,下一個兄弟節點。
          6 、nodeName?屬性,返回節點的標簽名字
          7 、nodeValue?屬性,傳回指定節點相關的文字(不是屬性,就是 * 號的這個內容? **
          8 、ownerDocument?屬性,根節點
          9 、parentNode?屬性,傳回目前節點的父節點。只能應用在有父節點的節點中。
          搞一個例子:
          function?Add()
          {
          var?dom?
          = ? new ?ActiveXObject( " Microsoft.XMLDOM " );?
          dom.loadXML(ret);?
          if ?(dom.documentElement? != ? null )
          {?
          var?nodes?
          = ?dom.documentElement.selectNodes( " //SelectItem " );? // 得到根節點下所有SelectItem節點
          if ?(nodes? != ? null )?
          {
          for (var?i = 0 ;i?
          一些常用的函數:
          1 、AppendChild?方法,加上一個節點當作指定節點最后的子節點。
          2 、cloneNode(deep)方法,deep?是一個布爾值。如果為true,此節點會復制以指定節點發展出去的所有節
          點。如果是false,只有指定的節點和它的屬性被復制。
          3 、createAttribute(name)方法,建立一個指定名稱的屬性。
          4 、createElement?方法,建立一個指定名稱的元素。
          5 、xmlDocument.createNode(type,?name,?nameSpaceURI);type?用來確認要被建立的節點型態,name?是一個字符
          串來確認新節點的名稱,命名空間的前綴則是選擇性的。nameSpaceURI?是一個定義命名空間URI?的字
          符串。如果前綴被包含在名稱參數中,此節點會在nameSpaceURI?的內文中以指定的前綴建立。如果不
          包含前綴,指定的命名空間會被視為預設的命名空間。
          6 、getElementsByTagName?方法,傳回指定名稱的元素集合。
          7 、haschildnodes?方法,要解釋嗎?
          8 、insertBefore?方法,在指定的節點前插入一個子節點。xmlDocumentNode.insertBefore
          (newChild,refChild);refChild?是參照節點的地址。新子節點被插到參照節點之前。如果refChild?參數沒有包含
          在內,新的子節點會被插到子節點列表的末端。
          9 、load?方法和loadXML?方法,前這從url,后者從字符串片斷。
          10 、nodeFromID?方法,傳回節點ID?符合指定值的節點。
          11 、removeChild?方法和replaceChild(newChild,oldChild),顧名思義
          12 、selectNodes和selectSingleNode?方法,傳回所有符合提供樣式的節點。參數為一包含XSL?樣式的字符串。
          以下收集了一些MSDN的例子
          (
          1 )
          var?xmlDoc?
          = ? new ?ActiveXObject( " Msxml2.DOMDocument.3.0 " );
          var?rootElement
          = xmlDoc.createElement( " memo " );
          xmlDoc.appendChild(rootElement);(
          2 )?var?xmlDoc? = ? new ?ActiveXObject( " Msxml2.DOMDocument.3.0 " );
          var?rootElement
          = xmlDoc.createElement( " memo " );
          rootElement.setAttribute(
          " author " ,? " Pat?Coleman " );? // 屬性author的值為Pat?Coleman
          xmlDoc.appendChild(rootElement);
          (
          3 )?var?xmlDoc? = ? new ?ActiveXObject( " Msxml2.DOMDocument.3.0 " );
          var?rootElement
          = xmlDoc.createElement( " memo " );
          var?memoAttribute
          = xmlDoc.createAttribute( " author " );
          var?memoAttributeText
          = xmlDoc.createTextNode( " Pat?Coleman " );
          memoAttribute.appendChild(memoAttributeText);
          rootElement.setAttributeNode(memoAttribute);
          xmlDoc.appendChild(rootElement);
          // 這個例子和(2)同樣效果,但是用不同的方法,這里把attribute也當做一個節點,attribute?node的
          子節點只可以是textnode,所以這里要先創建一個textnode在賦給他。
          4
          var?xmlDoc?
          = ? new ?ActiveXObject( " Msxml2.DOMDocument.3.0 " );
          var?rootElement
          = xmlDoc.createElement( " memo " );? // 創建一個元素
          var?memoAttribute = xmlDoc.createAttribute( " author " );? // 創建一個屬性
          var?memoAttributeText = xmlDoc.createTextNode( " Pat?Coleman " );? // 創建一個文本節點
          var?toElement = xmlDoc.createElement( " to " );? // 再創建一個元素
          var?toElementText = xmlDoc.createTextNode( " Carole?Poland " );? // 再創建一個文本節點
          memoAttribute.appendChild(memoAttributeText);
          xmlDoc.appendChild(rootElement);
          rootElement.setAttributeNode(memoAttribute);
          rootElement.appendChild(toElement);
          toElement.appendChild(toElementText);


          屬性:
          attributes
          Contains?the?list?of?attributes?
          for ? this ?node.?Read - only.
          baseName
          * Returns?the?base?name? for ?the?name?qualified?with?the?namespace.?Read - only.
          childNodes
          Contains?a?node?list?containing?the?children?nodes.?Read
          - only.
          dataType
          * Specifies?the?data?type? for ? this ?node.?Read / write.
          definition
          * Returns?the?definition?of?the?node?in?the?document?type?definition?(DTD)?or?schema.?Read - only.
          firstChild
          Contains?the?first?child?of?the?node.?Read
          - only.
          lastChild
          Returns?the?last?child?node.?Read
          - only.
          name
          Contains?the?attribute?name.?Read
          - only.
          namespaceURI
          * Returns?the?Uniform?Resource?Identifier?(URI)? for ?the?namespace.?Read - only.
          nextSibling
          Contains?the?next?sibling?of?
          this ?node?in?the?parent ' s?child?list.?Read-only.
          nodeName
          Contains?the?qualified?name?of?the?element,?attribute,?or?entity?reference,?or?a?fixed?string?
          for ?other?node?types.?Read - only.
          nodeType
          Specifies?the?XML?Document?Object?Model?(DOM)?node?type,?which?determines?valid?values?and?whether?the?node?can?have?child?nodes.?Read
          - only.
          nodeTypedValue
          * Contains?the?node?value?expressed?in?its?defined?data?type.?Read / write.
          nodeTypeString
          * Returns?the?node?type?in?string?form.?Read - only.
          nodeValue
          Contains?the?text?associated?with?the?node.?Read
          / write.
          ownerDocument
          Returns?the?root?of?the?document?that?contains?the?node.?Read
          - only.
          parentNode
          Contains?the?parent?node.?Read
          - only.
          parsed
          * Indicates?the?parsed?status?of?the?node?and?child?nodes.?Read - only.
          prefix
          * Returns?the?namespace?prefix.?Read - only.
          previousSibling
          Contains?the?previous?sibling?of?
          this ?node?in?the?parent ' s?child?list.?Read-only.
          specified
          Indicates?whether?the?node?(usually?an?attribute)?is?explicitly?specified?or?derived?from?a?
          default ?value?in?the?document?type?definition?(DTD)?or?schema.?Read - only.
          text
          Represents?the?text?content?of?the?node?or?the?concatenated?text?representing?the?node?and?its?descendants.?Read
          / write.
          value
          Contains?the?attribute?value.?Read
          / write.
          xml
          Contains?the?XML?representation?of?the?node?and?all?its?descendants.?Read
          - only.
          方法:
          appendChild
          Appends?
          new ?child?node?as?the?last?child?of? this ?node.
          cloneNode
          Clones?a?
          new ?node.
          hasChildNodes
          Provides?a?fast?way?to?determine?whether?a?node?has?children.
          insertBefore
          Inserts?a?child?node?to?the?left?of?the?specified?node?or?at?the?end?of?the?list.
          removeChild
          Removes?the?specified?child?node?from?the?list?of?children?and?returns?it.
          replaceChild
          Replaces?the?specified?old?child?node?with?the?supplied?
          new ?child?node.
          selectNodes
          Applies?the?specified?pattern
          - matching?operation?to? this ?node ' s?context?and?returns?the?list?of?matching?nodes?as?IXMLDOMNodeList.
          selectSingleNode
          Applies?the?specified?pattern
          - matching?operation?to? this ?node ' s?context?and?returns?the?first?matching?node.
          transformNode
          Processes?
          this ?node?and?its?children?using?the?supplied?XSL?Transformations?(XSLT)?style?sheet?and?returns?the?resulting?transformation.
          transformNodeToObject
          Processes?
          this ?node?and?its?children?using?the?supplied?XSL?Transformations?(XSLT)?style?sheet?and?returns?the?resulting?transformation?in?the?supplied?object.?


          posted on 2006-11-22 18:19 mlw2000 閱讀(383) 評論(0)  編輯  收藏 所屬分類: JavaScript

          主站蜘蛛池模板: 芒康县| 华池县| 莱州市| 文登市| 襄汾县| 郯城县| 阳朔县| 瓦房店市| 定襄县| 宜兰市| 井陉县| 丰原市| 苗栗市| 石门县| 建阳市| 讷河市| 板桥市| 鹿泉市| 建始县| 林州市| 梨树县| 双桥区| 贡山| 开封市| 满城县| 封丘县| 河北区| 广水市| 甘孜| 武强县| 通化市| 佳木斯市| 武夷山市| 保靖县| 青神县| 洪江市| 临朐县| 南通市| 阿城市| 汽车| 凉山|