gpt4 book ai didi

php - 使用 : 读取 XML 节点

转载 作者:行者123 更新时间:2023-12-04 06:04:34 24 4
gpt4 key购买 nike

我有一个供稿,但有一个 :在我要检索的 who's 值的项目中。我该怎么办?

饲料: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);

最佳答案

这就是所谓的 xml 命名空间的前缀。 See this namespace tutorial .

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

您如何执行此操作完全取决于您用于操作 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);

关于php - 使用 : 读取 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8513280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com