8 Replies Latest reply on Jul 1, 2017 6:33 AM by BKBK

    How to dump XML from a post

    sdettling222 Level 1

      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!!!!

        • 1. Re: How to dump XML from a post
          WolfShade Level 4

          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,

           

          ^ _ ^

          • 2. Re: How to dump XML from a post
            sdettling222 Level 1

            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!!!

            • 3. Re: How to dump XML from a post
              WolfShade Level 4

              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,

               

              ^ _ ^

              • 4. Re: How to dump XML from a post
                sdettling222 Level 1

                Is there any way to simply ready the post instead of saving the file? I would like to insert into a database instead of saving a file.

                 

                Thanks!!!

                • 5. Re: How to dump XML from a post
                  Steve Sommers Level 4

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

                  • 6. Re: How to dump XML from a post
                    WolfShade Level 4

                    There is a way to do that, I've done it.  Sadly that was many, many years ago, and I am not recalling how I did it.  I don't have access to those .cfm files to see, anymore.  But I remember setting it up so that if a user uploaded a file, that file was saved to a variable (as opposed to saving it to a folder) then directly inserted into a database BLOB.

                     

                    Of course, this was for an intranet, not a public-facing website.  I didn't have to consider that some malicious actor would try to upload a virus.  If this is for a public-facing website, you'll want to save it to a folder that has execute permissions removed and allow a virus-checker to scan it before you do anything with it.

                     

                    V/r,

                     

                    ^ _ ^

                    • 7. Re: How to dump XML from a post
                      sdettling222 Level 1

                      Thanks Steve, I will certainly try that...

                       

                      Appreciate the feedback!!!!

                      • 8. Re: How to dump XML from a post
                        BKBK Adobe Community Professional & MVP

                        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 .