Jump to content

SimpleXML Namespace problem


AlexGrim

Recommended Posts

The problem is that i don't know how to access elements that have namespace prefixes. If you see the last element in this node, it uses a namespace "gd", and i'm used to accessing the elment name, but the namespace is throwing it off...

 

Here's the XML:

<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gContact='http://schemas.google.com/contact/2008' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'>
..................
<entry>
	<id>http://www.google.com/m8/feeds/contacts/XXXXXXXXXXXXXXXX/base/0</id>
	<updated>2008-10-21T20:26:36.075Z</updated>
	<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact'/>
	<title type='text'>Some Guy</title>
	<link rel='http://schemas.google.com/contacts/2008/rel#edit-photo' type='image/*' href='http://www.google.com/m8/feeds/photos/media/XXXXXXXXXXXXXXXX/0/XXXXXXXXXXXXXXXX'/>
	<link rel='self' type='application/atom+xml' href='http://www.google.com/m8/feeds/contacts/XXXXXXXXXXXXXXXX/full/0'/>
	<link rel='edit' type='application/atom+xml' href='http://www.google.com/m8/feeds/contacts/XXXXXXXXXXXXXXXX/full/0/XXXXXXXXXXXXXXXX'/>
	<gd:email rel='http://schemas.google.com/g/2005#other' address='[email protected]' primary='true'/>
</entry>
</feed>

 

 

and here's the php:

$xml = simplexml_load_string($xml);
$a[] = array();
$n=0;

foreach ($xml->entry as $entry) {
$a[$n][0] = $entry->email['address'];
$a[$n][1] = $entry->title;
$n++;
}

 

 

Thanx

Link to comment
https://forums.phpfreaks.com/topic/176226-simplexml-namespace-problem/
Share on other sites

Without testing it out, something like:

$xml = simplexml_load_string($xml);
$namespaces = $xml->getNamespaces(true);

foreach ($xml->entry as $entry) {
$a[$n][0] = $entry->email['address'];
$a[$n][1] = $entry->title;
$a[$n][2] = $entry->title;

$entryGD = $entry->children($namespaces['gd']);
$entryGDAttributes = $entryGD->attributes();
$a[$n][3] = $entryGDAttributes['address'];

$n++;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.