Skip to main content
Inspiring
August 21, 2018
Answered

Extract data from XML

  • August 21, 2018
  • 1 reply
  • 1452 views

(Testing on CF2016)

I have test CFM file that creates an XML file with the result as:

<TransferredValueTxn>

<TransferredValueTxnReq>

<ReqCat>TransferredUse</ReqCat>

<ReqAction>StatInq</ReqAction>

<Date>20100318</Date>

<Time>124430</Time>

<PartnerName>PARTNER</PartnerName>

<CardActionInfo>

<PIN>th2i-s4is-4a34-fa9k-e71u</PIN>

<AcctNum>optional</AcctNum>

<SrcRefNum>987654</SrcRefNum>

</CardActionInfo>

</TransferredValueTxnReq>

</TransferredValueTxn>

I use another CFM page to retrieve this data:

<cfhttp url="http://127.0.0.1/dev/api/test.cfm" method="get" result="objGet">

<CFDUMP VAR="#objget#">

This gives me all of the data in the FILECONTENT, which is

<?xml version="1.0" ?> <TransferredValueTxn> <TransferredValueTxnReq> <ReqCat>TransferredUse</ReqCat> <ReqAction>StatInq</ReqAction> <Date>20100318</Date> <Time>124430</Time> <PartnerName>PARTNER</PartnerName> <CardActionInfo> <PIN>th2i-s4is-4a34-fa9k-e71u</PIN> <AcctNum>optional</AcctNum> <SrcRefNum>987654</SrcRefNum> </CardActionInfo> </TransferredValueTxnReq> </TransferredValueTxn>

Could anybody give me some guidance as to how I would extract all of the data into separate variables so that I could then work with the data.

Thanks

This topic has been closed for replies.
Correct answer BKBK

Thanks for that, much appreciated.

My code was super simple

<cfhttp url="http://127.0.0.1/dev/api/test.cfm" method="get" result="objGet">

<cfdump var="#xmlParse(objget)#">

I have a developer that takes care of complex issues on an as needed basis. I decided it's going to be quicker to have him do it. He's doing the first one and then I'll be able to see if I can use that to build the rest.

I fear I'll end up spending too much time trying to get to grips with it.

There's something odd in all of this, if I try to CFOUTPUT the cfhttp.filecontent, it also throws an error, I guess it must be because of the format, maybe, possibly.


https://forums.adobe.com/people/ACS+LLC  wrote

My code was super simple

<cfhttp url="http://127.0.0.1/dev/api/test.cfm" method="get" result="objGet">

<cfdump var="#xmlParse(objget)#">

I have a developer that takes care of complex issues on an as needed basis. I decided it's going to be quicker to have him do it. He's doing the first one and then I'll be able to see if I can use that to build the rest.

I fear I'll end up spending too much time trying to get to grips with it.

There's something odd in all of this, if I try to CFOUTPUT the cfhttp.filecontent, it also throws an error, I guess it must be because of the format, maybe, possibly.

The result attribute of cfhttp is a structure. You get an error because you are attempting to parse it as if it is a string.

You were almost there. Your XML can be obtained as follows:

<cfhttp url="http://127.0.0.1/dev/api/test.cfm" method="get" result="objGet">

<cfset xmlString=objGet.fileContent>

<!--- alternative--->

<!---

<cfhttp url="http://127.0.0.1/dev/api/test.cfm" method="get">

<cfset xmlString=cfhttp.fileContent>

--->

<cfset xmlObject=xmlParse(xmlString)>

<cfdump var="#xmlObject#">

1 reply

Community Expert
August 21, 2018

This is a very brief answer due to limited time, so maybe someone else will give you a better one. But until then, put it XMLParse to see how the XML data is essentially converted to arrays and structures, and work from there.

<cfdump var="#xmlParse(objget)#>

Dave Watts, Fig Leaf Software

Dave Watts, Eidolon LLC
ACS LLCAuthor
Inspiring
August 21, 2018

Thanks Dave.

I did give it a shot to see if it could help me jump this hurdle but it returned an error

Complex object types cannot be converted to simple values.

Maybe somebody else will add to the thread.

Thanks.

Community Expert
August 21, 2018

I left off the closing quote. Otherwise, you should be able to use CFDUMP with any variable in CF at all, including complex objects.

Dave Watts, Fig Leaf Software

Dave Watts, Eidolon LLC