我正在进行项目,我必须将coldfusion查询和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>
答案 0 :(得分:2)
在XML文档中,应该只有一个父节点。您正在复制XML文档中的<livelookup>
节点,这会导致语法错误。
我更改cfoutput
所在的位置,您可以在<customer>
父节点中输出多个<livelookup>
个节点(根据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>