Drongo_III Posted July 11, 2012 Share Posted July 11, 2012 Hi guys I am trying to access an xml file from youtube that uses name spaces but when i try to access these elements I seem to get nothing back. As a simplified example: My XML <?xml version='1.0' standalone='yes'?> <movies xmlns:f="hppt://www.test.org"> <f:name>hello</f:name> </movies> I want to access the value of the f:name element so I do this: $xml = simplexml_load_file('myxml.xml'); echo $xml->{'f:name'}; But it doesn't echo anything to the page. So I must be doing something wrong but I'm a bit stuck on what that is... The only thing I am struggling with are these elements with namespaces (colons). Any help would be appreciated! Drongo Quote Link to comment Share on other sites More sharing options...
requinix Posted July 11, 2012 Share Posted July 11, 2012 You have to go through the children and attributes methods before you can access them. How those work is by returning to you a list of elements/attributes located in the new namespace. Or returning a copy of the original object with its "current namespace" changed. Whatever the mechanism the end result is basically the same. Anyways, code. echo $xml /* default namespace */ ->children("f", true) /* "hppt://www.test.org" namespace */ ->name; Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted July 11, 2012 Author Share Posted July 11, 2012 Stick with me on this I might be a little slow... I've done this: echo $xml->name->children("f", true)->name; Based on what i understand from what you said. But it returns an error: Warning: main() [function.main]: Node no longer exists in C:\wamp\www\xml\test What am i doing wrong? Quote Link to comment Share on other sites More sharing options...
Drongo_III Posted July 11, 2012 Author Share Posted July 11, 2012 Ah spotted my mistake. Should have been: echo $xml->children("f", true)->name; Now it displays correctly. Thank you for introducing me to that. Think i need to spend a little time practicing You have to go through the children and attributes methods before you can access them. How those work is by returning to you a list of elements/attributes located in the new namespace. Or returning a copy of the original object with its "current namespace" changed. Whatever the mechanism the end result is basically the same. Anyways, code. echo $xml /* default namespace */ ->children("f", true) /* "hppt://www.test.org" namespace */ ->name; Quote Link to comment 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.