This content has been marked as final. Show 7 replies
If you want to set it in-line:
var sURN:String = "whatever";
var xml:XML = ...
To find and set it later (assuming the "xml" variable is in scope)
xml.QueryStructureRequest .registry::URN = "whatever"
I think your problem is the namespace on the URN xml tag. Do you require that?
The cleanest way is to keep it in a separate file.
private var _url:String;
private var urlRequest:URLRequest;
private var urlLoader:URLLoader;
private var xmlData:XML;
private function init():void //Call the initializer from the application tag
_url = new String("Filelocation.xml");
urlRequest = new URLRequest(_url);
urlLoader = new URLLoader(urlRequest);
private function loadXML():void
xmlData = XML(urlLoader.data);
//To get an item from it.
var a:String = xmlData.child(0).Author; //Extract element 0 "Author" from xmlData
var t:String = xmlData.child(0).Title; //Extract element 0 "Title" from xmlData
Alert.show("The author of element 0 is: " + a); //Show "Author" string
Alert.show("The title of element 0 is: " + t); //Show "Title" string
/* To show all of the elements, do this
var allItems:String = new String("");
for(var c:uint = 0; c < xmlData.children().length(); c++)
allItems = allItems + xmlData.child(c).Author + " " + xmlData.child(c).Title + " ";
<?xml version="1.0" encoding="utf-8"?>
<Elements> (You must have a parent container tag)
<Element> (You must have child containers.)
<Title>A Brief History of Time</Title>
Note: If you receive some sort of 2148 or 2048 error, do the following below.
-Right-click on the project
-Click on properties
-Click on Flex Compiler
-In the additional compiler arguments box, type -use-network=false. Put a space between the first argument and the typed one or else the new argument will not be accepted.
Oops. It should be "private function loadXML(e:Event):void instead of "private function loadXML():void"
Christopher's advice is good if your xml is static.
My personal preference is for HTTPService over URLLoader. It is essentially the same, in fact it uses URLLoader internally, but is a bit easier to code, especially if you are more comfortable in mxml.
Well that sides all working, but unfortunately I do need the namespaces and they are making things more complicated that it needs to be, but thanks all works.
I was trying similar code and although I love Flex and think the results are great I am a bit disappointed by the lack of error reporting when trying to navigate complex xml. For someone new to pick up it’s a bit confusing when everything runs and it just does nothing when there's an error in xml path.
Also interesting to see that you can embed var’s in strings that should be very useful in the future.
Thanks for your responses.
"...embed var’s in strings ..." That only works in assigning "literal" xml to a variable.
When working with XML, if the path is not extremely simple, I usually build the path a step at a time, checking each step using trace(toXMLString())
var xmlTemp:XML = myXML.FirstPathStep;
xmlTemp = xmlTemp.NextPathStep;
Good advice I think I'll to do that.