Samuz Posted March 2, 2012 Share Posted March 2, 2012 Hi there hoping someone can help me with this little problem I can't seem to get around. Okay I have a large XML file with quite abit of information. It looks something like this: <nationlist> <nation data="Kanden"> <nationid>254470</nationid> <ruler>Samus</ruler> <nation_name>Kanden</nation_name> <resource1>Cow</resource1> <resource2>Water</resource2> <alliance>Independent Republic Of Orange Nations</alliance> </nation> <nation data="Bubbler Nation"> <nationid>13778</nationid> <ruler>Matt Miller</ruler> <nation_name>Bubbler Nation</nation_name> <resource1>Aluminum</resource1> <resource2>Lumber</resource2> <alliance>Independent Republic Of Orange Nations</alliance> </nation> </nationlist> In my PHP file, I grab the content based on a $GET request. $simplexml = simplexml_load_file('test.xml'); $myDataObjects = $simplexml->xpath('//nation[@data="'.$_GET['nation'].'"]'); So if I had 'Kanden' in the GET request, it'll return information for Kanden in an array. Abit like this: Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [data] => Kanden ) [nationid] => 254470 [ruler] => Samus [nation_name] => Kanden [resource1] => Cow [resource2] => Water [alliance] => Independent Republic Of Orange Nations ) ) Now my problem, how would I go about retrieving specific values from the array, like if I wanted to retrieve the 'ruler' index, what would I do to get 'Samus'. And also how would I make it possible so that other people without server access, can grab data from this page and use it in their own scripts? Sorry for the wall of text! Just wanted to explain it properly. Regards Quote Link to comment https://forums.phpfreaks.com/topic/258130-phpxml-help/ Share on other sites More sharing options...
codebyren Posted March 2, 2012 Share Posted March 2, 2012 Since the xpath method of the SimpleXMLElement returns an array of objects (which is good in case there's more than one match for the xpath provided), you'll need to iterate over the results or at least determine how many there are first: <?php if ( ! $myDataOjects) die('No results found.'); if (count($myDataOjects) > 1) { // There's more than one result with data=Kanden so we "foreach it". foreach($myDataOjects as $obj) { echo "The ruler of this element is {$obj->ruler}"; } } else { // Only one result so we can access it by the '0' key of the result array echo "The ruler of Kanden is {$myDataObjects[0]->ruler}"; } ?> And also how would I make it possible so that other people without server access, can grab data from this page and use it in their own scripts? I don't really understand the question. Where does this XML file reside? And are you processing it on a different server? And which server do "other people" not have access to? Quote Link to comment https://forums.phpfreaks.com/topic/258130-phpxml-help/#findComment-1323197 Share on other sites More sharing options...
silkfire Posted March 2, 2012 Share Posted March 2, 2012 1. As you see, SimpleXML XPath query returns you an array. In this case that array will always have one element, the SimpleXML object. SO you can do like this to retrieve a specific element: $element = array_pop($myDataObjects)->element; 2. You can echo out the XML of your node: $node->asXML(); Quote Link to comment https://forums.phpfreaks.com/topic/258130-phpxml-help/#findComment-1323198 Share on other sites More sharing options...
Samuz Posted March 2, 2012 Author Share Posted March 2, 2012 And also how would I make it possible so that other people without server access, can grab data from this page and use it in their own scripts? I don't really understand the question. Where does this XML file reside? And are you processing it on a different server? And which server do "other people" not have access to? Sorry I should have been more clear. The server is public, what I meant is that i'm the only one that has to the source and i'd like it so that other users would be able to generate their own data from the feed. Something like what twitter does. 1. As you see, SimpleXML XPath query returns you an array. In this case that array will always have one element, the SimpleXML object. SO you can do like this to retrieve a specific element: $element = array_pop($myDataObjects)->element; 2. You can echo out the XML of your node: $node->asXML(); Thank i'll look into that abit and see how it goes. Quote Link to comment https://forums.phpfreaks.com/topic/258130-phpxml-help/#findComment-1323201 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.