iterating through XML to create Markers

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
lya98
Posts: 15
Joined: December 7th, 2006, 10:39 pm

Hello,

The new forum looks great! Lets say I've got an XML file that looks like this:

Code: Select all

<CJ>
    <vidPlayerTextFrame mark="false">
      <vidPlayer mark="false">
       <Trans_IN mark="true">Side</Trans_IN>
       <vidName mark="true">blarg - brand</vidName>
       <vidID mark="true">3943k3493</vidID>
       <vidLogo mark="true">dlfkjdf.psd</vidLogo>
       <vidText mark="true">amazing text</vidText>
      </vidPlayer>
     </vidPlayerTextFrame>
    <vidPlayerTextFrame mark="false">
      <vidPlayer mark="false">
       <Trans_IN mark="true">Flip</Trans_IN>
       <vidName mark="true">bloo - brand</vidName>
       <vidID mark="true">3943k3493</vidID>
       <vt idLogo mark="true">dlfkjdf.psd</vidLogo>
       <vidText mark="true">amazing text</vidText>
      </vidPlayer>
     </vidPlayerTextFrame>
 </CJ>
I use the file to create an XML object called XmlObj. What I'd like to
do is create a layer marker, x seconds apart, for each element that
has a mark="true" attribute, in the same order as they appear in the
XML tree.

It's very easy to create the marker by explicity referencing the XML
element by name, like this:

Code: Select all

var markValue = aaXmlObj.vidPlayerTextFrame[0].vidPlayer[0].vidName[0];

var mv = new MarkerValue(markValue);


But how do I iterate through the whole XML object without knowing what the elements are before hand or knowing how deeply nested the tree will be?

Thanks!

-louai
bradshaw1965
Posts: 98
Joined: March 14th, 2006, 2:16 pm
Location: Atlanta, GA
Contact:

Hey louai,

The general idea is to loop through the outermost element that you know will be consistent.

So, if the CJ element is known to have vidPlayerTextFrame as children

(pseudocode, I'm not sitting in front of a dev setup)

Code: Select all

var xmlNodes = cj.children();

for(frame in xmlNodes){
     if(frame.name() == 'vidPlayerTextFrame' ){
          if(frame.@marker == 'true'){
               //do your stuff
          }
     }
}
that's the general idea, I'm not sure if name() is a string comparison etc.,but you loop through the known parent, check if the node is the type that you want, check the attribute that you need, then do your stuff.

If you need nested trees, you'll have to deal with children() a bit more, but it's the same idea, loop through the outermost parent(), check for children(), and find the attributes you are interested in. Also, if you don't care what the node name of the marker attribute is you can skip the name() comparison and just loop through the children and check for the marker attribute.

Best,

Dale
Dale Bradshaw
Technology Director | Primal Screen
creative-workflow-hacks.com
Atlanta, GA
lya98
Posts: 15
Joined: December 7th, 2006, 10:39 pm

Thanks Dale! After reading up on XPath I ended up doing this:

Code: Select all

//filter out the nodes that are used as markers using xpath
var MarkerNodes = XmlObj.xpath ("//*[@mark = 'true']");
Which allows me to iterate through the remaining XML like an array.

Code: Select all

for (i = 0; i <= MarkerNodes.length(); i++){
	markerOb[i] = MarkerNodes[i].name();
 etc...
	}
Please correct me if I'm wrong but it appears that Adobe did not implement the filtering or wildcard features of E4X which is why I found XPath useful here. Thanks again!
bradshaw1965
Posts: 98
Joined: March 14th, 2006, 2:16 pm
Location: Atlanta, GA
Contact:

Please correct me if I'm wrong but it appears that Adobe did not implement the filtering or wildcard features of E4X which is why I found XPath useful here. Thanks again!
yep, that's right, although I don't think Xpath is native to E4X so it's a pretty decent compromise. Your Xpath query looks great, especially if it doesn't matter where the marker attribute sits in relationship to parents and siblings. Looks like you've got a great start.
Dale Bradshaw
Technology Director | Primal Screen
creative-workflow-hacks.com
Atlanta, GA
framelabor
Posts: 2
Joined: February 19th, 2008, 1:03 pm

hi everybody, happy to be here ;-)

after spending the whole day trying to load an external xml-file from my desktop i finally give up. coming from an actionscript background i thought this was gonna be easy - well...

my question is: how do i actually load an external xml? i used

Code: Select all

xmlFile = File.openDialog()
, but this seems to only be giving me the path, not the actual data. thus

Code: Select all

var loadedXML = new XML(xmlFile);
does only contain the path, too, when i write it to the console (using toString()). also tried messing around with xmlFile.open("r"), .read, and so on but i just can't get i to work.

when i populate my own internal xml-object everything works like a charm.

what am i doing wrong here? could somebody please tell me the exact steps needed?

thank you!!

nils
lya98
Posts: 15
Joined: December 7th, 2006, 10:39 pm

Here's how I do it:

Code: Select all

//load the path to the XML file
var filePath = "/g/Automation/test.xml"

//create a new file object using the path
aafileObj = new File(filePath); 

//open the file with read privledges
aafileObj.open("r","TEXT","????");

//read the file as a string and store it in a variable
var aaXmlStr = aafileObj.read

//create an XML object using the string
var aaXmlObj = new XML (aaXmlStr);
I think the key part you were missing was actually reading the file, after it was opened with read privledges.

Code: Select all

xmlFile = File.openDialog()
This line only creates a file path, but does not open a file.

Hope this helps!
framelabor
Posts: 2
Joined: February 19th, 2008, 1:03 pm

hey thanks! you saved my day (well, half of it since it took me 2 hrs to find out that there were 2 brackets missing after the read command in your code). everything is runnin' sweet. now for some AUTOMATION!

i still get an "undefined" as last output in my console window though. any ideas where this might result from?

thanks again!
lya98
Posts: 15
Joined: December 7th, 2006, 10:39 pm

here were 2 brackets missing after the read command in your code
Ouch! Sorry about that. I must not have copy and pasted that part! That kind of thing often trips me up when working with new objects.
i still get an "undefined" as last output in my console window though. any ideas where this might result from?
I wouldn't worry too much if your code is doing what you want.
Post Reply