Jump to content

PHP/XML help


Samuz

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.