Reputation: 83
I'm trying to build a type of query GUI, which returns queries from a php script in xml format via an httpservice. Each query returns different results eg.
I'm really stuck on how to display the queries and access the node names and values. Here's an example of an xml:
<node>
<action>query</action>
<quotes name="Most Quotes">
<first>John</first>
<last>Smith</last>
<quote_num>71</quote_num>
</quotes>
<quotes name="Least Quotes">
<first>Dave</first>
<last>Cook</last>
<quote_num>6</quote_num>
</quotes>
</node>
Id like to present the data in a readable way. Thanks
Upvotes: 0
Views: 313
Reputation: 174
Here's a way to do it without knowing what the node names or attribute names will be
for each(var item : XML in yourXML.children()) {
trace(item.name());//this will get the name of the node
for each(var attribute : XML in item.attributes()) {
trace(attribute.name() + " = " + attribute.toXMLString()); // prints out the attribute names and values
}
}
Upvotes: 1
Reputation: 2467
Here the example code for read your XML (it works perfectly):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" horizontalAlign="center">
<mx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
import mx.utils.ObjectUtil;
private var tempXML:XML;
public function init():void{
tempXML = myXML;
txtA.text = myXML.toString();
readXml();
}
public function readXml():void{
var str:String = "";
var myXML:XMLList = new XMLList(myXML);
for each(var node:XML in myXML){
str = str + "action:" + node["action"] + "\n";
for each(var obj2:XML in node.quotes){
str = str + " name:" + obj2.attributes().toXMLString() + "\n";
str = str + " first:" + obj2["first"] + "\n";
str = str + " first:" + obj2["last"] + "\n";
str = str + " quote_num:" + obj2["quote_num"] + "\n";
}
txtB.text = str;
}
}
]]>
</mx:Script>
<mx:XML id="myXML">
<node>
<action>query</action>
<quotes name="Most Quotes">
<first>John</first>
<last>Smith</last>
<quote_num>71</quote_num>
</quotes>
<quotes name="Least Quotes">
<first>Dave</first>
<last>Cook</last>
<quote_num>6</quote_num>
</quotes>
</node>
</mx:XML>
<mx:HBox width="100%">
<mx:TextArea id="txtA" width="400" height="400" />
<mx:TextArea id="txtB" width="400" height="400" />
</mx:HBox>
Note: This code was created in actionscript 3, but should also work in your version. Please try it and tell me if will be useful, otherwise I'll have to post a code for your version. Remember that there are many ways to do this, i posted this way because maybe is more simple.
You can try this HERE.
Please visit this link for more explanation Working with XML
Second Version
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" horizontalAlign="center">
<mx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
import mx.utils.ObjectUtil;
private var tempXML:XML;
public function init():void{
tempXML = myXML;
txtA.text = myXML.toString();
readXml();
}
public function readXml():void{
var str:String = "";
var quotes:XMLList = myXML.quotes;
str = str + "action:" + myXML["action"] + "\n";
for (var i:int = 0; i < quotes.length(); i++){
str = str + "----quote name:" + XMLList(quotes[i]).attributes().toXMLString() + "\n";
var quotes_child:XMLList = quotes[i].children();
for (var j:int = 0; j < quotes_child.length(); j++){
str = str + "--------" + XML(quotes_child[j]).name() + ":" + quotes_child[j] + "\n";
}
}
txtB.text = str;
}
]]>
</mx:Script>
<mx:XML id="myXML">
<node>
<action>query</action>
<quotes name="Most Quotes">
<first>John</first>
<last>Smith</last>
<quote_num>71</quote_num>
</quotes>
<quotes name="Least Quotes">
<first>Dave</first>
<last>Cook</last>
<quote_num>6</quote_num>
</quotes>
<quotes name="other">
<first>other_first</first>
<last>other_last</last>
<quote_num>other_num</quote_num>
<other_property>other_prop</other_property>
</quotes>
</node>
</mx:XML>
<mx:HBox width="100%">
<mx:TextArea id="txtA" width="400" height="400" />
<mx:TextArea id="txtB" width="400" height="400" />
</mx:HBox>
You can try this HERE.
Check that in this new version, I iterate over each child using a "for" statement with increment variable.
Upvotes: 1