Actionscript 3 使用flex的动态XML
我正在尝试构建一种查询GUI,它通过httpservice以xml格式从php脚本返回查询。 每个查询返回不同的结果 例如Actionscript 3 使用flex的动态XML,actionscript-3,apache-flex,flex4,flash-builder4.5,Actionscript 3,Apache Flex,Flex4,Flash Builder4.5,我正在尝试构建一种查询GUI,它通过httpservice以xml格式从php脚本返回查询。 每个查询返回不同的结果 例如 用最短和最短的引号表示 价值最高的商店 我真的被困在如何显示查询和访问节点名称和值上了。 下面是一个xml示例: <node> <action>query</action> <quotes name="Most Quotes"> <first>John</first> <last>Smith
<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>
查询
约翰
史密斯
71
戴夫
烹调
6.
我想以可读的方式呈现数据。
感谢这里是读取XML的示例代码(它工作得非常好):
请访问此链接以了解更多说明
第二版
<?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>
检查一下,在这个新版本中,我使用带有增量变量的“for”语句迭代每个子级。这里有一种方法,可以在不知道节点名或属性名的情况下执行此操作
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
}
}
这太棒了,谢谢!我想知道,是否有一种方法可以获取字段名,例如“first”、“last”,而不是像您那样硬编码,因为其他查询返回不同的字段,所以“first”和“last”并不总是xml@mrcurious,请查看我的帖子,我编辑并添加了一个更通用的新代码,请告诉我是否有用。不要忘记节点引号是读取其子节点的起点,在这种情况下,必须对其进行harcoded(可能也可以使用索引)。
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
}
}