PDA

View Full Version : What's Wrong with my XML?


soggybag
03-17-2008, 10:20 PM
I'm trying to get the p tags out of this XML document. But I can't seem to figure out what I'm doing wrong. Help me out what am I missing?

I don't get an errors. But i don't see any output? It seems that it is just not finding the p tags.

Here is a snippet of my AS3 code:
private function xml_loaded( e:Event ):void {
var my_xml:XML;
my_xml = new XML( e.target.data );
my_xml.ignoreWhitespace = true;

var style_nodes // :XMLList;
var caption_nodes // :XMLList;

style_nodes = my_xml.child( "styling" );
caption_nodes = my_xml.tts.body.child( "p" );
// caption_nodes = my_xml.child( "p" );

for each ( var p in caption_nodes ) {
var _obj:Object = new Object();
var cap_text:String = p.toString();
var begin:String = p.attribute( "begin" ).toString();
var end:String = p.attribute( "end" ).toString();
trace( begin + ":" + ":" + end + " " + cap_text );
_obj.cap_text = cap_text;
}

Here's a snippet of the XML. There are no errors here I have used this file with different cod and works fine.
<?xml version="1.0" encoding="iso-8859-1"?>
<tt xmlns="http://www.w3.org/2006/04/ttaf1" xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling" xml:lang="en">
<head>
<styling>
<style id="defaultSpeaker" tts:fontSize="12px" tts:fontFamily="SansSerif" tts:fontWeight="normal" tts:fontStyle="normal" tts:textDecoration="none" tts:color="white" tts:backgroundColor="black" tts:textAlign="left" />
<style id="defaultCaption" tts:fontSize="12px" tts:fontFamily="SansSerif" tts:fontWeight="normal" tts:fontStyle="normal" tts:textDecoration="none" tts:color="white" tts:backgroundColor="black" tts:textAlign="center" />
</styling>
</head>
<body id="thebody" style="defaultCaption">
<div xml:lang="en">
<p begin="0:00:00.00" end="0:00:01.02"></p>
<p begin="0:00:01.02" end="0:00:02.06">So in 2003,</p>
<p begin="0:00:02.06" end="0:00:05.00">I got married to my husband, who's from Belgium.</p>
<p begin="0:00:07.04" end="0:00:10.06">And we bought a house in Ghent, Belgium.</p>
<p begin="0:00:10.06" end="0:00:12.00">And one summer,</p>

cybereality
03-17-2008, 10:31 PM
//find the number of p tags
var pTags= xml.tt[0].body[0].div[0].elements("p").length();
// loop through tags
for(var i=0; i<pTags; i++){
// parse data and attributes
var currentData = xml.tt[0].body[0].div[0].p[i];
var currentBegin = xml.tt[0].body[0].div[0].p[i].@begin;
var currentEnd = xml.tt[0].body[0].div[0].p[i].@end;
// do something to data here
// ADD CODE HERE
}

soggybag
03-18-2008, 02:13 AM
Thanks for the reply. But it's still not working.

When I trace the following I get undefined?

var my_xml:XML;
my_xml = new XML( e.target.data );
my_xml.ignoreWhitespace = true;
var pTags = my_xml.tt[0] // .body[0].div[0].elements( "p" ).length();
trace( pTags );

soggybag
03-18-2008, 02:19 AM
Seems if I trace the following I get 0. This should show 2.

var pTags = my_xml.child( "tt" ).length();

If I trace my_xml I get the text of my XML file?

trace( my_xml )

soggybag
03-18-2008, 05:34 AM
After banging my head on the keyboard for an hour or more I find that the following works:

var my_xml:XML;
my_xml = new XML( e.target.data );
// my_xml.ignoreWhitespace = true;

for each ( var p:XML in my_xml..p ) {
trace( p.@begin + " " + p.@end + " " + p );
}

If anyone can explain why the earlier do not work feel free to "school" me.

wvxvw
03-18-2008, 03:37 PM
var xml:XML = <tt xmlns="http://www.w3.org/2006/04/ttaf1" xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling" xml:lang="en">
<head>
<styling>
<style id="defaultSpeaker" tts:fontSize="12px" tts:fontFamily="SansSerif" tts:fontWeight="normal" tts:fontStyle="normal" tts:textDecoration="none" tts:color="white" tts:backgroundColor="black" tts:textAlign="left" />
<style id="defaultCaption" tts:fontSize="12px" tts:fontFamily="SansSerif" tts:fontWeight="normal" tts:fontStyle="normal" tts:textDecoration="none" tts:color="white" tts:backgroundColor="black" tts:textAlign="center" />
</styling>
</head>
<body id="thebody" style="defaultCaption">
<div xml:lang="en">
<p begin="0:00:00.00" end="0:00:01.02"></p>
<p begin="0:00:01.02" end="0:00:02.06">So in 2003,</p>
<p begin="0:00:02.06" end="0:00:05.00">I got married to my husband, who's from Belgium.</p>
<p begin="0:00:07.04" end="0:00:10.06">And we bought a house in Ghent, Belgium.</p>
<p begin="0:00:10.06" end="0:00:12.00">And one summer,</p>
</div>
</body>
</tt>;
var ns:Namespace = xml.inScopeNamespaces()[0];
trace(xml..*::p.toXMLString());
trace('======================= '+ns+' =======================');
trace(xml..ns::p.toXMLString());
If you defined a namespace for a tag, than, you have to refer the tag by it's full name (QName) which consists of Namespace and localName.

lookatme0128
03-18-2008, 05:36 PM
you may need a listener to know when to access the XML. you are probably accessing it before it's fully loaded so it's blank. although your function is accepting an Event parameter so maybe you do have it?

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);



function LoadXML(e:Event):void
{
xmlData = new XML(e.target.data);
trace(xmlData)

}

good luck dunno if i helped.

wvxvw
03-18-2008, 06:11 PM
Oh, and BTW:
my_xml.ignoreWhitespace = true; will do absolutely nothing %)
ignoreWhitespace is a static property of XML class, so, if you want to use it, the syntax is as follows:
var xml:XML = <xml />;
XML.ignoreWhitespace = true;

soggybag
03-18-2008, 06:30 PM
Thanks for the replies. It appears the new system for working with XML in AS3 is very different from the AS2 version.

I had added ignoreWhiteSpace = true as this was useful in AS2.