简体   繁体   中英

Flex/ActionScript, PHP, and XML Error

I have some XML that I am reading from using Flex/ActionScript. When I load the XML, Flash Builder gives me an error that says Error: Unknown Property: 'EventTitle'. at mx.collections::ListCollectionView/http://www.adobe.com/2006/actionscript/flash/proxy::getProperty()[E:\\dev\\4.y\\frameworks\\projects\\framework\\src\\mx\\collections\\ListCollectionView.as:870]. Error: Unknown Property: 'EventTitle'. at mx.collections::ListCollectionView/http://www.adobe.com/2006/actionscript/flash/proxy::getProperty()[E:\\dev\\4.y\\frameworks\\projects\\framework\\src\\mx\\collections\\ListCollectionView.as:870]. The only place that I am referencing 'EventTitle' is below where I am loading it into a list.

When I remove the <Invite> part from the XML, it loads fine. But once I add it in again it gives me the error. Below is the structure of the XML:

 <Invites>
      <Invite>
        <EventTitle>HelloWorld</EventTitle>
      </Invite>
      <Invite>
        <EventTitle>HelloWorld2</EventTitle>
      </Invite>
    </Invites>

I am using PHP to create the XML:

if ($_POST['method'] == "GET_INVITES") {
    $sql = mysql_connect("localhost:8889", "---------------", "--------------");
    if(!$sql) {
        die('Could not connect:' . mysql_error());
    }   

    mysql_select_db("Calendar", $sql);
    $getInvitesQuery = mysql_query("SELECT * FROM `Invites` WHERE `To`='" . $_POST['userID'] . "'");

    $result = "<Invites>";
    $result .= "<Result>SUCCESS</Result>";
    while($row = mysql_fetch_array($getInvitesQuery)){
        $result .= "<Invite>";
        $result .= "<FromID>" . $row['From'] . "</FromID>";
        $result .= "<EventTitle>" . $row['Title'] . "</EventTitle>";
        $result .= "<EventDay>" . $row['Day'] . "</EventDay>";
        $result .= "<EventHour>" . $row['Hour'] . "</EventHour>";
        $result .= "<EventMinute>" . $row['Minute'] . "</EventMinute>";
        $result .= "</Invite>";
    }
    $result .= "</Invites>";    
    print($result); 
}

I am using HTTPService in Flex/ActionScript to load the XML from the PHP file into a list. Here is the code:

<s:HTTPService id="getInvites" result="getInvitesResult(event)" method="POST" url="http://localhost/invite.php" useProxy="false">
            <s:request xmlns="">
                <method>GET_INVITES</method>
                <userID>{my_id.text}</userID>
            </s:request>
        </s:HTTPService>

<s:List id="invites" x="5" y="295" width="310" change="rowSelected(event)">
        <s:dataProvider>
            <s:ArrayCollection source="{getInvites.lastResult.Invites.Invite.EventTitle}"/>
        </s:dataProvider>
    </s:List>

I have been working and trying to figure out a solution for 2-3 hours. Any help is much appreciated.

Thanks, Jacob

I think there are several problems here:

Don't include the root tag of your XML object in e4x expressions

When using e4x expressions, you should not include the root tag of the XML. So try changing this line:

<s:ArrayCollection source="{getInvites.lastResult.Invites.Invite.EventTitle}"/>

To:

<s:ArrayCollection source="{getInvites.lastResult.Invite.EventTitle}"/>

As an alternative you could add another root tag to the XML so that your original expression would be valid:

<root>
    <Invites>
       ...
    </Inivtes>
</root>

Supply a proper dataProvider to the list

The e4x expression "Invite.EventTitle" (when used on your XML) will return an XMLList object. This is not suitable for using as the dataProvider to your list. The dataProvider property of the list wants something that implements the IList interface. Fortunately, all you need to do is create an XMLListCollection object. You're going to need to refactor your code so that you can create this XMLListCollection .

One way is to add result and fault handlers to your HTTPService . This is actually better than using the lastResult property. The reason is that when your app makes multiple calls to the service, using lastResult may not be referring to the correct result.

Inside the result event handler, you can create the new XMLListCollection and set it on the list:

<s:HTTPService id="getInvites" result="handleResult(event)" fault="handleFault(event)" resultFormat="e4x" ... />

private function handleResult(event:ResultEvent):void
{
    var xml:XML = event.result as XML;
    var xmlList:XMLList = xml.Invite.EventTitle; // an XML "array" of event titles
    var xmlCollection:XMLListCollection = new XMLListCollection(xmlList);
    // now set xmlCollection as the dataProvider for the list
    invites.dataProvider = xmlCollection;
}

Note in the above I've also specified the resultFormat for the HTTPService as "e4x".

Above is coded from memory, so there could be a mistake here or there ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM