"... break that long line of E4X into separate components..."
I am with you here.  Do it one step at a time and check each step with a
trace(xml.toXMLString()).  If you really like concise, hard to read code,
you can squash it back together once you get it working.  

Tracy Spratt,
Lariat Services, development services available

-----Original Message-----
From: [email protected] [mailto:[email protected]] On
Behalf Of Ian Thomas
Sent: Monday, June 22, 2009 1:26 PM
To: [email protected]
Subject: Re: [flexcoders] Need help tranforming some XML

Hi Jason,
  Stepping through it, it works when you break that long line of E4X
into separate components.

(For some reason people seem to want to write really long E4X
statements - I'm not quite sure why...)

Like this, your loop works.

for each (var contentRowItem:XML in _contentXML..row)
{
   var moduleName:String = contentRowItem.Module;
   var topicXML:XML = new XML("<topic />");
   topicx...@title = contentRowItem.LinkTitle;
   // This e4x creates an XMLList, so pull off the first entry
   var module:XML=finalXML.module.(@title == moduleName)[0];
   // Likewise
   var
topics:XML=module..subsection.(@title==contentRowItem.Subsection).topics[0];
   topics.appendChild(topicXML);
}

However - I'd include a bit of error checking for those XMLLists...

for each (var contentRowItem:XML in _contentXML..row)
{
   var moduleName:String = contentRowItem.Module;
   var topicXML:XML = new XML("<topic />");
   topicx...@title = contentRowItem.LinkTitle;
   var modulesList:XMLList=finalXML.module.(@title == moduleName);
   if (modulesList.length()!=1)
     throw new Error("Can't find single module with title:"+moduleName);

   var topicsList:XMLList=
modulesList[0]..subsection.(@title==contentRowItem.Subsection).topics;
   if (topicsList.length()!=1)
     throw new Error("Can't find single subsection with
title:"+contentRowItem.Subsection);

   topicsList[0].appendChild(topicXML);
}

My general approach to this type of stuff is: break it down. Store and
trace out each part of your e4x statement in a separate variable, bit
by bit. That'll show you where it's going wrong.

HTH,
   Ian

On Mon, Jun 22, 2009 at 5:54 PM, Merrill,
Jason<[email protected]> wrote:
>
>
> Been wrestling with a script that transforms some XML from one schema to
> another for a while, and have most of the transformation working, but
having
> a hard time wrapping my head around the last little bit.  Probably easy
for
> some E4X XML gurus out there.  The original XML (built automatically by
the
> sever-side app) is fairly flat and looks like this:
>
> (_contentXML)
>
> <xml>
>
>   <rows>
>
>
>
>     <row>
>
>       <ID>1</ID>
>
> <Module>iBuild</Module>
>
> <Subsection>Assumptions</Subsection>
>
>       <Topic>Starting iRequire</Topic>
>
>       <Overview>To start iRequire, you will first need to do a few things,
> like create a user name and password.</Overview>
>
>       <Author>Sanders, Larry</Author>
>
>       …etc.
>
>     </row>
>
>
>
>     <row>
>
>       <ID>2</ID>
>
> <Module>iDeliver</Module>
>
> <Subsection>Technical Constraints</Subsection>
>
>         …etc.
>
> What I need to do, is take it from that “flat” form, and based on the
values
> of some of the nodes (<Module>,<Subsection>, and <Topic> values), make new
> more hierarchical XML that is based on the Module, Subsection, and Topic
> values in the XML, so that it looks like this:
>
> <xml>
>
> <modules>
>
>                 <module title="iPlan" orderID="5">
>
>                 <subsections>
>
>                                 <subsection title="Assumptions">
>
>                                         <topics>
>
>                                                 <topic title=”Starting
> iRequire”>
>
>                                                         <overview>
>
>                                                                 To start
> iRequire, you will first need to do a…
>
>                                                         </overview>
>
> <author>Sanders, Larry</author>
>
>                                         </topics>
>
>                         <subsection title="Project Information"/>
>
>                         </subsections>
>
>                 </module>
>
>                 <module title=”iDeliver” orderID=”6”>
>
>                         <subsections>
>
>                                 <subsection title="Assumptions"/>
>
>                         ..etc.
>
> I have it assembled this far, where the module nodes are created just
fine,
> and the subsections are appearing under the right module with the right
> title attribute:
>
> (finalXML)
>
> <xml>
>
> <modules>
>
>                 <module title="iPlan" orderID="5">
>
>                 <subsections>
>
>                                 <subsection title="Assumptions">
>
>                                         <topics />
>
> </subsection>
>
>                         <subsection title="Project Information">
>
>                                         <topics />
>
> </subsection>
>
>                         </subsections>
>
>                 </module>
>
>                 <module title=”iDeliver” orderID=”6”>
>
>                         <subsections>
>
>                                 <subsection title="Assumptions">
>
> <topics />
>
> </subsection>
>
>                         <subsection title="Technical Constraints">
>
>                                         <topics />
>
> </subsection>
>
> <subsection title="Design Phase Documents">
>
>                                         <topics />
>
> </subsection>
>
> <subsection title="Implementation Phase Documents">
>
> <topics />
>
> </subsection>
>
>                         </subsections>
>
>                 </module>
>
>                 …etc.
>
> But what I can’t seem to get is that last part of how to insert the right
> topic data under each <topics> node (Overview, ID, Author, etc).  I’d be
> happy enough just inserting the entire original <row> node into the right
> <topic> node.  My current attempt looks like this:
>
> (finalXML is my “new“ XML and _contentXML is the original XML)
>
> for each (var contentRowItem:XML in _contentXML..row)
>
> {
>
>         var moduleName:String = contentRowItem.Module;
>
>         var topicXML:XML = new XML(<topic />);
>
>         topicx...@title = contentRowItem.LinkTitle;
>
>         finalXML..module.(@title == moduleName)..subsection.(@title ==
> contentRowItem.Subsection).topics.appendChild(topicXML)
>
> }
>
> But with this approach, I get the error, “TypeError: Error #1086: The
> appendChild method only works on lists containing one item.” I’ve also
tried
> some forms of theNode.setChildren and so many ways to loop my head is
> spinning. Any ideas?
>
> Really, my question just boils down to inserting all the right oringal
> <row/> node(s) from the original XML as children into the new <topics/>
node
> of the new finalXML based on the Module, Subsection, and LinkTitle values
> from the original <row> node.
>
>
>
> Any help is GREATLY appreciated!
>
> Jason Merrill
>
> Bank of  America   Global Learning
>
> Shared Services Solutions Development
>
> Monthly meetings on the Adobe Flash platform for rich media experiences -
> join the Bank of America Flash Platform Community
>
> 


------------------------------------

--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Alternative FAQ location:
https://share.acrobat.com/adc/document.do?docid=942dbdc8-e469-446f-b4cf-1e62
079f6847
Search Archives:
http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups Links





Reply via email to