ColdFusion 操作 XML

<!---新建XML--->
<cfset rootnode=XmlNew()>

<!---新建根节点--->
<cfset rootnode.xmlRoot = XmlElemNew(rootnode,"XML")>

<!---给新建的根节点加个属性--->
<cfset rootnode.xmlRoot.XmlAttributes.type = "default">
<cfset rootnode.xmlRoot.XmlAttributes["type"] = "default">

<!---再加一个新的节点--->
<cfset sRow = XmlElemNew(rootnode,"ROW")>

<!---给这个新节点加点内容--->
<cfset sRow.XmlText = "test">

<!---把这个新节点加到根节点上--->
<cfset rootnode.xmlRoot.XmlChildren[1] = sRow>

<!---我再给这个节点也加个属性怎么样--->
<cfset rootnode.xmlRoot.XmlChildren[1].XmlAttributes.col0=sCurrValue>

<!---那么我print出来呢--->
<cfset xmlString = ToString(rootnode)>
<cfoutput>#xmlString#</cfoutput>

<!---还有呢--->
oNode.xmlChildren[1] = xmlElemNew(oXml, "description");
oNode.description.xmlValue = "<![CDATA[hello world!]]>"; 

<!---这些呢--->
oNode.xmlChildren[1] = xmlElemNew(oXml, "description");
oNode.description.xmlValue = "<![CDATA[hello world!]]>"; 

<legend placement="Left">
              <![CDATA[
        $(colLabel)
        $(colPercent)
              ]]>
</legend>
<dataLabels style="Pattern" 
              placement="inside">
              <![CDATA[
        $(value)
        $(rowLabel)
              ]]>
</dataLabels>

<!---读取XML--->
<cfset xmlGeorge=XMLParse(ExpandPath("Users.xml"))>
<cfoutput>
 <h2>#xmlGeorge.users.u1.name# #xmlGeorge.users.u2.name#</h2>
</cfoutput>
<cfdump var="#xmlGeorge#">

<!---创建XML--->
<cfcontent type="text/xml" reset="yes">
<cfset user=XmlNew()>
<cfset user.xmlRoot = XmlElemNew(user,"Name")>
<cfset user.xmlRoot.XmlAttributes.Title = "testuser">
<cfset elemFirstName = XmlElemNew(user,"FirstName")>
<cfset elemLastName = XmlElemNew(user,"LastName")>
<cfset elemFirstName.XmlText = "sun">
<cfset elemLastName.XmlText = "pc">
<cfset user.xmlRoot.XmlChildren[1] = elemFirstName>
<cfset user.Name.XmlChildren[2] = elemLastName>
<cfset xmlString = ToString(user)>
<cfoutput>#xmlString#</cfoutput>

<!---数据库数据生成XML--->
<cfsetting showdebugoutput="no">
<!---①先从库里边得到数据--->
<cfquery name="rsUsers" datasource="mydb">
	select * from users
</cfquery>

<!---②新建一个XML--->
<cfset users=XmlNew()>

<!---③为新建的xml添加根元素users--->
<cfset users.xmlRoot = XmlElemNew(users,"users")>
<!---为根元素添加title属性--->
<cfset users.xmlRoot.XmlAttributes.title = "testuser">

<!---④循环创建节点,然后添加节点到根节点下--->
<cfoutput query="rsUsers">
	<!---㈠创建节点user、username、password--->
	<cfset uNode = XmlElemNew(users,"user")>
	<cfset sName = XmlElemNew(users,"username")>
	<cfset sPwd = XmlElemNew(users,"password")>
	<!---㈡给节点赋值--->
	<cfset sName.XmlText = rsUsers.username>
	<cfset sPwd.XmlText = rsUsers.password>
	<!---㈢将节点追加到别的节点上(如user到根节点,username到user节点)--->
	<cfset users.xmlRoot.XmlChildren[rsUsers.currentRow] = uNode>
	<cfset users.xmlRoot.XmlChildren[rsUsers.currentRow].XmlChildren[1] = sName>
	<cfset users.xmlRoot.XmlChildren[rsUsers.currentRow].XmlChildren[2] = sPwd>
</cfoutput>

<cfset xmlString = ToString(users)>
<cfoutput>#xmlString#</cfoutput>

<!---下载XML--->
<cfoutput>
	<meta http-equiv="Content-Type" content="charset=gb2312"> 
	<cfset sFile="users">
	<cfcontent type="text/xml;charset=utf-8" reset="yes">
	<cfheader name="Content-Disposition" value="attachment;filename=#sFile#.xml">
	<cfheader name="Content-Description" value="#sFile#">
</cfoutput>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值