jeffy

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            70 Posts :: 1 Stories :: 14 Comments :: 0 Trackbacks
            當(dāng)用js的alert 方法顯示ajax以responseText顯示返回結(jié)果時(shí)候, 顯示的是個(gè)xml結(jié)構(gòu)文檔, 但以responseXML解析xml的時(shí)候, 所有節(jié)點(diǎn)長度都為0,  這個(gè)問題關(guān)鍵是服務(wù)器端沒有指定正確的文檔格式:
                  response.setContentType("text/xml;charset=UTF-8"); (正確)
                   response.setContentType("text/html;charset=UTF-8");(錯(cuò)誤)
          posted on 2007-08-17 17:35 Live-in Java 閱讀(2986) 評論(2)  編輯  收藏

          評論

          # re: Ajax以responseXML返回,客戶端(IE)不能分析xml問題 2009-02-10 06:07 liyaxi
          原文:http://jiangx.javaeye.com/blog/153066
          1.對XMLHttpRequest請求返回的responseXML進(jìn)行解析,responseXML是個(gè)XMLDOcument對象
          假設(shè)返回的responseXML為:
          <?xml version="1.0" encoding="UTF-8"
          standal?>
          <response>
          <method>checkName</method>
          <result>1</result>
          </response>
          則獲取method和result值方法為:
          var response = req.responseXML.documentElement;
          method = response.getElementsByTagName('method')[0].firstChild.data;
          result = response.getElementsByTagName('result')[0].firstChild.data;

          2.創(chuàng)建一個(gè)XMLDocument對象
          function getXMLDocument() {
          var xDoc = null;
          if (document.implementation && document.implementation.createDocument) {
          xDoc = document.implementation.createDocument("", "", null);
          } else {
          if ((typeof ActiveXObject) != "undefined") {
          var msXmlAx = null;
          try {
          msXmlAx = new ActiveXObject("Msxml2.DOMDocument");
          }
          catch (e) {
          msXmlAx = new ActiveXObject("Msxml.DOMDocument");
          }
          xDoc = msXmlAx;
          }
          }
          if (xDoc == null || typeof xDoc.load == "undefined") {
          xDoc = null;
          }
          return xDoc;
          }

          3.創(chuàng)建一個(gè)DOM樹
          <people>
          <person first-name="eric" middle-initial="h" last-name="jung">
          <address street="321 south st" city="denver" state="co" country="usa" />
          </person>
          <person first-name="jed" last-name="brown">
          <address street="321 north st" city="atlanta" state="ga" country="usa" />
          <address street="321 south avenue" city="denver" state="co" country="usa" />
          </person>
          </people>
          程序如下:
          var doc=getXMLDocument();
          var peopleElem = doc.createElement("people");
          var personElem1 = doc.createElement("person");
          personElem1.setAttribute("first-name", "eric");
          personElem1.setAttribute("middle-initial", "h");
          personElem1.setAttribute("last-name", "jung");

          var addressElem1 = doc.createElement("address");
          addressElem1.setAttribute("street", "321 south st");
          addressElem1.setAttribute("city", "denver");
          addressElem1.setAttribute("state", "co");
          addressElem1.setAttribute("country", "usa");
          personElem1.appendChild(addressElem1);

          var personElem2 = doc.createElement("person");
          personElem2.setAttribute("first-name", "jed");
          personElem2.setAttribute("last-name", "brown");

          var addressElem3 = doc.createElement("address");
          addressElem3.setAttribute("street", "321 north st");
          addressElem3.setAttribute("city", "atlanta");
          addressElem3.setAttribute("state", "ga");
          addressElem3.setAttribute("country", "usa");
          personElem2.appendChild(addressElem3);

          var addressElem5 = doc.createElement("address");
          addressElem5.setAttribute("street", "321 south avenue");
          addressElem5.setAttribute("city", "denver");
          addressElem5.setAttribute("state", "co");
          addressElem5.setAttribute("country", "usa");
          personElem2.appendChild(addressElem5);

          peopleElem.appendChild(personElem1);
          peopleElem.appendChild(personElem2);
          doc.appendChild(peopleElem);
          alert(doc.xml);//xml屬性只對IE管用
            回復(fù)  更多評論
            

          # re: Ajax以responseXML返回,客戶端(IE)不能分析xml問題 2009-02-10 06:20 liyaxi
          得到的是一個(gè)數(shù)組
          var str=new Array();
          var xmlDoc=xmlHttp.responseXML;
          for(var i=0;i<xmlDoc.length;i++)
          {
          str[i]=xmlDoc.getElementsByTagName("name")[i].childNodes [0].nodeValue;

          }
          注意要判斷ie text() 和火狐的textContent()區(qū)別  回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 凤城市| 杨浦区| 勃利县| 蒙城县| 通榆县| 丹凤县| 崇义县| 沾益县| 晋中市| 江西省| 泸水县| 井陉县| 白城市| 杨浦区| 兰西县| 伊春市| 翼城县| 张家口市| 大庆市| 石景山区| 龙陵县| 青川县| 诸暨市| 香港| 武功县| 龙州县| 嘉善县| 亚东县| 乌兰浩特市| 大连市| 天等县| 甘德县| 锡林浩特市| 黔西县| 红桥区| 高阳县| 汝州市| 华宁县| 腾冲县| 清新县| 习水县|