I got a problem reading an XML-Feed Sample of the XML File:
<f:feed xmlns:f="http://www.bbgo.de/feedxml/feed" xmlns:a="http://www.bbgo.de/feedxml/article" xmlns="http://www.w3.org/1999/xhtml" f:customer="blub" f:name="dax-tecdax">
<f:title>Analysen: DAX, TecDAX</f:title>
<f:date>2010-09-22T20:46:29+02:00</f:date>
<f:url>http://www.bbgo.de/export/feed/stock-channel/dax-tecdax</f:url>
<f:articles>
<a:article a:id="2310446" a:status="published" a:payed-content="false" xml:lang="de-DE">
<a:title>Kabel Deutschland mit Verkaufsempfehlung</a:title>...
A simple test like:
$feed = new SimpleXMLElement($xml);
print_r($feed);
produces
SimpleXMLElement Object ( )
if i try
echo $xml;
the above sample is echoed correctly.
Why $feed array is not built? How can I access all <a:article>
in <f:articles>
?
How can I access all
<a:article>
in<f:articles>
?
To me simpleXML seems a bit clumsy when working with namespaces (but then again, I'm not a PHP coder). You could use the children
method but in my opinion using XPath is simpler.
$feed->registerXPathNamespace('f', 'http://www.bbgo.de/feedxml/feed');
$feed->registerXPathNamespace('a', 'http://www.bbgo.de/feedxml/article');
$article = feed->xpath("/f:feed/f:articles/a:article");
First you need to register the namespaces with some prefixes (prefixes don't need to match the ones used in the XML document but namespace URIs must match) and then use these prefixes in your XPath expression that selects your target. Without the element name prefixes the XPath expression would look for elements that are not in any namespace.
Why $feed array is not built?
It's hard to guess a reason for this. Some things you could test:
$xml
a well-formed xml document$feed = new SimpleXMLElement('<root><child/></root>');
simplexml_load_file($yourXMLfile);
or simplexml_load_string($yourXMLstring);