Cyrus Kafai Wu
Cyrus Kafai Wu

Reputation: 149

Reading XML Nodes with :

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);  

Upvotes: 2

Views: 338

Answers (2)

Francis Avila
Francis Avila

Reputation: 31621

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);

Upvotes: 1

user142162
user142162

Reputation:

Wrap the string in {}:

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

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

Upvotes: 2

Related Questions