• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

xml formatting

New Here ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

the following code writes to an xml document. the problem is it writes everything to the same line. when i go to parse the xml doc i get an error. i would like to modify this code to write each node to it own line.

before code my xml files looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<messageList>
     <message>
            <sender>bob</sender>
            <creator>tom</creator>

     </message>

</messagelist>

           <cfset xApps = XmlParse(cfhttp.FileContent)>

            <cfset arrayInsertAt(xApps.messageList.xmlChildren,1,xmlElemNew(xApps,'message'))>
            <cfset xApps.messageList.message[1].sender = xmlElemNew(xApps,'sender')>
            <cfset xApps.messageList.message[1].sender.xmlText = "#send#">
            <cfset xApps.messageList.message[1].creator = xmlElemNew(xApps,'creator')>
            <cfset xApps.messageList.message[1].creator.xmlText = "#creat#">

           <cffile action="write" file="C:\temp\messages.xml"
        output=#toString(xApps)#>

my code does this to the xml file which does not parse.

<?xml version="1.0" encoding="UTF-8"?>
<messageList>

     <message><sender>Bill</sender><creator>Dave</creator></message><message>
       <sender>winter</sender>
       <creator>winter</creator>

    </message>

</messagelist>

TOPICS
Advanced techniques

Views

816

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
community guidelines
Valorous Hero ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

What is tyring to parse this?

XML is supposed to be white space agnostic and not care how the file is spaced.

Your version is a little hard for humans, but a parser should not care.

Votes

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
community guidelines
New Here ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

not following the humans thing but here goes. i grap an exisiting doc and parse it in a CFC

<cfhttp url="messages.xml " method="GET">

<cfset xApps = XmlParse(cfhttp.FileContent)>

i grab data from a form and insert it into the xml doc. that works fine. when i try to modify the xml a second time

it gives me a parsing error. if i update the newly written data on the xml doc manually, placing each node on its own line the xml doc will update. this is how i know my data is correct but formatting is not

Votes

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
community guidelines
Advocate ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

Show us the code. All of it.

Votes

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
community guidelines
New Here ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

here is the code for the cfc object. the form data is in the variable. this cfc gets called each time the form gets filled out and changes need to be made to the xml doc

<cfcomponent displayname="updatexml" hint="">
  <cfscript>
    //get user info needed   struct name , var name
   objUser = createObject("component", request.rootPath&".user");
   objUser.getUserProperty("frmSender", "Sender"); 
   objUser.getUserProperty("frmRequester", "creator");
   objUser.getUserProperty("frmStartDate", "StartDate");
   objUser.getUserProperty("frmStartTime", "StartTime");
   objUser.getUserProperty("frmEndDate", "EndDate");
   objUser.getUserProperty("frmEndTime", "EndTime");
   objUser.getUserProperty("frmMessage", "messageText");
   objUser.getUserProperty("selectOptionList", "OptionList");
  
  
//  objUser.getUserProperty("frmexthome", "ehome");
//  objUser.getUserProperty("frmextdatawarehouse", "extdata");
  </cfscript>
  

  <cffunction name="newmessage" hint="adds new message to messages.xml . Data comes from addUserMessage.cfm, returns boolean" output="yes" access="public" returntype="string"> 
<!---     <cffunction name="newmessage" access="remote" returntype="xml" output="false"> --->
     <cfscript>
    var send = "#request.Sender#";
     var creat = "#request.creator#";
    var sdate = "#request.StartDate#";
    var edate = "#request.EndDate#";
    var mess = "#request.messageText#";
    var opt  = "#request.OptionList#";
    var stime = "#request.StartTime#";
    var etime = "#request.EndTime#";
   </cfscript>
  
      <!--- convert time to 24 hours --->
      <cfset StartTime=#stime#>
            <cfset EndTime=#etime#>
  
   <!--- add date and time together to update xml doc --->
            <cfset startdatetime = DateFormat("#sdate#" , "yyyymmdd" )& "" & TimeFormat("#StartTime#", "Hhmm")>
            <cfset enddatetime   = DateFormat("#edate#" , "yyyymmdd" ) & "" & TimeFormat("#EndTime#", "Hhmm")>

   <!--- place to contents of checkbox choices into an array --->
            <cfset myArrayList = ListToArray(opt)>
     <!---   <cfdump var=#myArrayList#/></p>
        <cfdump var=#opt#/></p> --->

   <!--- get reference to xml page --->
   <cfhttp url="messages.xml" method="GET">
   
      <!--- parse xml page --->
   <cfset today = dateFormat(now(), "mm/dd/yyyy")>
  <!--- <cfset xApps = XmlParse("request.browserPath&\message_dummy.xml")>   --->
         <cfset xApps = XmlParse(cfhttp.FileContent)>  
  <!--- set carriage return var --->
          <cfset variables.CrLf = Chr(13) & Chr(10)>
  <!-- set new values in array --->
   <cfset arrayInsertAt(xApps.messageList.xmlChildren,1,xmlElemNew(xApps,'message'))>
            <cfset xApps.messageList.message[1].sender = xmlElemNew(xApps,'sender')>
      <cfset xApps.messageList.message[1].sender.xmlText = "#send#">
              <cfset xApps.messageList.message[1].creator = xmlElemNew(xApps,'creator')>
      <cfset xApps.messageList.message[1].creator.xmlText = "#creat#">
      <cfset xApps.messageList.message[1].dateCreated = xmlElemNew(xApps,'dateCreated')>
      <cfset xApps.messageList.message[1].dateCreated.xmlText = "#today#">
      <cfset xApps.messageList.message[1].startDate = xmlElemNew(xApps,'startDate')>
            <cfset xApps.messageList.message[1].startDate.xmlText = "#startdatetime#">            
      <cfset xApps.messageList.message[1].endDate = xmlElemNew(xApps,'endDate')>
        <cfset xApps.messageList.message[1].endDate.xmlText = "#enddatetime#">      
      <cfset xApps.messageList.message[1].messageText = xmlElemNew(xApps,'messageText')>
      <cfset xApps.messageList.message[1].messageText.xmlText = "#mess#">            
       <!--- loop through checkbox options and update xml file --->     
       <cfloop from="1" to="#arraylen(myArrayList)#" index="j">
         <cfset arrayInsertAt(xApps.messageList.message.xmlChildren, j,xmlElemNew(xApps,'forPage'))>
