如何将使用包含HTML内容的SQL查询数据填充的XML文档对象输出到ColdFusion中的XML文件?

时间:2022-10-16 13:55:59

I have written a script in ColdFusion that grabs all of the HTML content for my website in a database table and creates an XML document object. Below is my code so far.

我用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#">

I was able to successfully do a cfdump on the XML document object and it dumped all content for all web pages in the browser window.

我成功地在XML文档对象上做了一个cfdump,并在浏览器窗口中转储所有web页面的内容。

However, the part I cannot seem to get working is the last line, <cffile> to output the XML data to an XML file. Below is the error I get when trying to run execute this.

但是,最后一行 将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.

表达式请求一个变量或中间表达式作为一个简单的值。但是,结果不能转换为简单的值。简单的值是字符串、数字、布尔值和日期/时间值。查询、数组和COM对象是复杂值的示例。

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.

最可能导致错误的原因是您试图使用一个复杂的值作为一个简单的值。例如,您尝试在一个委员会标记中使用查询变量。

I am new to ColdFusion so I could be leaving something out in my code, does anyone have an idea on how to fix this?

我是ColdFusion新手,所以我可能会在代码中遗漏一些东西,有人知道如何修复这个问题吗?

2 个解决方案

#1


3  

I have a large and small concern about this

我对此有很大的顾虑

First

第一个

<!--- 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>

This is going to generate lots and lots of <id> and <pagecontent> tags

这将生成大量的 标记

Consider

考虑

<pagecontent id="#qPageInfo.id#">#Trim(XMLFormat(qPageInfo.info))#</pagecontent>

Second

第二个

<cffile action="write" file="%filepath%/webcontent.xml" output="#qPageInfo#">

Now the real problem, qPageInfo is the query. I suspect that you wanted to write the xml.

现在真正的问题是,qPageInfo是查询。我怀疑您想要编写xml。

The simpler approach is to deal with the XML as a string

更简单的方法是将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#">

OR

If you want to make sure you are processing xml and not just a string that is xml

如果要确保处理的是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)#">

#2


2  

Not sure if you posted the actual code with your question or not but you have % percent signs instead of # hash tags around your filepath variable.

不确定您是否发布了带有问题的实际代码,但是您在filepath变量周围有%的符号而不是#散列标记。

This:

这样的:

<cffile action="write" file="%filepath%/webcontent.xml" output="#qPageInfo#">

Should be this:

应该是这样的:

<cffile action="write" file="#filepath#/webcontent.xml" output="#xmlPageInfo#">

updated to use the correct variable xmlPageInfo for writing the file as others have mentioned.

更新后使用正确的变量xmlPageInfo来编写文件,就像其他人提到的那样。

#1


3  

I have a large and small concern about this

我对此有很大的顾虑

First

第一个

<!--- 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>

This is going to generate lots and lots of <id> and <pagecontent> tags

这将生成大量的 标记

Consider

考虑

<pagecontent id="#qPageInfo.id#">#Trim(XMLFormat(qPageInfo.info))#</pagecontent>

Second

第二个

<cffile action="write" file="%filepath%/webcontent.xml" output="#qPageInfo#">

Now the real problem, qPageInfo is the query. I suspect that you wanted to write the xml.

现在真正的问题是,qPageInfo是查询。我怀疑您想要编写xml。

The simpler approach is to deal with the XML as a string

更简单的方法是将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#">

OR

If you want to make sure you are processing xml and not just a string that is xml

如果要确保处理的是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)#">

#2


2  

Not sure if you posted the actual code with your question or not but you have % percent signs instead of # hash tags around your filepath variable.

不确定您是否发布了带有问题的实际代码,但是您在filepath变量周围有%的符号而不是#散列标记。

This:

这样的:

<cffile action="write" file="%filepath%/webcontent.xml" output="#qPageInfo#">

Should be this:

应该是这样的:

<cffile action="write" file="#filepath#/webcontent.xml" output="#xmlPageInfo#">

updated to use the correct variable xmlPageInfo for writing the file as others have mentioned.

更新后使用正确的变量xmlPageInfo来编写文件,就像其他人提到的那样。