IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Flex Discussion :

Import XML depuis un controller PHP


Sujet :

Flex

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Avril 2009
    Messages : 29
    Par défaut Import XML depuis un controller PHP
    Bonjour
    Je voudrais prendre le XML généré par ce controller PHP et le placer dans un tree.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
    <?php
        function build(&$xml, $parent)
        {
            $sql = 'SELECT idpanel AS id, belongTo AS parent, mname AS name, `group` AS `type` '
                 . 'FROM atactionpanel '
                 . 'WHERE belongTo ' . $parent ;
            $rs = mysql_query($sql) ;
            while ( $tuple = mysql_fetch_assoc($rs) ) {
                if ( $tuple['type'] == 't' ) {
                    $groupe = $xml->addChild('groupe') ;
                    $groupe->addAttribute('id', $tuple['id']) ;
                    $groupe->addAttribute('name', $tuple['name']) ;
                    build($groupe, '= ' . $tuple['id']) ;
                } elseif ( $tuple['type'] == 'f' ) {
                    $pannel = $xml->addChild('pannel') ;
                    $pannel->addAttribute('id', $tuple['id']) ;
                    $pannel->addAttribute('name', $tuple['name']) ;
                } else {
                    // Elément indéterminé !!
                }
            }
        }
     
        mysql_connect('localhost', 'root', 'root') ;
        mysql_select_db('APIM') ;
     
        $root = new SimpleXMLElement('<list/>') ;
        build($root, 'IS NULL') ;
     
        header('Content-Type: text/xml') ;
        echo $root->asXML( ) ;
    ?>
    Qui me génère ce XML :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
     
    <list>
    −
    <groupe id="0" name="APIM">
    <pannel id="1" name="Email"/>
    <pannel id="2" name="Print"/>
    <pannel id="3" name="File"/>
    <pannel id="4" name="Http/s"/>
    <pannel id="5" name="FTP"/>
    </groupe>
    <pannel id="6" name="Scripting"/>
    <pannel id="8" name="User Def"/>
    −
    <groupe id="9" name="RDBMS">
    <pannel id="10" name="ETL"/>
    </groupe>
    −
    <groupe id="12" name="Middleware">
    <pannel id="13" name="Corba"/>
    <pannel id="14" name="JMS"/>
    </groupe>
    −
    <groupe id="15" name="Directories">
    <pannel id="16" name="LDAP"/>
    </groupe>
    −
    <groupe id="17" name="Operating System">
    <pannel id="18" name="Ms Windows"/>
    −
    <groupe id="19" name="Unix">
    <pannel id="20" name="SUN Solaris 9"/>
    <pannel id="21" name="IBM AIX 5"/>
    <pannel id="22" name="Redhat Linux 9"/>
    <pannel id="23" name="Mandrake Linux 9.1"/>
    </groupe>
    </groupe>
    −
    <groupe id="24" name="EDM">
    <pannel id="25" name="Documentum 4"/>
    </groupe>
    −
    <groupe id="26" name="Workgroup">
    <pannel id="27" name="Lotus Domino 6"/>
    <pannel id="28" name="Microsoft Outlook"/>
    </groupe>
    −
    <groupe id="53" name="Legacy">
    −
    <groupe id="54" name="SAGE">
    <pannel id="55" name="SAGE 50"/>
    </groupe>
    </groupe>
    <pannel id="56" name="&nbsp;&nbsp;&nbsp;EDI"/>
    <pannel id="57" name="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ANSI X12"/>
    <pannel id="58" name="&nbsp;&nbsp;&nbsp;xCBL 4"/>
    <pannel id="59" name="&nbsp;&nbsp;&nbsp;Oracle Financials"/>
    </list>
    Pour le moment j'ai entré le XML en dur directement dans le FLEX. Mais je voudrais que à chaque démarrage de mon application, sa appel le controller pour mettre a jour mon XML.

    Voici mon code MXML :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
     
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="src/index.html">
     
        <mx:Script>
            <![CDATA[
    	        private var dragSmallBox:Canvas;
    	        private var dragArea:Rectangle;
    	        private var draggableCanvas:Canvas = new Canvas();
     
                private function rollOutHandler(event:MouseEvent):void {
                    // start dragging the canvas, limit the drag inside the stage
                    draggableCanvas.startDrag(false, dragArea);
     
                    // relatedObject is the object where the mouse went after the rollOut event
                    event.relatedObject.addEventListener(
                    	MouseEvent.MOUSE_UP, 
                    	function():void {
    	                    draggableCanvas.stopDrag();
     
    	                    // we need to remove the rollOut event handler to avoid calling it
    	                    // when trying to roll out when mouseUp was already handled
    	                    dragSmallBox.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
    	                }
                    );
     
                };
     
                private function mouseDownHandler(event:MouseEvent, da:Canvas = null):void {
     
                    if(da == null)
                    {
                    	dragArea = new Rectangle(0, 0,
                    		stage.width-draggableCanvas.width,stage.height-draggableCanvas.height)
                    }
                    else
                    {
                    	dragArea = getCanvasRect(da);
                    	dragArea.width = dragArea.width - draggableCanvas.width;
    					dragArea.height = dragArea.height - draggableCanvas.height;
                    }
     
                	dragSmallBox = new Canvas();
                	dragSmallBox.width = 15;
                    dragSmallBox.height = 15;
     
                    // comment out the following to hide the canvas
                    // smallBox.setStyle("backgroundColor", 0xff0000);
                    dragSmallBox.addEventListener(
                    	MouseEvent.MOUSE_UP, 
                    	function():void {
                        	draggableCanvas.stopDrag();
                        	dragSmallBox.removeEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
                    	}
                    );
     
                    draggableCanvas = Canvas(event.currentTarget);
                    draggableCanvas.addChild(dragSmallBox);
     
                    // move the smallBox to location where the current mouse pointer is its center
                    dragSmallBox.move(
                    	event.currentTarget.mouseX - (dragSmallBox.width / 2),
                        event.currentTarget.mouseY - (dragSmallBox.height / 2)
                    );
     
                    dragSmallBox.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
                }
     
                private function getCanvasRect(c:Canvas):Rectangle{
                	return new Rectangle(0, 0, c.width, c.height);
                }
     
            ]]>
        </mx:Script>
     
        <mx:Script>
            <![CDATA[
     
                import mx.collections.XMLListCollection;
     
                [Bindable]
                private var company:XML =
     
                  <list>
    				<groupe name="APIM" id="0">
    					<pannel name="Email" id="1"/>
    					<pannel name="Print" id="2"/>
    					<pannel name="File" id="3"/>
    					<pannel name="Http/s" id="4"/>
    					<pannel name="FTP" id="5"/>
    				</groupe>
     
    				<pannel name="Scripting" id="6"/>
    				<pannel name="User Def" id="7"/>
     
             	</list>;
     
                [Bindable]
     
                private var companyData:XMLListCollection = new XMLListCollection(company.groupe);
     
                private function treeLabel(item:Object):String
                {
     
                    var node:XML = XML(item);
                    if( node.localName() == "groupe" )
     
                        return node.@name;
                    else
                        return node.@name;
                }
     
            ]]>
        </mx:Script>
     
     
     
    <mx:Canvas mouseDown="mouseDownHandler(event)" x="10" y="10" height="450" width="200" backgroundColor="red">
    <mx:Tree id="tree" dataProvider="{companyData}"
           labelFunction="treeLabel"
           height="450" width="200"
           />
    </mx:Canvas>
    </mx:Application>

  2. #2
    Membre Expert

    Profil pro
    Inscrit en
    Mai 2006
    Messages
    895
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 895
    Par défaut
    Salut,

    Pour appeler du php côté Flex, voici la doc : http://www.adobe.com/devnet/flex/art...tarted_05.html

    Pour appeler au moment de la creation de composant, tu peux appeler ton HTTPService (cf doc) comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" viewSourceURL="src/index.html"
    creationComplete="monHttpService.send();">
    L'évènement creationComplete permet d'intercepter la fin de la création de tes composants Flex, c'est un bon moment pour envoyer ta requête HTTP.

    ++

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Avril 2009
    Messages : 29
    Par défaut
    Après quelques recherche voila ce que ça donne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#ffffff" creationComplete="form1.send()">
     
       <mx:String id="phpFile">http://localhost:8888/CSR/index.php</mx:String>
       <mx:HTTPService id="form1" url="{phpFile}"
         method="POST" resultFormat="e4x"
         result="{tree.dataProvider = event.result as XML}">
       </mx:HTTPService>
     
     <mx:Tree id="tree" height="450" width="200"/>
       </mx:Application>
    Mon seul problème c'est le Tree prend les donnée comme un string et non pas comme un XML. Ce qui me donne un Tree avec 1 groupe et tout mon XML en label. Comment faire pour que le tree prenne les donnée en tant que XML?
    Merci d'avance

  4. #4
    Membre Expert

    Profil pro
    Inscrit en
    Mai 2006
    Messages
    895
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2006
    Messages : 895
    Par défaut
    Toujours dans la doc officielle, on t'explique le fonctionnement d'un tree :
    http://livedocs.adobe.com/flex/2/lan...xamplesSummary

    Il faut utiliser le champs labelField. Il s'agit du même exemple que dans le Flex Component Explorer à mettre dans tes favoris pour l'avoir toujours sous la main.

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Avril 2009
    Messages
    29
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations forums :
    Inscription : Avril 2009
    Messages : 29
    Par défaut
    Merci encore

    hop dans les favoris

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AS3] afficher du xml depuis une page php
    Par gunsailor dans le forum ActionScript 3
    Réponses: 6
    Dernier message: 13/06/2012, 15h05
  2. [MySQL] Controller PHP mySQL -> XML
    Par Gealion dans le forum PHP & Base de données
    Réponses: 6
    Dernier message: 29/04/2009, 14h35
  3. Réponses: 2
    Dernier message: 04/10/2007, 11h04
  4. Transformation xml + xsl -> HTML via PHP
    Par petit-ourson dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 19/10/2003, 22h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo