我在 ColdFusion 中编写了一个脚本,它在数据库表中获取我网站的所有 HTML 内容并创建一个 XML 文档对象。到目前为止,下面是我的代码。
<!--- Get page info --->
<cfquery name="qPageInfo" datasource="#application.datasource#">
SELECT top 800 id, title, info, app
FROM sitePublic
</cfquery>
<cfdump var="#qPageInfo#">
<!--- Creating XML document object w/ sitePublic data --->
<cfxml variable="xmlPageInfo">
<page>
<cfoutput query="qPageInfo">
<id>#qPageInfo.id#</id>
<pagecontent>#Trim("#XMLFormat(qPageInfo.info)#")#</pagecontent>
</cfoutput>
</page>
</cfxml>
<!---<cfdump var="#xmlPageInfo#">--->
<!--- Need to figure out how to output to a file --->
<cffile action="write" file="%filepath%/webcontent.xml" output="#qPageInfo#">
我能够成功地对 XML 文档对象执行 cfdump,并将所有网页的所有内容转储到浏览器窗口中。
然而,我似乎无法开始工作的部分是最后一行,<cffile>
将 XML 数据输出到 XML 文件。以下是我在尝试运行时遇到的错误。
The expression has requested a variable or an intermediate expression result as a simple value. However, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values.
The most likely cause of the error is that you tried to use a complex value as a simple one. For example, you tried to use a query variable in a cfif tag.
我是 ColdFusion 的新手,所以我的代码中可能遗漏了一些东西,有人知道如何解决这个问题吗?
最佳答案
我对此有大大小小的担忧
首先
<!--- Creating XML document object w/ sitePublic data --->
<cfxml variable="xmlPageInfo">
<page>
<cfoutput query="qPageInfo">
<id>#qPageInfo.id#</id>
<pagecontent>#Trim("#XMLFormat(qPageInfo.info)#")#</pagecontent>
</cfoutput>
</page>
</cfxml>
这将生成很多很多 <id>
和 <pagecontent>
标签
考虑
<pagecontent id="#qPageInfo.id#">#Trim(XMLFormat(qPageInfo.info))#</pagecontent>
第二
<cffile action="write" file="%filepath%/webcontent.xml" output="#qPageInfo#">
现在是真正的问题,qPageInfo
是查询。我怀疑您想编写 xml。
更简单的方法是将 XML 作为字符串处理
<cfsavecontent variable="xmlPageInfo">
<page>
<cfoutput query="qPageInfo">
<pagecontent id="#qPageInfo.id#">#Trim(XMLFormat(qPageInfo.info))#</pagecontent>
</cfoutput>
</page>
</cfsavecontent>
<cffile action="write" file="%filepath%/webcontent.xml" output="#xmlPageInfo#">
或
如果你想确保你正在处理 xml 而不仅仅是 xml 的字符串
<cfxml variable="xmlPageInfo">
<page>
<cfoutput query="qPageInfo">
<pagecontent id="#qPageInfo.id#">#Trim(XMLFormat(qPageInfo.info))#</pagecontent>
</cfoutput>
</page>
</cfxml>
<cffile action="write" file="%filepath%/webcontent.xml" output="#ToString(xmlPageInfo)#">
关于html - 在 ColdFusion 中,如何将填充有包含 HTML 内容的 SQL 查询数据的 XML 文档对象输出到 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38254925/