如何在Flex中使用属性创建和访问xml

时间:2011-11-30 12:31:05

标签: php mysql xml

所以我的问题是,我正在尝试创建一个应用程序,它主要从MySQL数据库中检索数据,分为具有创建数据的特定日期的表。 现在,我有一个PHP脚本,它允许我从数据库中提取数据并将其放入XML文件中。问题是,我没有日期排序,这在应用程序中非常重要,但我不知道,我将如何创建一个脚本,从MySQL数据库中提取数据和将数据放入以下格式:

<allitems>
    <items date="29-11-2011">
        <item>
            <title>Something</title>
            <title2>Something else</title2>
        </item>
        <item>
            <title>Something2</title>
            <title2>Something else2</title2>
        </item>
        <item>
            <title>Something3</title>
            <title2>Something else3</title2>
        </item>
    </items>
</allitems>

虽然会有更多的组,每个组都有不同的日期属性,并且每个组都是从不同的表创建的,但所有这些数据都在一个XML文件中。

顺便说一句,这是我现在使用的PHP脚本:

$query = mysql_query("SELECT * FROM test_codes");
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<items>";

$fp = fopen($filename,"w");

for($x = 0 ; $x < mysql_num_rows($query) ; $x++){
    $row = mysql_fetch_assoc($query);

    $xml_output .= "\t<item>\n"; 
    $xml_output .= "\t\t<title>" . $row['title'] . "</title>\n";
    $xml_output .= "\t\t<code>" . $row['code'] . "</code>\n";
    $xml_output .= "\t\t<quantity>" . $row['quantity'] . "</quantity>\n"; 
    $xml_output .= "\t\t<price>" . intval($row['price'])*intval($row['quantity']) . "</price>\n"; 
    $xml_output .= "\t</item>\n";
}

$xml_output .= "</items>";

fwrite($fp,$xml_output);
fclose($fp);

这是一张图片,说明我的Flex应用程序(在列表中)中的数据使用情况: http://img855.imageshack.us/img855/9291/sofxml.png

2 个答案:

答案 0 :(得分:1)

假设您有一个php脚本从mySQL中提取数据并使用xml响应(就像您在问题中提到的那样)http://myserver.com/myphp.php?arg1=val1&arg2=val2

您将使用HTTPService来获取

private var service:HTTPService;

//Run this on application initialize
private function init() {
    service=new HTTPService();
    service.addEventListener(ResultEvent.RESULT, sResult);
    service.resultFormat="xml";
    service.url="http://myserver.com/myphp.php?arg1=val1&arg2=val2";
}

private function sResult(e:ResultEvent):void {
    var o:Object = xmlStringToObject((e.result as XMLNode).toString());
    //Now you have your xml as an object
    trace(o.allitems.items.getItemAt(0).date); //gives 29-11-2011
    trace(o.allitems.items.getItemAt(0).item.getItemAt(0)); //{title: Something, title2: Something else}
    //You get the pattern, if not, debug and watch o
}

public function xmlStringToObject(xmlStr:String):Object{
    var xmlDoc:XMLDocument = new XMLDocument(xmlStr);
    xmlDoc.ignoreWhite=true;
    var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
    var resultObj:Object = decoder.decodeXML(xmlDoc);
     return resultObj;
}

答案 1 :(得分:1)

// query ordered by date
$query = mysql_query("SELECT * FROM test_codes ORDER BY 'date' DESC");
$orderedResult = array();

// group the items by date
$queryResult = mysql_fetch_asoc($query);
foreach($queryResult as $row) {
  $orderedResult[$row['date']][] = $row;
}

// render output by date
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<allitems>";

$fp = fopen($filename,"w");

foreach($orderedResult as $key => $dateItems) {
  $xml_output .= '<items date="' . $key . '">';
  foreach($dateItems as $row) {
    $xml_output .= '<item>';
        $xml_output .= '<title>' . $row['title'] . '</title>';
        $xml_output .= '<code>' . $row['code'] . '</code>';
        $xml_output .= '<quantity>' . $row['quantity'] . '</quantity>';
    $xml_output .= '</item>'
  }
  $xml_output .= '</items>';
}

$xml_output .= "</allitems>";

fwrite($fp,$xml_output);
fclose($fp);