小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

JS解析XML - snowfox3761的專欄

 Blex 2011-06-08
朋友很急,正好我沒(méi)什么事就一起幫忙,順便學(xué)一學(xué)。
前一段還以為js上解析xml是ajax的功勞,其實(shí)不然。
朋友的問(wèn)題如下:
Dear Applicant,
Thank you very much for your interest in this position. Would you please send me some _fragment_ of code and designment document that you've worked on in your past projects?  That will helps us better evaluate your applicaiton.
We hope you can demonstrate your experience/learning ability by finishing the assignment below. It'll be a jscript function to transform a given format of XML input to another XML format.
The input would be like:

<?xml version="1.0"?>
<menu xmlns="">
    <menuitem>
        <node>1</node>
        <parent>1</parent>
        <name>parent</name>
    </menuitem>
    <menuitem>
        <node>2</node>
        <parent>1</parent>
        <name>1st child</name>
    </menuitem>
    <menuitem>
        <node>3</node>
        <parent>1</parent>
        <name>2nd child</name>
    </menuitem>
    <menuitem>
        <node>4</node>
        <parent>2</parent>
        <name>grantchild</name>
    </menuitem>
</menu>

The output would be like:
<?xml version="1.0"?>
<menu xmlns="">
    <menuitem>
        <name>parent</name>
        <menuitem>
            <name>1st child</name>
            <menuitem>
                <name>grand child</name>
            </menuitem>
        </menuitem>
        <menuitem>
            <name>2nd child</name>
        </menuitem>
    </menuitem>
</menu>
In the input format, tag "node" and "parent" specifies the structure of the tree, while "name" and others tags should be copied to the output format.  Therefore, the code should be able to handle the following fragment of XML:
<menuitem>
        <node>2</node>
        <parent>1</parent>
        <name>1st child</name>
        <lastName>Dai</lastName>
        <firstName>Kun</firstName>
</menuitem>
Thank you again for your time, and please reply this email after you get it.
我只幫忙解析,后面嗎,只提供思路:解析的元素封裝成js的object,之后處理邏輯就和java一樣了。
解析代碼:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function loadXml(){
  var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
     //try //Internet Explorer
       //{
      // xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      // }
      // catch(e)
      // {
      // try //Firefox, Mozilla, Opera, etc.
     // {
     // xmlDoc=document.implementation.createDocument("","",null);
     // }
      //   catch(e) {alert(e.message)}
        //   }
   xmlDoc.async="false";//取消異步加載
   xmlDoc.load("a.xml");
   //xmlDoc.loadXmlString(字符串);如果xml的內(nèi)容是一個(gè)字符串比如<x>1</x>可以用此方法
   //var items = xmlDoc.selectNodes("/menu/menuitem");
   //alert(items.length);
  // for(var i=0;i<items.length;i++){
  // alert(xmlDoc.selectSingleNode("/menu/menuitem["+i+"]/node").text);
  // alert(xmlDoc.selectSingleNode("/menu/menuitem["+i+"]/parent").text);
  // alert(xmlDoc.selectSingleNode("/menu/menuitem["+i+"]/name").text);
   //}
  //方法2
  var items = xmlDoc.getElementsByTagName("menuitem");
   alert(items.length);
   for(var i=0;i<items.length;i++){
   var x = items.firstChild;
   alert(x.getAttribute("title"));//取得標(biāo)簽上的屬性,如<x title="a"></x>取得title的值'a'
   alert(x.text);//或者xmldoc.getElementsByTagName("node").text
   alert(x.nextSibling.text);//nextSibling方法是取得下一個(gè)node
   alert(x.nextSibling.nextSibling.text);
   }
  }
  function getNode(doc, xpath) {
  varretval = "";
  var value = doc.selectSingleNode(xpath);
  if (value) retval = value.text;
  return retval;
    }
//-->
</SCRIPT>
</HEAD>

<BODY>
<input type="button" onclick="loadXml()" value = "ok">
</BODY>
</HTML>
用了兩種方法,第一種是通過(guò)網(wǎng)上google的例子依賴于selectSingleNode和selectNodes方法,
個(gè)人覺(jué)得通用性很難實(shí)現(xiàn),如果實(shí)現(xiàn)的話,需要的是封裝路徑,而且事先要知道整個(gè)xml文件的完整結(jié)構(gòu)。
第二種方法,是我從w3school上學(xué)到的,在被xmlDoc加載之后,xml部分應(yīng)該就具備了頁(yè)面元素的特性,所以通過(guò)getElementsXX等方法就可以去做,在w3school上學(xué)到的竅門(mén),拿到firstChild之后后面的元素就用nextsibling,當(dāng)然需要加判斷的,測(cè)試的關(guān)系沒(méi)有加。
這次發(fā)現(xiàn),對(duì)與html沒(méi)有足夠充分的了解而去研究js框架是比較困難的,所以w3school繼續(xù)學(xué)習(xí)中。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多