Mike Waller
asked on
xml parsing, output to txt
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
_agx_, is using an array notation better than what I'm doing? Let me know if you see a better way.
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.)
(BTW, using listAppend will prevent trailing commas in your variable.)
ASKER
_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(qOrde rsToProces s.id)#</cf output>
<cfset updateids = ValueList(qOrdersToProcess .id)>
<cfoutput>#updateids#</cfo utput>
..
Any ideas?
<cfif isXML(xmlString)>
<cfoutput>#ValueList(qOrde
<cfset updateids = ValueList(qOrdersToProcess
<cfoutput>#updateids#</cfo
..
Any ideas?
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?
How does this relate to the xml in the original post?
ASKER
actually, I figured it out.
ASKER
I just placed an update statment under the cffile statement and that worked.
So what did you do differently? Also, is the original question answered as well?
> 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.
Outside of the loop, yes? Otherwise the update would run many, many times.
ASKER
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:Ord er xmlns:ns1="http://www.url.com"><Order><x1>mo dule6-1</x 1><x2>modu le6-2</x2> </Order></ ns1:Order>
Is there a statement to check for that?
<?xml version="1.0" encoding="UTF-8"?><ns1:Ord
Is there a statement to check for that?
ASKER
Yes, I placed it outside the loop.
> 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.xmlRo ot.Order.x mlChildren )#"
You are already referencing the parent node here:
.... <cfloop from=1 to="#arrayLen(parsed.xmlRo
ASKER
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?
Yes, you can test for the existence of the key "Order". Then do something else if it was not found
<cfif structKeyExists(parsed.xml Root, "Order")>
do your existing code here
<cfelse>
"Order" key not found. do something else
</cfif>
<cfif structKeyExists(parsed.xml
do your existing code here
<cfelse>
"Order" key not found. do something else
</cfif>
ASKER
_agx_, would that go on the inside or outside of the cfloop?
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>
ASKER
Cool, thx _aqx_
ASKER
Thx
<cfset mystring = listAppend(mystring, parsed.xmlRoot.Order["x"& i ].xmlText)>