Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何结合ColdFusion查询和XML?_Xml_Coldfusion - Fatal编程技术网

如何结合ColdFusion查询和XML?

如何结合ColdFusion查询和XML?,xml,coldfusion,Xml,Coldfusion,我正在进行一个项目,在这个项目中,我必须结合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文件。这

我正在进行一个项目,在这个项目中,我必须结合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>

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

我更改了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>

删除查询会得到相同的结果,对吗?或者您跳过了创建最小示例的步骤。您的cfoutput不应该包装整个元素吗?否则,您将拥有一个具有多个customer_id元素的客户。然而,这并不能解决你的问题。在我看来,你的问题就像一个隐藏的性格问题。但是,我没有在发布的代码中看到任何隐藏字符。我需要查看生成的xml。这就是为什么删除查询可能有用的原因。@user3023588-我不是讽刺的意思,但在发布问题时,思考其他人在试图回答问题时可能需要哪些信息是有帮助的:例如,如果您遇到xml解析错误,那么。。。其他人将需要看到实际生成的XML以及试图解析它的代码(假设它不是外部服务)。没有相关信息,我们只能猜测。创建可以在任何环境中运行的,应该是故障排除过程的一部分。尝试在不使用ColdFusion的情况下获取一个工作的XML示例,然后编写ColdFusion代码以复制示例的格式@Leigh提出了一个很好的观点,即您需要包含更多信息才能得到合理的答案。感谢您的回答,您的回答是正确的,但还有一件事,每个客户记录之间的空间导致了问题。你知道如何消除他们之间的空白吗?提前谢谢!我再次调整了答案。我没有清除所有的空白。请说得更具体些。与其说它对我不起作用,不如说它为什么或如何不起作用。这会给我带来问题为什么你认为这就是问题?一个更大的问题是你缺少结束标记。将其添加到当前字符串中,它就可以用XMLParse进行解析。这就提出了一个问题,究竟是什么被用来解析XML?另一方面,虽然在技术上可以使用.cfm文件生成xml,但如果使用cfsavecontent和CFC,控制输出和空白要容易得多,而不是直接在.cfm文件中输出。@beloitdavisja-虽然在技术上可以工作,将查询和xml生成代码分离(即不将cfquery嵌套在xml IMO中)更为简洁:已同意。对于S.O.来说,清晰和简单是最好的。提到查询放置的原因是,您经常看到新用户混合查询和输出,错误地认为这是必需的,而事实并非如此,这只会使代码更难阅读。在基本示例中区分事物是鼓励更好实践的微妙方式;-。此外,它使示例更清晰、更容易理解。当然,我对询问者的背景一无所知,所以他们可能已经意识到了这一点。
<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>