2010年9月10日 星期五

讀取xml

<!---使用java元件讀取xml --->
 <cfscript>
  xmlReaderPool = CreateObject("JAVA", "XMLReaderPool");
  poolXMLReader = XMLReaderPool.getInstance();
  xmlList = poolXMLReader.getXMLReader();
  xmlList.doParse(returnvalue);
  xmlRecordCount= xmlList.getAttributes("ROOT.RECORDCOUNT");
  pageRowCount = xmlList.getItemNums("ROOT.ITEM");
 </cfscript>


<!-----使用cfm內建元件讀取xml----->
<cfset isXml = false>
<cftry>
    <cfset returnXml = xmlParse(returnValue)> 
    <cfif isxmldoc(returnXml)>  
        <cfset xmlList = returnXml.xmlRoot >    
        <cfset xmlRecordCount = xmlList.xmlAttributes["RECORDCOUNT"]>
        <cfset pageRowCount = arraylen(xmlList.xmlChildren)>
        <cfset isXml = true>     
    </cfif>
    <cfcatch type="any">
        <cfset rowCount = 0>
    </cfcatch> 
</cftry>

<!--- 另一種cfm讀取方式--->
<cfif xml neq "">
    <cfset xml_hot_name = xmlparse(xml) >
    <cfset xml_root=#xml_hot_name.xmlRoot#>
    <cfset cData.hot_name_count=#xml_root.XmlAttributes.recordcount#>
    <cfif cData.hot_name_count gt 0>
        <cfset cData.dataCount = cData.dataCount + cData.hot_name_count >
        <cfset att=fun_rand(#cData.hot_name_count#)>
        <cfloop index="g" from="1" to="#cData.hot_name_count#">
            <cfset xml_all = structnew() >
            <cfset xml_all=xml_root.XmlChildren[#ListGetAt(att,g)#].XmlAttributes>
            <cfset cData.hot_name[g] = structnew() >
            <cfset cData.hot_name[g].h_name = xml_all.h_name>
            <cfset cData.hot_name[g].h_tel = xml_all.h_tel>
            <cfset cData.hot_name[g].h_add = xml_all.h_add>
        </cfloop>
    </cfif>
</cfif>

 <!--- 另一種cfm讀取方式--->
<cfset resumeXml = xmlParse(xmlContent)>
<cfloop from="1" to="#arraylen(resumeXml.xmlroot.xmlchildren)#" index="i">
    <cfset tmp_name = resumeXml.xmlroot.xmlchildren[i].xmlname>
    <cfset tmp_text = resumeXml.xmlroot.xmlchildren[i].xmltext>
    <cfset tmp_type = resumeXml.xmlroot.xmlchildren[i].xmlattributes["type"]>
    <cfif tmp_type eq "array">
        <cfset tmp_array = arraynew(1)>
        <cfloop from="1" to="#arraylen(resumeXml.xmlroot.xmlchildren[i].xmlchildren)#" index="j">
            <cfset tmp_array[j] = resumeXml.xmlroot.xmlchildren[i].xmlchildren[j].xmltext>  
        </cfloop>
        <cfset "#tmp_name#" = tmp_array>
    <cfelse>
        <cfset "#tmp_name#" = trim(tmp_text)>
    </cfif>
</cfloop>
 

沒有留言:

張貼留言