ColdFusion之XML構建二

 

我在這裏用ColdFusion對XML進行操作,下面是從數據庫查出來的生成Xml文件的代碼如下:

<cfsetting enablecofoutputonly="yes">

    <cfquery name="pubs" datasource="pubs">

      select title_id,title,price from titles

    </cfquery>

    <cfscript>

        mydoc=xmlnew();

        mydoc.xmlroot=xmlelemnew(mydoc,"body");

       for(i=1;i le #pubs.recordcount#; i=i+1)

      {

           mydoc.xmlroot.xmlchildren[i]=xmlelemnew(mydoc,"title");

           mydoc.xmlroot.xmlchildren[i].xmltext="#xmlformat(pubs.title[i])#";

           structinsert(mydoc.xmlroot.xmlchildren[i].xmlattributes,"id","#pubs.title_id[i]#");

          mydoc.xmlroot.xmlchildren[i].xmlchildren[1]=xmlelemnew(mydoc,"price");

          mydoc.xmlroot.xmlchildren[i].xmlchildren[1].xmltext="#pubs.price[i]#";

      }

    </cfscript>

    <cffile action="write" addnewline="no" charset="utf-8" file="#getdirecoryfrompaht(gettemplatepath())#myxml.xml" output="#tostring(mydoc)#">

<cfsetting enablecofoutputonly="no">

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章