【问题标题】:XML(in php) parsing on iPhone在 iPhone 上解析 XML(在 php 中)
【发布时间】:2011-12-29 11:35:02
【问题描述】:

我正在开发一个 iPhone 应用程序,我想将一些数据放入 UITableView。 我有一个应用程序显示从 here 运行的几个 xml 解析器。

然后,我将 GDataXMLParser 从这个项目中分离出来并让它运行,但是 我有一个奇怪的问题,我想不通。

这是放置php文件的代码。

- (void)start {
    self.startTimeReference = [NSDate timeIntervalSinceReferenceDate];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    self.parsedSongs = [NSMutableArray array];
    NSURL *url = [NSURL URLWithString:@"http://mydomain.blabla/phpxmltest.php"];
    [NSThread detachNewThreadSelector:@selector(downloadAndParse:) toTarget:self withObject:url];
}

当我直接用 echo 将 php 转换为 xml 时,像这样,

......
echo "<entry><title>this is the TEST</title><item>TEST</item></entry>";
......

iPhone 应用程序会像 XML 一样解析此代码。 但是当我使用 mySQL 查询将 php 转换为 xml 时(因为我想从 DB 中创建 xml 项),就像这样,

<?php
echo '<?xml version="1.0" encoding="UTF-8"?>';
$result = mysql_connect("localhost", "my ID", "my Password");
mysql_select_db("my DB");
$q = "select name, price, age, likeit, keyword from Table where category=101";
$result = mysql_query($q);
$num_rows = mysql_num_rows($result);

echo "<entry>\n";
for ($i=1; $i<=$num_rows; $i++) {
    $row = mysql_fetch_assoc($result);
    echo "<item>\n";
    echo "<title>" . $row["name"] . "</title>\n";
    echo "<category>" . $row["price"] . "</category>\n";
    echo "<artist>" . $row["age"] . "</artist>\n";
    echo "<album>" . $row["likeit"] . "</album>\n";
    echo "<releasedate>" . $row["keyword"] . "</releasedate>\n";
    echo "</item>\n";
}
echo "</entry>";
?>

iPhone 应用程序不解析此代码。它告诉我 XML 中没有项目。 对我来说最奇怪的是,Web Browser 上的结果完全一样。 当我将url放入浏览器时,输出本身和源(具有浏览器的查看源功能)完全相同。这是网络浏览器中的源视图。(请不要介意一些编码问题)

<?xml version="1.0" encoding="UTF-8"?>
<entry>
<item>
<title>������ ���ĺ� ������</title>
<category>11000</category>
<artist>3</artist>
<album>0</album>
<releasedate>���ĺ� ���߱�</releasedate>
</item>
<item>
<title>���ĺ� ��������</title>
<category>18000</category>
<artist>3</artist>
<album>0</album>
<releasedate>���ĺ� ����</releasedate>
</item>
…..

我努力让它发挥作用,但这对我来说太难了。我是 iOS 和 Web 编程的初学者。请让我知道是什么问题和解决方案。 提前谢谢你!:D

【问题讨论】:

  • 深入你的代码让我有些困惑,所以我最好向你展示一种在 iPhone 中快速简单地解析 XML 的方法。 Follow the link,它展示了一种简单的移动方式。希望对您有所帮助。
  • 感谢您的帮助,但我成功构建了 XML Parser。问题是当我尝试使用 php 将 xml 放入 iPhone XML Parser 时,它不起作用,尤其是在使用 php 函数从数据库中提取数据之后。无论如何,您的建议似乎很容易构建 XML Parser。
  • 目标 c 中的 php 函数不起作用。此代码也是关于如何将 xml 数据提取到目标 c 中。它与php或.net无关。
  • 这个downloadAndParse函数是什么?显示相同的代码。
  • 是的,我的 Xcode 项目也没有在 Objective C 中使用 php 函数。我只是放了 php 文件的 url 而不是 xml 文件。因为我不得不从 Web 数据库中提取一些数据并将其放入 xml。

标签: php iphone mysql xml xml-parsing


【解决方案1】:

(请不要介意一些编码问题)
也许我们不介意,但 xml 解析器可能会。
你应该
  • 在 http 响应头中设置 mimetype
  • 在 http 响应头中设置字符集(虽然它已经在 xml 声明中)
  • 将mysql的客户端encondig设置为utf8,以便接收utf-8编码的数据
  • 使用适当的转义函数处理数据库中的所有数据
  • 甚至更好地使用XMLWriter之类的东西

并且您还应该将错误消息打印为有效的 xml 文档,因为您告诉客户端它将接收 xml。

例如(仅由php -l 测试):

<?php
if ( headers_sent() ) {
  die("can't set mimetype and/or charset after output has been sent to the client");
}
ini_set('default_charset', 'utf-8');
ini_set('default_mimetype', 'text/xml');
// ini_set('default_mimetype', 'application/xml');

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

$mysql = mysql_connect("localhost", "my ID", "my Password");
if ( !$mysql ) {
    die('<error>database connection failed</error>');
}

if ( !mysql_select_db("my DB", $mysql) ) {
    die('<error>database selection failed</error>');
}

if ( !mysql_set_charset('utf8', $mysql) ) {
    die('<error>setting database selectiocharset failed</error>');
}

$q = 'SELECT name, price, age, likeit, keyword FROM Table WHERE category=101';
$result = mysql_query($q, $mysql);
if ( !$result ) {
    die('<error>database query failed</error>');
}


echo "<entry>\n";
while( false!=($row=mysql_fetch_assoc($result)) ) {
    echo '
        <item>
            <title>',  htmlspecialchars($row["name"], 'utf-8'), '</title>
            <category>', htmlspecialchars($row["price"], 'utf-8'), '</category>
            <artist>', htmlspecialchars($row["age"], 'utf-8'), '</artist>
            <album>', htmlspecialchars($row["likeit"], 'utf-8'), '"</album>
            <releasedate>',  htmlspecialchars($row["keyword"], 'utf-8'), '</releasedate>
        </item>';
}
echo "</entry>";
?>

【讨论】:

  • 天啊,我不知道如何感谢您的帮助。其实我对http响应头之类的东西只有一个模糊的想法,所以我想知道我是如何设置它们的。我添加了'ini_set',你让我知道,所以它现在可以工作了。非常感谢!:D,新年快乐!
猜你喜欢
  • 2011-01-20
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
相关资源
最近更新 更多