Highlighted

Extract data from XML

Enthusiast ,
Aug 21, 2018

Copy link to clipboard

Copied

(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

Adobe Community Professional
Correct answer by BKBK | Adobe Community Professional

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

Views

414

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Extract data from XML

Enthusiast ,
Aug 21, 2018

Copy link to clipboard

Copied

(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

Adobe Community Professional
Correct answer by BKBK | Adobe Community Professional

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

Views

415

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Aug 21, 2018 0
Adobe Community Professional ,
Aug 21, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 21, 2018 0
Enthusiast ,
Aug 21, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 21, 2018 0
Adobe Community Professional ,
Aug 21, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 21, 2018 0
Enthusiast ,
Aug 21, 2018

Copy link to clipboard

Copied

I did notice that, and added it, but that's when I got that error

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 21, 2018 0
LEGEND ,
Aug 21, 2018

Copy link to clipboard

Copied

I had a project a long time ago that required allowing users to upload an XML file (typically 3MB) that would extract location data for offices around the world, build a query and insert it into a database.  It's very long and complex, and I won't post it here.. but I will provide some code samples, maybe that can help.

As far as the error message you are getting, something, somewhere is trying to save a complex object as a variable, which obviously can't be done.  I'd have to see your code in order to spot it.

Anyhoo.. after a file is uploaded, I check to make sure that the mimetype is application/xml or text/xml.  Once that has passed, then I read the contents of the .xml file into a variable and check that the contents are proper XML.

<cffile action="read" file="#variables.folder#/to.xml" variable="variables.thisXML" />

<cfswitch expression="#IsXml(variables.thisXML)#">

    <cfcase value="yes">

          <cfset variables.thisXML = xmlParse(variables.thisXML) />

          <cfset variables.CNSLarray = XmlSearch(variables.thisXML,'/contact_info/list_org_id') /><!--- my desired information is now in an array --->

          <cfset variables.CNSL = QueryNew('ID,ORG_ID,NAME,CITY,STATE,ZIP,EMAIL,PHONE') />

          <cfset QueryAddRow(variables.CNSL,arrayLen(variables.CNSLarray)) /><!--- add rows equal to length of my array --->

          <cfloop from="1" to="#arrayLen(variables.CNSLarray)#" index="idx">

              <cfset querySetCell(variables.CNSL,'ID',variables.CNSLarray[idx]) />

              ...

          </cfloop>

          <cfset StructDelete(variables,'CNSLarray')  /><!--- garbage collection --->

              <!---  query to delete all data in location table --->

              <!--- query to insert all new data --->

    </cfcase>

    <cfdefaultcase>FILE IS NOT XML - ABORTING<cfabort /></cfdefaultcase>

</cfswitch>

HTH,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 21, 2018 0
Enthusiast ,
Aug 21, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 21, 2018 0
LEGEND ,
Aug 21, 2018

Copy link to clipboard

Copied

Depends upon the error message/details.

V/r,

^ _ ^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 21, 2018 0
Adobe Community Professional ,
Aug 21, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 21, 2018 1
Enthusiast ,
Aug 21, 2018

Copy link to clipboard

Copied

Got it thanks that did the trick, I can now see the data. My next hurdle would be to actually find a way to use it.

As I've set a developer on this now, I'll get to see how he does this final part and hopefully I'll be able to reverse that in order to create it for other API's that I also need to create.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 21, 2018 0
Adobe Community Professional ,
Aug 22, 2018

Copy link to clipboard

Copied

Hi @ACS LLC, please mark the correct answer. It will help someone in future.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 22, 2018 0
ACS_LLC LATEST
Enthusiast ,
Aug 24, 2018

Copy link to clipboard

Copied

Done

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Aug 24, 2018 1