Solved

xml parsing, output to txt

Posted on 2009-04-01
19
199 Views
Last Modified: 2013-12-24
I have the following code and am trying to output to a file which works but it only grabs the last record in the db and outputs to a txt.  It should loop through all records and output all records the the file.  also, how would I adjust it so that there is a line break after each record in the txt?
<cfif qOrdersToProcess.recordcount GT 0>
 

<cfloop query=qOrdersToProcess>

<cfset xmlString="#orderString#">

<cfif isXML(xmlString)>

<cfset parsed=XmlParse(xmlString)>

<cfset mystring="">
 

<cfloop from=1 to="#arrayLen(parsed.xmlRoot.Order.xmlChildren)#" step=1 index=i>

<cfset mystring=mystring & #evaluate("parsed.xmlRoot.Order.x#i#.xmlText")# & ','>

</cfloop>
 

<cfset myString = Left(myString, Len(myString)-1)>

<cffile action="write" file="#srcDir#CNV#counter##dateFormat(endDate,'mmdd')#"  output="#mystring#">
 

</cfif>

</cfloop>
 

</cfif>

Open in new window

0
Comment
Question by:COwebmaster
  • 10
  • 9
19 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 24041461
Using action="write" overwrites any existing file.  Since you are inside a loop, each iteration overwrites the previous line, leaving only the last line in the query.

Try using append instead:

<cffile action="append" addNewLine="yes"
file="#srcDir#CNV#counter##dateFormat(endDate,'mmdd')#"  
output="#mystring#">
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24041480
Also, have you tried using array notation to eliminate the need for evaluate(..)?

<cfset mystring = listAppend(mystring, parsed.xmlRoot.Order["x"& i ].xmlText)>
0
 

Author Comment

by:COwebmaster
ID: 24041542
_agx_, is using an array notation better than what I'm doing?  Let me know if you see a better way.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24041588
Yes, it saves a little extra processing that you really do not need.  IMO it is also cleaner code.  

 (BTW, using listAppend will prevent trailing commas in your variable.)
0
 

Author Comment

by:COwebmaster
ID: 24042031
_agx_, I'm trying to pull back the ids and place them in a ValueList.  I've tried this but it pulls back all ids and repeats them for every iteration..

<cfif isXML(xmlString)>
<cfoutput>#ValueList(qOrdersToProcess.id)#</cfoutput>
<cfset updateids = ValueList(qOrdersToProcess.id)>
<cfoutput>#updateids#</cfoutput>
..

Any ideas?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24042193
Yes, you can't really do that. If you try and combine valueList (all values) with a loop through _individual_ records you are going to get duplicates.

How does this relate to the xml in the original post?  
0
 

Author Comment

by:COwebmaster
ID: 24042261
actually, I figured it out.
0
 

Author Comment

by:COwebmaster
ID: 24042274
I just placed an update statment under the cffile statement and that worked.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24042284
So what did you do differently?  Also, is the original question answered as well?
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 52

Expert Comment

by:_agx_
ID: 24042293
> I just placed an update statment under the cffile statement and that worked.

Outside of the loop, yes? Otherwise the update would run many, many times.
0
 

Author Comment

by:COwebmaster
ID: 24042305
Actually, there is one last piece I need to do.  I have a isXML statement in there to make that the record is correctly formatted.  However, I need to make sure the parent node is a certain value, say "Order".  So my xml statement would look like..

<?xml version="1.0" encoding="UTF-8"?><ns1:Order xmlns:ns1="http://www.url.com"><Order><x1>module6-1</x1><x2>module6-2</x2></Order></ns1:Order>

Is there a statement to check for that?                                                            
0
 

Author Comment

by:COwebmaster
ID: 24042311
Yes, I placed it outside the loop.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24042835
> I need to make sure the parent node is a certain value, say "Order"

You are already referencing the parent node here:

 .... <cfloop from=1 to="#arrayLen(parsed.xmlRoot.Order.xmlChildren)#"
0
 

Author Comment

by:COwebmaster
ID: 24043381
Ok, but how can I be sure that the parent node is correct?  In other words, a third party will be inserted those xml strings into the db so I want to make sure Order is correct and not Orders and so on.  Is there a way to check for that?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24043485
Yes, you can test for the existence of the key "Order". Then do something else if it was not found

<cfif structKeyExists(parsed.xmlRoot, "Order")>
      do your existing code here
<cfelse>
      "Order" key not found. do something else
</cfif>
0
 

Author Comment

by:COwebmaster
ID: 24043663
_agx_, would that go on the inside or outside of the cfloop?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 24044030
It depends on what you want to do if the key does not exist. But probably Inside the loop, after the isXML check

<cfif isXML(xmlString)>

    <cfset parsed=XmlParse(xmlString)>

    <cfif structKeyExists(parsed.xmlRoot, "Order")>

    ... do the loop and file append here

   </cfif>

</cfif>

Open in new window

0
 

Author Comment

by:COwebmaster
ID: 24046131
Cool, thx _aqx_
0
 

Author Closing Comment

by:COwebmaster
ID: 31565421
Thx
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Web Site Hosting 10 80
Configure IIS to process JSON 10 75
Checkbox and ListView in Android Layout 4 56
setup wamp server for first time 2 46
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now