<!---               <cfset xApps.messageList.message[1].forPage.xmlText= "#myArrayList#">   --->
               <cfset tempvar= #myArrayList#>
                  <cfset tempvar2= #trim(tempvar)#>
                  <cfset xApps.messageList.message[1].forPage.xmlText= "#tempvar2#"> 
           </cfloop>

   <!---<cfdump var="#xApps#">   testing --->

   <!---write new values to xml page--->
     <cffile action="write" file="/webdocs/intranet/cfapps/userMessageDev/userMessage/messages.xml"
        output=#toString(xApps)#> 
       
          <!--- Return the XML code to the browser --->
    <!--- (or whatever program initiated this request) --->
    <!--- <CFCONTENT
    TYPE="text/plain"
    RESET="Yes"><CFOUTPUT>#ToString(xApps)#</CFOUTPUT> --->
          <!--- -------------------------------------------- --->
       
       
   <cfreturn xApps />
</cffunction>
</cfcomponent>

Votes

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
community guidelines
Advocate ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

Add charset attributes to all cfhttp and cffile calls. For the rest nothing obvious, I'll try to run it later.

Votes

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
community guidelines
Valorous Hero ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

jsiggia wrote:

not following the humans thing

A human looking at an XML document generally cares about line breaks and nested tag indention for ease of readability.

A computer shouldn't.  The XML specification indicates that parsers are not supposed to care.

Votes

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
community guidelines
New Here ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

got it. i am a db guy and used to seeing things in rows and columns. this

is my first web app. there must be something i am missing. when i reformat

the data i have just sent to them xml doc by open the doc in dreamweaver

and pressing enter after each node and saving i am able to parse and update

doc

thanks

Message was edited by jochemd to remove an email address

Votes

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
community guidelines
Valorous Hero ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

jsiggia wrote:

<?xml version="1.0" encoding="UTF-8"?>
<messageList>

     <message><sender>Bill</sender><creator>Dave</creator></message><message>
       <sender>winter</sender>
       <creator>winter</creator>

    </message>

</messagelist>

Is this an actual copy from your output, or did you type this in?  If the former, then the problem is the <messageList>...</messagelist> tags.  While the XML specification indicates it is to be white space agnostic, it is also to be case sensitive.  Thus the <messageList> with an upper case 'L" and </messagelist> with a lower case 'l" are not matching tags.

Upon fixing the case issue the following code worked just as expected on my system.

<cfsavecontent variable="xml"><?xml version="1.0" encoding="UTF-8"?>
<messageList>

     <message><sender>Bill</sender><creator>Dave</creator></message><message>
       <sender>winter</sender>
       <creator>winter</creator>

    </message>

</messageList></cfsavecontent>

<cfset xApps = xmlParse(xml)>
<cfdump var="#xApps#">

If the case of the "L" was just a typo in the forum post, then the problem might be with actual data?  Again is this the actual output or is a simplified example where the actual output might contain special characters such as ampersand "&" characters?

If this is the case, you may not be able to just cut and paste the XML from a browser and parse it.

Votes

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
community guidelines
New Here ,
Nov 25, 2009 Nov 25, 2009

Copy link to clipboard

Copied

this is a simplified example . i will check the xml doc on monday for the L vs  l but i believe that is also a typo on my part. i appreicate your time..enjoy the holiday

Votes

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
community guidelines
New Here ,
Dec 01, 2009 Dec 01, 2009

Copy link to clipboard

Copied

LATEST

i cleaned up the data on my xml file to simplify it and make it easier to debug. I also update code to include charset value. my xml doc is updating correctly now. thank you for your time and help with this issue

    <cfhttp url="messages.xml" method="GET" charset = "UTF-8">

Votes

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
community guidelines
Resources
Documentation