0

httpservice を介して xml 形式で php スクリプトからクエリを返すクエリ GUI のタイプを構築しようとしています。各クエリは異なる結果を返します。

  1. ほとんどの引用符と leat 引用符を持つ担当者
  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の操作

2 番目のバージョン

<?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 に答える