Jump to content

Extract Specific Data From XML File


andrewgreenwood247@hotmail

Recommended Posts

Hi All,

 

I've watched the tutorial which shows how we can return the first 5 records from XML data. It all makes sense, however, what I would like to do is return specific data. See attachment...

 

phonebook.xml

 

I would like to return the record where <name></name> = Stan including all other elements relating to the record (number, address and email), But how would I code this in simplexml?

 

I assume it will be similar to the tutorial with some kind of arguement but I am well and truely stuck here...

 

Thanks

 

Andy

Link to comment
Share on other sites

Hi All,

 

I've watched the tutorial which shows how we can return the first 5 records from XML data. It all makes sense, however, what I would like to do is return specific data. See attachment...

 

phonebook.xml

 

I would like to return the record where <name></name> = Stan including all other elements relating to the record (number, address and email), But how would I code this in simplexml?

 

I assume it will be similar to the tutorial with some kind of arguement but I am well and truely stuck here...

 

Thanks

 

Andy

 

First off let me tell you I am an absolute PHP beginner. I have a similar need and just so happen to be also working off the same tutorial. In your example, to get the information in the tags you can do this:

 

<?php

$file = "phonebook.xml";

$xml = simplexml_load_file($file);

//get data from XML tags and store in array

foreach($xml->note as $item) {

$info = array(
	"name" => $item->name,
	"number" => $item->number,
	"address" => $item->address,
	"email" => $item->email
);

//simple echo out
echo "<br />";
print $info["name"];
echo "<br />";
print $info["number"];
echo "<br />";
print $info["address"];
echo "<br />";
print $info["email"];
echo "<br />";

}
?>

 

If you want to test with this, just put your php and xml file in the same directory and run it. That's at least one way to access the data in the tags, after you get it I'm not sure how to deal with it. I wont hijack your thread, so I will start another one with my question. Hope this helps.

Link to comment
Share on other sites

@benjamin - I'm not sure if that was completely what @AndyG23 was looking for, since he wanted to display the results associated with one specific record (not all of them), but posting the code helped me have code to start from.

 

@AndyG23 - Here's one way to approach it:

 

<?php

$xml = simplexml_load_file("xml.xml");

foreach($xml->note as $item) 
{
if ($item->name == "Stan")
{
	$info = array(
       	"name" => (string)$item->name,
       	"number" => (string)$item->number,
       	"address" => (string)$item->address,
       	"email" => (string)$item->email
               );
               break;
}
}

echo "<pre>";
print_r($info);
echo "</pre>";

?>

The "(string)" section is required to convert the $item->name (which is a SimpleXML object) into a string. The "break;" causes the PHP parser to jump out of the foreach loop once it finds the record it is looking for. However, keep in mind that you still need to figure out what to do if the correct record doesn't exist, or what happens if two of the same record exist (two people named "Stan", for example.) According to the code above, only the first "Stan"'s info would be returned.

 

If you have any questions let me know.

Link to comment
Share on other sites

Ah, Thanks Ben, That is what I was looking for.

 

So, Say if each item had it's own unique id (in this case the telephone number) I could add further agruments to the "if" statement?

 

Like this...

 

if ($item->name == "Stan" && $item->number == "+44325656565")

 

Thanks for your help on this guys (both Benjamin and Ben), it's very much appreciated.

Link to comment
Share on other sites

However, keep in mind that you still need to figure out what to do if the correct record doesn't exist

Hi Ben,

 

I had a look at this last night and tried to figure out what code I would write to account for a record that didn't exist. Again, I'm baffled.

 

Any ideas?

 

Regards

 

Andy

Link to comment
Share on other sites

Something like this:

 

<?php

$xml = simplexml_load_file("xml.xml");
$found = false;

foreach($xml->note as $item) 
{
if ($item->name == "Stan")
{
	$found = true;
	$info = array(
       	"name" => (string)$item->name,
       	"number" => (string)$item->number,
       	"address" => (string)$item->address,
       	"email" => (string)$item->email
	);
       break;
}
}

if($found){
// Item found
echo "<pre>";
print_r($info);
echo "</pre>";
}else{
// Item not found
echo "<p>Nooooooo! I can't find itttt!  SAD FACE</p>";
}

?>

Link to comment
Share on other sites

<?php 

$xml = simplexml_load_file("xml.xml"); 

foreach($xml->note as $item)  
{ 

if(!$item->name) {
// no name tag to process
echo "No items found";

} else {

       if ($item->name == "Stan") 
       { //there's a name tag.  check and return if criteria is met.
               $info = array( 
               "name" => (string)$item->name, 
               "number" => (string)$item->number, 
               "address" => (string)$item->address, 
               "email" => (string)$item->email 
               ); 
       break; 
       } 
}
} 


       // Item was found and now returned 
       echo "<pre>"; 
       print_r($info); 
       echo "</pre>"; 


?>

 

I was thinking something along the lines of the above.

But, I'm at work now and can't try any of them out. :(

 

Thanks BeeDev

 

Just thought I would add onto this... I've just read the "Help Vampires" post, I hope I'm not deemed as a "Help Vampire". :(

Link to comment
Share on other sites

I've just read the "Help Vampires" post, I hope I'm not deemed as a "Help Vampire". :(

No -- that post was aimed primarily at people who post questions and don't try to help themselves first (by searching Google, or this forum in case others have asked the same question, experimenting and trying things on their own, etc.) Basically, people who post lots of questions and simply expect someone to provide them with all the answers, without trying to solve them on their own first. If you've made an attempt and are still struggling, we are happy to help. :)

Link to comment
Share on other sites

Hi Guys,

 

I just wanted to thank all of the people who posted above. I had a technical test for a software engineers job on Thursday and it was based on extracting data from numerous xml files.

 

Without the knowledge you all passed on and with myself having no prior experience with extracting xml data I would have been very stuck indeed!

 

So, thanks again from a very happy AndyG

 

Interview is on Thursday, fingers crossed.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...