使用以下命令读取 XML 节点:

发布于 2024-12-21 01:55:47 字数 526 浏览 1 评论 0原文

我有一个提要,但我想要检索的值的项目中有一个 : 。我该怎么办?

供稿:http://publishers.spilgames .com/rss?lang=en-US&tsize=1&format=xml&limit=100

foreach($xml->entry as $game) {     
    $it = $it+1;
    $name = mysql_real_escape_string($game->title);         
    $link = $game->link[href];
    $description = mysql_real_escape_string($game->media:description);  

I have a feed but there's a : in the item of who's value I want to retrieve. What do I do?

Feed: http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100

foreach($xml->entry as $game) {     
    $it = $it+1;
    $name = mysql_real_escape_string($game->title);         
    $link = $game->link[href];
    $description = mysql_real_escape_string($game->media:description);  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你又不是我 2024-12-28 01:55:47

将字符串包裹在 {} 中:

$description = mysql_real_escape_string($game->{'media:description'});

请参阅:字符串:复杂(卷曲)语法可变变量

Wrap the string in {}:

$description = mysql_real_escape_string($game->{'media:description'});

See: Strings: Complex (curly) syntax and Variable variables

魔法少女 2024-12-28 01:55:47

这就是所谓的 xml 命名空间的前缀。 查看此命名空间教程

您实际上并不匹配前缀,而是匹配前缀代表的命名空间。

如何执行此操作完全取决于您用来操作 xml 的内容。你没有说你正在使用什么,但我猜你正在使用 SimpleXML。

对于 SimpleXML,默认情况下只有没有命名空间的节点才会包含在对象访问树中。要获取命名空间元素,您需要显式地请求它们:

$xml=simplexml_load_file('http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100');

foreach($xml->entry as $game) {
    $description = (string) $game->children('http://search.yahoo.com/mrss/')->description;
    var_dump($description);
}

虽然在这种特殊情况下它可能不是最佳选择,但您也可以使用 XPath 更直接地匹配命名空间节点:

$xml=simplexml_load_file('http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100');

$NS = array(
    'media' => 'http://search.yahoo.com/mrss/',
);
foreach ($NS as $prefix => $uri) {
    $xml->registerXPathNamespace($prefix, $uri);
}

foreach($xml->entry as $entry) {
    // match the first media:description element
    // get the first SimpleXMLElement in the match array with current()
    // then coerce to string.
    $description = (string) current($entry->xpath('media:description[1]'));
    var_dump($description);
}

这是一个更完整的示例,它也稍微修饰了您的代码。

$xml=simplexml_load_file('http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100');

// This gets all the namespaces declared in the root element
// using the prefix as declared in the document, for convenience.
// Note that prefixes are arbitrary! So unless you're confident they
// won't change you should not use this shortcut
$NS = $xml->getDocNamespaces();

$games = array();
foreach($xml->entry as $entry) {
    $mediaentry = $entry->children($NS['media']);
    $games[] = array(
        // to get the text value of an element in SimpleXML, you need
        // explicit cast to string
        'name' => (string) $entry->title,
        // DO NOT EVER use array-access brackets [] without quoting the string in them!
        // I.e., don't do "$array[name]", do "$array['name']"
        // This is a PHP error that happens to work.
        // PHP looks for a *CONSTANT* named HREF, and replaces it with
        // string 'href' if it doesn't find one. This means your code will break
        // if define('href') is ever used!!
        'link' => (string) $entry->link['href'],
        'description' => (string) $mediaentry->description,
    );
}
$it = count($games); // there is no need for your $it+1 counter!

// $games now has all your data.
// If you want to insert into a database, use PDO if possible and prepare a query
// so you don't need a separate escaping step.
// If you can't use PDO then do:
// $escapedgame = array_map('mysql_real_escape_string', $thegame);

This is what is called a prefix for an xml namespace. See this namespace tutorial.

You don't actually match on the prefix, you match on the namespace the prefix stands for.

How you do this is entirely dependent on what you are using to manipulate the xml. You don't say what you are using, but I will guess you are using SimpleXML.

With SimpleXML, by default only nodes with no namespace are included in the object-access tree. To get namespaced elements, you need to explicitly ask for them:

$xml=simplexml_load_file('http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100');

foreach($xml->entry as $game) {
    $description = (string) $game->children('http://search.yahoo.com/mrss/')->description;
    var_dump($description);
}

Although it's probably not the best choice in this particular case, you can also use XPath to match namespaced nodes more directly:

$xml=simplexml_load_file('http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100');

$NS = array(
    'media' => 'http://search.yahoo.com/mrss/',
);
foreach ($NS as $prefix => $uri) {
    $xml->registerXPathNamespace($prefix, $uri);
}

foreach($xml->entry as $entry) {
    // match the first media:description element
    // get the first SimpleXMLElement in the match array with current()
    // then coerce to string.
    $description = (string) current($entry->xpath('media:description[1]'));
    var_dump($description);
}

Here's a more complete example which also spruces up your code a bit.

$xml=simplexml_load_file('http://publishers.spilgames.com/rss?lang=en-US&tsize=1&format=xml&limit=100');

// This gets all the namespaces declared in the root element
// using the prefix as declared in the document, for convenience.
// Note that prefixes are arbitrary! So unless you're confident they
// won't change you should not use this shortcut
$NS = $xml->getDocNamespaces();

$games = array();
foreach($xml->entry as $entry) {
    $mediaentry = $entry->children($NS['media']);
    $games[] = array(
        // to get the text value of an element in SimpleXML, you need
        // explicit cast to string
        'name' => (string) $entry->title,
        // DO NOT EVER use array-access brackets [] without quoting the string in them!
        // I.e., don't do "$array[name]", do "$array['name']"
        // This is a PHP error that happens to work.
        // PHP looks for a *CONSTANT* named HREF, and replaces it with
        // string 'href' if it doesn't find one. This means your code will break
        // if define('href') is ever used!!
        'link' => (string) $entry->link['href'],
        'description' => (string) $mediaentry->description,
    );
}
$it = count($games); // there is no need for your $it+1 counter!

// $games now has all your data.
// If you want to insert into a database, use PDO if possible and prepare a query
// so you don't need a separate escaping step.
// If you can't use PDO then do:
// $escapedgame = array_map('mysql_real_escape_string', $thegame);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文