Copy link to clipboard
Copied
i'm trying parse a user selected XML file and am completely stuck. i've read through the Javascript Tools Guide CS6 - XML Object Reference section several times, but i must be missing something. here is my XML:
<xml>
<comp>
<name>My Comp</name>
<width>1280</width>
<height>720</height>
<fps>24</fps>
</comp>
<layers>
<layer value="Layer Name 1">1</layer>
<layer value="Layer Name 2">2</layer>
<layer value="Layer Name 3">3</layer>
<layer value="Layer Name 4">4</layer>
</layers>
</xml>
and here is my script attempting to parse it:
var config = File.openDialog("Choose A Config File","XML:*.xml");
if (config)
{
var xmlString = config.toString();
var myXML = new XML(xmlString);
alert(myXML.length);
} else {
alert("Could not open file.");
}
i get a blank alert box. future thanks for the help.
Copy link to clipboard
Copied
Hi,
As for all the properties in XML, you need to put a pair of parenthesis at the end, like that myXML.length(). eg.name(), localName(), parent(), etc...
By calling myXML.length, you are actually asking for the child called length, like this :
<xml>
<length/>
</xml>
As there is no child called length in your xml, you get no answer. Also, if you need to alert an XML, use myXML.toXMLString().
So whachout, it is an error that will happen quite a few times in your life. It is easy to lose a day of work trying to fing the bug when it is actually a forgotten pair of parenthesis (happenned to me last month).
Copy link to clipboard
Copied
@chauffeurdevan thanks for pointing that out; i didn't see that "()" was expected when reading all XML properties.
however, i'm still unclear about how to traverse and read through the XML nodes. for instance, how would i find and then alert the "width" node from my XML?
<xml>
<comp>
<width>1280</width>
</comp>
</xml>
Copy link to clipboard
Copied
There is a lot of way, depending on how much you know the xml you are parsing.
Here is some example, and you can mix between them.
Let's call the xml you just wrote myXML
alert(myXML.comp.width)
for each(var child in myXML){
for each(var subchild in child){
alert(subchild)
}
}
alert(myXML..width)
alert(myXML.comp.width)
alert(myXML.children()[0].children()[0])
I really suggest you give a look at this page, it is written for ActionScript, but it is really similar to javascript/extendscript http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4
Copy link to clipboard
Copied
interesting, that's what i've been testing, but to no avail. i still get a blank alert box. i'm not sure if this makes any difference but i'm testing my code in After Effects, not through Extendscript.
also, whenever i alert() for any value using toXMLString() i get the path to the XML file (e.g. ~/Desktop/Test.xml). here's my *.jsx code:
var config = File.openDialog("Choose Character Config File","XML:*.xml");
if (config)
{
var xmlString = config.toString();
var myXML = new XML(xmlString);
alert(myXML.toXMLString());
alert(myXML.comp.width);
} else {
alert("could not open file");
}
and my sample XML:
<xml>
<comp>
<name>My Comp</name>
<width>1280</width>
<height>720</height>
<fps>24</fps>
</comp>
</xml>
when i run this, i get an alert box that shows "~/Desktop/Test.xml", followed by another alert box that blank.
Copy link to clipboard
Copied
I think what you'e missing is actually reading and closing the XML file:
var xmlString = config.read();
var myXML = new XML (xmlString);
config.close();
Dan
Copy link to clipboard
Copied
@Dan thanks, you got me 99% there. i had to add config.open("r"); to make it finally work.
for future strugglers reference, here's the final code:
var config = File.openDialog("Choose Config File","XML:*.xml");
if (config)
{
config.open("r");
var xmlString = config.read();
var myXML = new XML(xmlString);
alert(myXML.toXMLString());
config.close();
} else {
alert("Could not open file");
}
Copy link to clipboard
Copied
Still little result even with that. The xml variable contains the original file's text, but it won't parse ...
The code :
f = File("test.xml");
f.open("r");
if(f) {
f.readln();
var xml = new XML(f.read());
f.close();
alert(xml.toString());
alert(xml.PPTSlideShowSession);
}
The file :
<?xml version="1.0" encoding="utf-8"?>
<PPTSlideShowSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" PPTSlideShowStartDateTime="2013-11-08T14:27:28.6445308+01:00" PPTSlideShowEndDateTime="2013-11-08T14:33:40.8945308+01:00" PowerpointVersion="12.0" xmlns="http://tempuri.org/Schema.xsd">
<ShowSlideInfo SlideId="271" SlideIndex="1" SlideNumber="1" BeginTime="5008" />
<ShowSlideInfo SlideId="287" SlideIndex="2" SlideNumber="2" BeginTime="10003" />
<ShowSlideInfo SlideId="288" SlideIndex="3" SlideNumber="3" BeginTime="15007" />
</PPTSlideShowSession>
The output :
First alerts show the raw file content
Second alert : nothing (blank)
Copy link to clipboard
Copied
It seems the colons in the PPTSlideShowSession attributes is messing it all up.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more