Jump to content

xml - simple question


Drongo_III

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/265546-xml-simple-question/
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/265546-xml-simple-question/#findComment-1360932
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/265546-xml-simple-question/#findComment-1360939
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/265546-xml-simple-question/#findComment-1360940
Share on other sites

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.