SimpleXML in PHP 5

Source: Internet
Author: User
Tags php server php and php code xml parser

In the previous tutorial you may have read XML parsing using PHP (in the middle, the full XML parser takes the first class, which takes tons of line code and extra time.) In PHP5, we can use simple XML classes to parse XML quickly in only two lines of code.

Our Extensible Markup Language text
I use the same text as the Jubba XML parsing tutorial, but I can add some attributes to show you the extra power of simple xml:

<description>equipped with a Japanese mind-control device, the giant monster has attacked important harbours along T He California coast. President to take action. </description>
</story>
<story>
<description>the Beast is seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported. </description>
</story>
<story>
<description>the "London Angel" known only as "Kit" has saved the U.K yet. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled Thank you goes out to her. </description>
</story>
<story>
<description>uhhhmmm ... No comment really ... just a little creepy to them together ... </description>
</story>
<story>
<description>the gifted youngster ' s birthday party should to be a blast. It turning thirteen and has requested a large cake, ice cream, and a petting zoo with complete pony. </description>
</story>
</news>


Our PHP code
I know you expect 200 of the left and right lines of advanced PHP and incomprehensible comments. You're wrong, here's two lines you need a simple XML object:


Since we ' re already using PHP5, why don ' t we exploit easy to use their () command?
$xmlFileData = file_get_contents ("Input.xml");
Here ' s we simple XML parser!
$xmlData = new SimpleXMLElement ($xmlFileData);
And here ' s the output.
Print_r ($xmlData);


Execute, in your PHP server, you get some nonsense garbled like the following.
SimpleXMLElement Object (
[Story] => Array (
[0] => simplexmlelement Object (
[Headline] => Godzilla attacks la!
[description] => equipped with a Japanese Mind-control device giant Monster has attacked important harbours The California coast. President to take action.
)
[1] => simplexmlelement Object (
[Headline] => Bigfoot spotted at m.i.t. Dining
[description] => the beast is seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported.
)
[2] => simplexmlelement Object (
[Headline] => London Angel saves England
[Description] => the "London Angel" known only as "Kit" has saved the U.K. Yet again. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled Thank you goes out to her.
)
[3] => simplexmlelement Object (
[Headline] => six-eyed Mans to is Wed to a eight-armed Woman
[description] => uhhhmmm ... No comment really ... just a little creepy them to ...
)
[4] => simplexmlelement Object (
[Headline] => Ahmed ' s birthday extravaganza!
[description] => the gifted youngster ' s birthday party should to be a blast. It turning thirteen and has requested a large cake, ice cream, and a petting zoo with complete pony.
)
)
)
)



In order to get the data actually from the confusing values, we can use it as an array for a class to apply to it. Like this:


Retrieving the headline from the story
$xmlHeadline = $xmlData->story[0]->headline;
Printing our Headline
Print ($xmlHeadline);


But what if you want the story of the date, but don't want to add another node array to the story? Just add a property. And simple XML can also handle the attributes! So, our new XML looks like this:

<?xml version= "1.0"?>
<news>
<story>
<description>the Beast is seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported. </description>
</story>
<story>
<description>the "London Angel" known only as "Kit" has saved the U.K yet. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled Thank you goes out to her. </description>
</story>
<story>
<description>uhhhmmm ... No comment really ... just a little creepy to them together ... </description>
</story>
<story>
<description>the gifted youngster ' s birthday party should to be a blast. It turning thirteen and has requested a large cake, ice cream, and a petting zoo with complete pony. </description>
</story>
</news>

So now we have all our news and dates, but how do we show it to the people? We can use a simple foreach loop to output all the people

//outputing all of We XML to People
foreach ($xmlData->story as $story) {
 print ("  print ($story->description. "<br/>_________________________<br/>");
 print ($story->headline["date"). "<br/><br/>");
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.