如何结合 ColdFusion Query 和 XML?

How to combine ColdFusion Query and XML?

我正在做一个项目,我必须将冷聚变查询和 xml 输出结合起来。每次我尝试 运行 我的页面时,我都会收到此错误:

An Error Occurred
XML Parser Returned Error: Invalid document end (5)
Source Path: http://test/apps/Milos/staffsearch.cfm
Call Type: http
View XML

如果我将页面保存为 .cfm 文件页面 运行 没问题,但我希望该页面为 .xml 文件。这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<cfquery name="myQuery" datasource="Test">
    Select StaffId,FirstName,LastName
    From Staff
</cfquery>
<!-- These are standard elements which can be inserted back into a request -->
<cfoutput query="myQuery">
<livelookup version="1.0" columns="first_name,last_name">
    <customer>
        <customer_id>#XMLFormat(StaffId)#</customer_id>
        <first_name>#XMLFormat(FirstName)#</first_name>
        <last_name>#XMLFormat(LastName)#</last_name>
    </customer>
</livelookup>
</cfoutput>

这是我的 XML:

   <?xml version="1.0" encoding="utf-8"?> 
   <!-- These are standard elements which can be inserted back into a request -->
   <livelookup version="1.0" columns="first_name,last_name">
        <customer>
            <customer_id>6</customer_id>
            <first_name>Jon</first_name>
            <last_name>Cook</last_name>
        </customer>
        <customer>
            <customer_id>7</customer_id>
            <first_name>Dave</first_name>
            <last_name>Herman</last_name>
        </customer>
    </livelookup>

在一个XML文档中,应该只有一个父节点。您正在复制 XML 文档中的 <livelookup> 节点,这会导致语法错误。

我改变了你的 cfoutput 所在的位置,你可以在 <livelookup> 父节点中输出多个 <customer> 节点(根据 livelookup 规范 here

<?xml version="1.0" encoding="utf-8"?><cfsilent>
<cfquery name="myQuery" datasource="Test">
    SELECT  StaffId,FirstName,LastName
    FROM    Staff
</cfquery>
<!--- These are standard elements which can be inserted back into a request --->
</cfsilent>
<livelookup version="1.0" columns="first_name,last_name">
<cfoutput query="myQuery"><customer>
    <customer_id>#XMLFormat(StaffId)#</customer_id>
    <first_name>#XMLFormat(FirstName)#</first_name>
    <last_name>#XMLFormat(LastName)#</last_name>
</customer></cfoutput>
</livelookup>

这将产生输出:

<?xml version="1.0" encoding="utf-8"?>
<livelookup version="1.0" columns="first_name,last_name">
    <customer>
        <customer_id>6</customer_id>
        <first_name>Dave</first_name>
        <last_name>Cook</last_name>
    </customer>
    <customer>
        <customer_id>7</customer_id>
        <first_name>Jon</first_name>
        <last_name>Maiden</last_name>
    </customer>
    <customer>
        <customer_id>94</customer_id>
        <first_name>Ian</first_name>
        <last_name>Hart</last_name>
    </customer>
</livelookup>

使用 CF 的 xml 函数的示例将产生相同的输出:

<cfsetting enablecfoutputonly="true"  />
<cfheader name="Content-Type" value="text/xml">

<cfset xmlObj = xmlNew() />
<cfset xmlObj.livelookup = xmlElemNew( xmlObj, 'livelookup' ) />
<cfset xmlObj.livelookup.xmlAttributes['version'] = '1.0' />
<cfset xmlObj.livelookup.xmlAttributes['columns'] = 'first_name,last_name' />

<cfloop query="myQuery">
    <cfset xmlObj.livelookup.xmlChildren[currentRow] = xmlElemNew( xmlObj, 'customer' )>

    <cfset xmlObj.livelookup.xmlChildren[currentRow]['customer_id'] = xmlElemNew( xmlObj, 'customer_id' )>
    <cfset xmlObj.livelookup.xmlChildren[currentRow]['customer_id'].xmlText = myQuery.StaffId>

    <cfset xmlObj.livelookup.xmlChildren[currentRow]['first_name'] = xmlElemNew( xmlObj, 'first_name' )>
    <cfset xmlObj.livelookup.xmlChildren[currentRow]['first_name'].xmlText = myQuery.firstName>

    <cfset xmlObj.livelookup.xmlChildren[currentRow]['last_name'] = xmlElemNew( xmlObj, 'last_name' )>
    <cfset xmlObj.livelookup.xmlChildren[currentRow]['last_name'].xmlText = myQuery.lastName>
</cfloop>

<cfoutput>#toString(xmlObj)#</cfoutput>