ngoweb Posted June 4, 2010 Share Posted June 4, 2010 I have an xml file i am trying to parse with simplexml <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"> <id>http://eventful.com/events/styx-and-foreigner-special-guest-kansas-/E0-001-029044011-4</id> <published>2010-03-10T08:05:18+00:00</published> <updated>2010-05-30T04:29:20+00:00</updated> <link href="http://eventful.com/events/styx-and-foreigner-special-guest-kansas-/E0-001-029044011-4" type="text/html" rel="alternate" /> <gd:eventStatus value="http://schemas.google.com/g/2005#event.confirmed" /> <gd:visibility value="http://schemas.google.com/g/2005#event.public" /> <gd:transparency value="http://schemas.google.com/g/2005#event.transparent" /> <title>Styx and Foreigner with special guest Kansas</title> <gd:when startTime="2010-06-07T18:30:00+00:00" endTime="2010-06-07T18:30:00+00:00" /> <gd:where rel="http://schemas.google.com/g/2005#event" valueString="Chastain Park Amphitheater near , Atlanta, Georgia, 30327, United States"> <gd:entryLink> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"> <id>http://eventful.com/venues/chastain-park-amphitheater-/V0-001-001274840-3</id> <link href="http://eventful.com/venues/chastain-park-amphitheater-/V0-001-001274840-3" type="text/html" rel="alternate" /> <title>Chastain Park Amphitheater</title> <gd:contactSection label="Chastain Park Amphitheater"> <gd:postalAddress> Atlanta, Georgia 30327, United States</gd:postalAddress> </gd:contactSection> The problem I am having is getting the start time of the event from this <gd:when startTime="2010-06-07T18:30:00+00:00" endTime="2010-06-07T18:30:00+00:00" /> Any clues how i can do this? Thanks all! Quote Link to comment https://forums.phpfreaks.com/topic/203896-simplexml-and-parsing-namespace-with-attributes/ Share on other sites More sharing options...
sKunKbad Posted June 4, 2010 Share Posted June 4, 2010 It's hard to tell without having the actual XML to play with, but from what you shared I'd start with something like this" <?php $the_xml = file_get_contents('http://the_file.xml'); $feed = new SimpleXMLElement($the_xml); $namespaces = $feed->getNamespaces(true); foreach ($feed->entry as $entry) { $event = $entry->children($namespaces['gd']); echo "<p>Event = " . $entry->title . "<br />startTime = " . $event->when->startTime . " and endTime = " . $event->when->endTime . "</p>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/203896-simplexml-and-parsing-namespace-with-attributes/#findComment-1068004 Share on other sites More sharing options...
sKunKbad Posted June 5, 2010 Share Posted June 5, 2010 Actually, I forgot something. You need to get an attribute. Consider this example: <?php $the_xml = <<<FEED <?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"> <id>http://eventful.com/atom/performers/bliminal-/P0-001-000192452-0</id> <link href="http://eventful.com/atom/performers/bliminal-/P0-001-000192452-0" type="application/atom+xml" rel="self" /> <link href="http://eventful.com/performers/bliminal-/P0-001-000192452-0" type="text/html" rel="alternate" /> <title>B-Liminal Tour Dates in 2009, B-Liminal Concert Schedule - Eventful</title> <subtitle>Up-to-date event feeds from eventful.com, the world's leading event website.</subtitle> <rights type="xhtml"> <div xmlns="http://www.w3.org/1999/xhtml"> Copyright © 2006 <a href="http://eventful.com/">Eventful, Inc.</a> All rights reserved. Use subject to terms of use: <a href="http://eventful.com/terms">http://eventful.com/terms</a> </div> </rights> <generator version="1.0">EVDB::Atom</generator> <opensearch:totalResults></opensearch:totalResults> <opensearch:startIndex>1</opensearch:startIndex> <updated>2009-09-13T11:48:52+00:00</updated> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"> <id>http://eventful.com/jupiter/events/bliminal-free-fan-appreciation-show-/E0-001-024704319-1</id> <published>2009-09-13T15:09:00+00:00</published> <updated>2009-09-13T15:48:12+00:00</updated> <link href="http://eventful.com/jupiter/events/bliminal-free-fan-appreciation-show-/E0-001-024704319-1" type="text/html" rel="alternate" /> <gd:eventStatus value="http://schemas.google.com/g/2005#event.confirmed" /> <gd:visibility value="http://schemas.google.com/g/2005#event.public" /> <gd:transparency value="http://schemas.google.com/g/2005#event.transparent" /> <title>B-Liminal FREE fan appreciation show!</title> <gd:when startTime="2009-09-19T02:00:00+00:00" endTime="2009-09-19T02:00:00+00:00" /> <georss:where> <gml:Point> <gml:pos>26.9342076 -80.0922588</gml:pos> </gml:Point> </georss:where> <gd:where rel="http://schemas.google.com/g/2005#event" valueString="Corners near 71 East Indiantown Road , Jupiter, Florida, 33458, United States"> <gd:entryLink> <entry xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" xmlns:media="http://search.yahoo.com/mrss/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"> <id>http://eventful.com/jupiter/venues/corners-/V0-001-001473018-9</id> <link href="http://eventful.com/jupiter/venues/corners-/V0-001-001473018-9" type="text/html" rel="alternate" /> <title>Corners</title> <georss:where> <gml:Point> <gml:pos>26.9342076 -80.0922588</gml:pos> </gml:Point> </georss:where> <gd:contactSection label="Corners"> <gd:postalAddress> 71 East Indiantown Road Jupiter, Florida 33458, United States </gd:postalAddress> <gd:geoPt lat="26.9342076" lon="-80.0922588" /> </gd:contactSection> <content type="html"> <div class='vcard'> <a class='fn org url uid' href='http://eventful.com/venues/V0-001-001473018-9'>Corners</a> <div class='adr'> <span class='street-address'>71 East Indiantown Road</span>, <span class='locality'>Jupiter</span>, <span class='region'>Florida</span> <span class='postal-code'>33458</span> <span class='country-name'>United States</span></div> </div> </content> <media:text type="html"> <div class='vcard'> <a class='fn org url uid' href='http://eventful.com/venues/V0-001-001473018-9'>Corners</a> <div class='adr'> <span class='street-address'>71 East Indiantown Road</span>, <span class='locality'>Jupiter</span>, <span class='region'>Florida</span> <span class='postal-code'>33458</span> <span class='country-name'>United States</span></div> </div> </media:text> <category scheme="http://eventful.com/venues/tags/" term="americantowns" label="American Towns" /> <category scheme="http://eventful.com/venues/tags/" term="americantownscom" label="americantowns.com" /> <category scheme="http://eventful.com/venues/tags/" term="artistdatacom" label="artistdata.com" /> <category scheme="http://eventful.com/venues/tags/" term="barnightclub" label="BarNight Club" /> <category scheme="http://eventful.com/venues/tags/" term="community" label="community" /> <category scheme="http://eventful.com/venues/tags/" term="local" label="local" /> <category scheme="http://eventful.com/venues/tags/" term="townsquare" label="town square" /> <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/g/2005#contact" /> <author> <name>evdb</name> <uri>http://eventful.com/users/evdb</uri> </author> </entry> </gd:entryLink> </gd:where> </entry> </feed> FEED; $feed = new SimpleXMLElement($the_xml); $namespaces = $feed->getNamespaces(true); foreach ($feed->entry as $entry) { $gd = $entry->children($namespaces['gd']); foreach($gd->when->attributes() as $k => $v) { $show_time[$k] = $v; } echo "<p>Event = " . $entry->title . "<br />startTime = " . $show_time['startTime'] . " and endTime = " . $show_time['endTime'] . "</p>\n"; } ?> That should be more than enough to show you what you need to do to get at an attribute of an element that is inside a namespace. Quote Link to comment https://forums.phpfreaks.com/topic/203896-simplexml-and-parsing-namespace-with-attributes/#findComment-1068011 Share on other sites More sharing options...
ngoweb Posted June 5, 2010 Author Share Posted June 5, 2010 Spot On! Thanks so much - that was driving me nuts! Quote Link to comment https://forums.phpfreaks.com/topic/203896-simplexml-and-parsing-namespace-with-attributes/#findComment-1068254 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.