0

我正在尝试构建一种查询 GUI,它通过 httpservice 从 xml 格式的 php 脚本返回查询。每个查询返回不同的结果,例如。

  1. 代表最多和最少的引号
  2. 价值最高的商店

我真的很纠结如何显示查询和访问节点名称和值。这是一个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>

我想以可读的方式呈现数据。谢谢

4

2 回答 2

1

这里是读取 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 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>

注意:此代码是在 actionscript 3 中创建的,但也应该适用于您的版本。请尝试并告诉我是否有用,否则我将不得不为您的版本发布代码。请记住,有很多方法可以做到这一点,我发布这种方式是因为可能更简单。

你可以试试这个这里

请访问此链接以获取更多说明使用 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”语句迭代每个孩子。

于 2013-09-06T15:12:36.837 回答
1

这是一种在不知道节点名称或属性名称将是什么的情况下执行此操作的方法

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
    }
}
于 2013-09-07T00:18:12.840 回答