Skip to main content
sdettling222
Known Participant
June 23, 2017
Answered

How to dump XML from a post

  • June 23, 2017
  • 2 replies
  • 2080 views

Hello all,

I have tried looking through the forum but could not find an answer......

I wanted to see if someone could tell me the best way dump XML that is posted to a cfm page we have setup. We will then insert into a table and the xmlparse directly from the table record.

Any assistance would be greatly appreciated.

Thank you very much!!!!

    This topic has been closed for replies.
    Correct answer Steve Sommers

    By POST I assume you mean the body content of the POSTed request is the raw XML:

         <cfif findNoCase("text/xml",cgi.CONTENT_TYPE)>

              <cfset variables.xmlText = getHTTPRequestData().content />

         </cfif>

    Now variables.xmlText is the xml string. To dump it as an xml document you need to parse it into an xml document first:

         <cfset variables.xmlText = parseXML(variables.xmlText) />

         <cfdump var="#variables.xml#" />

    2 replies

    Steve SommersCorrect answer
    Brainiac
    June 26, 2017

    By POST I assume you mean the body content of the POSTed request is the raw XML:

         <cfif findNoCase("text/xml",cgi.CONTENT_TYPE)>

              <cfset variables.xmlText = getHTTPRequestData().content />

         </cfif>

    Now variables.xmlText is the xml string. To dump it as an xml document you need to parse it into an xml document first:

         <cfset variables.xmlText = parseXML(variables.xmlText) />

         <cfdump var="#variables.xml#" />

    sdettling222
    Known Participant
    June 26, 2017

    Thanks Steve, I will certainly try that...

    Appreciate the feedback!!!!

    BKBK
    Adobe Expert
    July 1, 2017

    There are usually 2 ways to post XML to a CFM page:

    1) via an upload action

    <form method="post" enctype="multipart/form-data">

    <input name="FileContents" type="file"> 

    </form>

    2) as a string, for example,

    <form method="post">

    <input name="xmlContent" type="text"> 

    </form>

    If the poster used the first method, then the uploaded file would be saved to disk, and you could proceed as WolfShade suggests. If the poster used the second method, then the XML file would be available on your CFM page as the variable form.xmlContent .

    WolfShade
    Brainiac
    June 23, 2017

    Hi, sdettling222​,

    The first thing to do is to make sure that the upload is going to a folder outside of the wwwroot that has execution privileges off.

    Then parse the file using CFFILE and save it to a (scoped) variable, and use IsXML() to make sure that the file is XML.

    If the XML file will always have the same format/layout, then you can use XmlSearch(xmlVar,/path/to/your/info) to create an array of the data.  You can then use XmlParse() to get the values.

    HTH,

    ^ _ ^

    sdettling222
    Known Participant
    June 23, 2017

    Thank you very much for the reply.

    I will get right to work looking into this option.

    This will alway be the same structure with varying data. Im used to using cfhttp to hit a feed that returns XML, but really haven't worked with items posting to a page. I would like to first see what is being posted. Is there some kind of way that I can simply do a dump of the xml being send so I can add it to a table? It's just for testing purposes.

    What would be great is if there was a universal #xml.output# variable that CF would know just to grab and insert.

    Any feedback would be greatly appreciated!!!

    WolfShade
    Brainiac
    June 26, 2017

    For testing purposes, you can always CFDUMP it after you have used XmlParse() on it.

    For example, once the file has been uploaded, use CFFILE to 'read' the XML into a variable, use XmlParse() on the variable to set another variable, then CFDUMP that.

    <cffile action="read" file="/path/to/uploaded/file.xml" variable="variables.thisXML" />

    <cfset variables.parsedXML = XmlParse(variables.thisXML) />

    <cfdump var="#variables.parsedXML#" label="XML" expand="yes" />

    HTH,

    ^ _ ^