03-15-2011 05:06 PM
03-15-2011 05:43 PM
The file extension should be irrelevant. The mimetype may be more important.
A simple text embed as I showed earlier would work, with the results of toString() being passed to XML(). It can take numerous types and convert them.
If you data might contain any Unicode information, you'd want to pay particular attention to encoding issues. Generally the file would probably be UTF-8 encoded, so then you might need to use readUTF() on the object retrieved from the embed class. To avoid future problems, you should probably just start with that one, unless you know you will have a different encoding.
03-17-2011 09:54 AM - edited 03-17-2011 03:43 PM
So my code is pretty much as follows for searchers looking for solutions:
package
{
import flash.utils.ByteArray;
import qnx.ui.text.Label;
public class MYXML extends Sprite
{
super();
private var E1 = String: new String('string');
[Embed('MyXML.xml', mimeType='application/octet-stream')]
private var MyXMLFILE:Class;
private var EleXML:XML;
public function obtainXML():void {
var ba:ByteArray = new MyXMLFILE();
var st:String = ba.readUTFBytes(ba.length);
EleXML = new XML(st);
}
obtainXML();
var idlabel :Label = new Label();
idlabel.text = 'Informative information';
idlabel.format = new TextFormat( null, 14, 0xFFFFFF );
idlabel.x = 200;
idlabel.y = 20;
idlabel.width = idlabel.textWidth + 5;
idlabel.height = idlabel.textHeight + 5;
this.addChild( idlabel );
DisplayInfo();
private function DisplayInfo():void{
var EN:Label = new Label();
var ESTRING:String = new String( E1 );
var cSTRING:String = new String();
trace(EleXML.Element.(@symbol == ESTRING));
cSTRING = EleXML.Element.More.fact1.text();
EN.text = cSTRING;
EN.width = EN.textWidth + 5;
EN.x = 90;
EN.y = 90;
addChild(EN);
}
}
}
Sorry I forgot to add the xml...
<?xml version="1.0" encoding="utf-8"?>
<Element symbol="string">
<More>
<fact1>This is an element in an element</fact1>
<fact2>This is another element</fact2>
<fact3>This is fact</fact3>
</More>
</Element>
<Element symbol="notastring">
<More>
<fact1>This will not return true</fact1>
<fact2>This is not fact</fact2>
<fact3>It will come true if you search for @symbol == "notastring" </fact3>
</More>
</Element>
This should work perfectly with the above xml/as3 code.
03-17-2011 11:11 AM
Thanks tensioncore... for the sake of completeness..could you also post a sample xml file that goes with this code.
03-17-2011 12:00 PM
Tensioncore, kditty, and I did additional experimentation in the IRC chat (#playbook-dev on irc.freenode.net) and discovered a few related items.
For one thing, in spite of advice you can find on the web that mimeType of "text/xml" does something useful, it appears either to be unsupported, or broken. Certainly Adobe says it's not one of the select supported types.
Also, the ByteArray.readUTFBytes() approach doesn't appear to be the only way to do it. The following variation works fine as well:
[Embed('MyXML.xml', mimeType='application/octet-stream')]
private var MyXMLFILE:Class;
...
var EleXML:XML = XML(new MyXMLFILE());
Not that both approaches are creating (apparently.. the docs aren't exactly good on this) something called a ByteArrayAsset object when you do "new MyXMLFILE()", which supports pretty much all the ByteArray methods.
In this case, however, you're relying on the XML() call's ability to call toString() on the object passed (also undocumented, but it does take numerous types of input and converts them to strings to parse them). Note also that I'm using just "XML()" above instead of "new XML()"... there's no real difference in these cases (the function effectively calls new XML for you, and returns the result).
Both approaches appear to ignore any encoding="" attribute that you might have in your <?xml ?> tag, and treat it always as UTF-8 encoded data (at least in my experiments).
Lastly, although I've seen blogs saying that hardcoding the XML directly into your source code will compress it better than using Embed, my experiments show that in fact the data in the embedded XML above will be compressed quite efficiently, so you should have no worries about doing this and in fact, it's a rather nice way to get data like this into an app. I took one file with a line of 99 characters of text, and cloned the line 999 times (for a data size of about 10K). The resulting SWF grew only about 50 bytes... (I tried again with random data and it was 10K bigger, so that's conclusive.)
03-17-2011 03:42 PM
Thanks for helping me teach! It helps the learning!
tags07 wrote:Thanks tensioncore... for the sake of completeness..could you also post a sample xml file that goes with this code.