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='XXXXXXXXXXXXXXXX@gmail.com' 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
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++;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.