FLEX:无法从event.result检索XML

时间:2010-11-20 02:16:53

标签: xml flex casting null

我正在尝试将数据从SQL数据库提供给FLEX,中间使用php脚本。我在网络监视器和调试期间的“变量”窗口中看到消息正文中的数据,但我无法将它们检索到XML。

下面(event.message.toString())

(mx.messaging.messages::AcknowledgeMessage)#0
  body = "<?xml version="1.0" encoding="UTF-8"?>
<articles>
<node><id>2</id>
<articleid>2</articleid>
<nrporzadkowy>2</nrporzadkowy>
<tresc>moja tresc</tresc>
<rodzaj>textFlow</rodzaj>
</node>
</articles>"
  clientId = "DirectHTTPChannel0"
  correlationId = "C8993E66-DF60-FE63-73D3-6700CA497221"
  destination = ""
  headers = (Object)#1
    DSStatusCode = 200
  messageId = "475F2475-A915-29AB-4364-6700D08BD7D2"
  timestamp = 0
  timeToLive = 0

我正在尝试

protected function pobieranieElementow_resultHandler(event:ResultEvent):void
{
    var myXML2:XML = new XML();
    myXML2 =XML(event.result);
}

然后在myXML2中我只得到“[object Object]”,当我将转换方法更改为:

protected function pobieranieElementow_resultHandler(event:ResultEvent):void
    {
        var myXML2:XML = new XML();
        myXML2 =event.result as XML;
    }

我在myXML2中收到null。我不知道是怎么回事 ?我也会发布php脚本以防万一有什么问题(可能是错误的'\ n'标志)

<?php
  header('Content-type: text/xml; charset=utf-8');
  echo '<?xml version="1.0" encoding="utf-8"?>';
  echo "\r\n";
  $link = mysql_connect("HOST", "XXXXX", "XXXXDS");
  mysql_select_db("DATABASEXXXX");
  if (!$link) {
  printf("Connect failed: %s\n", mysql_connect_error());
  exit();
}
  $sql = sprintf("SELECT id,articleid,nrporzadkowy,tresc,rodzaj from elements where articleid = '%s'",$_POST["id"]);
  $result = mysql_query($sql);
  $beg = '<articles>';
  $end = '</articles>';
  echo "$beg\r\n";
  while($row = mysql_fetch_assoc($result))
  {
   echo '<node>';
   echo '<id>';
   echo $row["id"];
   echo '</id>';
   echo "\r\n";
   echo '<articleid>';
   echo $row["articleid"];
   echo '</articleid>';
   echo "\r\n";
   echo '<nrporzadkowy>';
   echo $row["nrporzadkowy"];
   echo '</nrporzadkowy>';
   echo "\r\n";
   echo '<tresc>';
   echo $row["tresc"];
   echo '</tresc>';
   echo "\r\n";
   echo '<rodzaj>';
   echo $row["rodzaj"];
   echo '</rodzaj>';
   echo "\r\n";  
   echo '</node>';
   echo "\r\n";
  }
  echo $end;
  mysql_free_result($result);
  mysql_close($link);
?>

PS 好吧,我可以通过以下方式将节点更改为arrayCollection:

var array:ArrayCollection = event.result.articles.node;

但我无法以这种方式提供dataGrid。

4 个答案:

答案 0 :(得分:1)

好的,错误是愚蠢的。我没有在httpService中指定resultFormat我将其保留为默认值,并且要读取xml我应该将其设置为“e4x”

<mx:HTTPService id="pobieranieElementow" 
                        url="URL" 
                        useProxy="false" 
                        method="POST"
                        result="pobieranieElementow_resultHandler(event)"
                        fault="pobieranie_faultHandler(event)"
                        {this was not set->}**resultFormat="e4x"**>

现在XML(event.result)正常运行。

答案 1 :(得分:1)

<mx:HTTPService id="getDataUser" 
                url="http://localhost/test/db.php" 
                resultFormat="e4x" useProxy="false"
                result="onResultData(event)" 
                fault="onFaultData(event)">     
</mx:HTTPService>

得到错误:

(mx.messaging.messages::AcknowledgeMessage)#0  
  body = "<user>abc</user>"
  clientId = "DirectHTTPChannel0"
  correlationId = "147F8A12-7055-5FD4-0584-0B8A07850826"  
  destination = ""
  headers = (Object)#1
    DSStatusCode = 200  
  messageId = "82772E32-1D8A-4A73-C5C8-0B8A07C30EE0"
  timestamp = 0
  timeToLive = 0

答案 2 :(得分:0)

我会尝试(并捕获)将event.result传递给构造函数而不是使用“as”进行转换。这样,您将从字符串中创建一个新的XML对象,而不是创建一个空白文档,然后将其转换为XML。希望有效。

答案 3 :(得分:0)

我认为你不需要

<?xml version="1.0" encoding="UTF-8"?>

线。