ColdFusion 操作 XML
程序员文章站
2022-05-06 22:29:46
...
<!---新建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>