Creating XML with PHP / Flickr Example

in #utopian-io7 years ago (edited)

Hi friends I have been dealing with programming for 3 years in Seyda.In this article I will tell you how to create XML with PHP.XML(Extensible Markup Language) Generally used for data storage, but nowadays it is preferred to use data exchange.In this article, we will learn to read and parse XML files.

SIMPLEXML

We will use the 'simpleXMLElemtent' class to work with XML documents with Php.this class will give us documents by parse and it will be more comfortable to solve.

Save the following codes in the root directory of example.xml.

<entry>
 <person>
  <name> Seyda Bozkurt </name>
  <city> Sakarya </city>
 </person>
 <person>
  <name> Mert </name>
  <city> Türkiye </city>
 </person>
</entry>

I now instantiate an object in the class 'SimpleXMLElemtent' to parse the example.xml file.To see the result, use the ' print_r () ' function.

index.php:

<?php
$xml = new SimpleXMLElement('example.xml',null,true);
echo "<pre>";
print_r ($xml);
echo "</pre>";
?>

After you run the codes, you will see a result like this.

1.png

Finding child nodes

XML contains parent and child nodes.To find this, the ' SimpleXMLElement ' class is found using the 'children()' method.Let's find the subnodes by going to the contents of the file 'example.xml'. you can examine the following codes.

<?php
$xml = new SimpleXMLElement('example.xml',null,true);
foreach($xml ->children() as $person){
foreach($person->children() as $name){
echo $name.'<br>'; } echo "<hr>";
}

you need to get something like this.

2.png

Attribute Listing

In some XML data, nodes 'attribute' occurs.To access these values, use the 'attribute ()' method of the 'SimpleXMLElement' class.we will examine the following codes to better introduce the subject as well.

<?xml version="1.0" encoding ="utf-8"?>
<entry>
  <person name="Seyda Bozkurt" city="Sakarya" />
  <person name="Mert" city="Türkiye"/>
</entry>

save it to a file with an .xml extension. now I see the code that will print it on the screen.

<?php
$xml = new SimpleXMLElement('example.xml',null,true);
foreach($xml->person as $kisi){
  foreach($kisi-> attributes() as $key=>$value)
   { echo $key.'='.$value.'<br>';  }  
 echo '<hr>'; }
?>

Two nested loops have been established above. Lists all contacts with the outer ' foreach ' cycle, while listing the attributes within the inner loop and people.Each ' attribute ' is printed on the screen and the "< hr>" HTML tag is put to add a horizontal line between the contacts.

3.png

Flickr Example

Filck.com published the last photos that the members had added to the address "http://api.flickr.com/services/feeds/photos_public.gne".I record the following codes as flickr.php.

<?php
$FLICKR_URL = 'http://api.flickr.com/services/feeds/photos_public.gne';
$xml = new SimpleXMLElement($FLICKR_URL,null,true);
foreach ($xml->entry as $flickr){
  $title = $flickr->title;
  $link = $flickr->link[0]['href'];
  $photo = $flickr->link[1]['href'];
  $photo=str_replace('_b.jpg','_s.jpg',$foto);
   echo '<a href="'.$link.'" title="'.$title.'">';
   echo '<img src="'.$photo.'" alt="'.$title.'"/>';
   echo '</a> ';
}
?>

I now share these images with you when I run these commands.If there are pictures on the screen there is no problem.
some pictures may not appear, some photos from flickr.com have restricted access under member privacy policy.If you click on the pictures, you will be connected to the Web page to enlarge the image.

4.png

github link : https://github.com/kansiper/Creating-XML-with-PHP
github picture
uygulamaprofil.png



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
http://php.net/manual/en/class.simplexmlelement.php

Your contribution cannot be approved yet. See the Utopian Rules. Please edit your contribution to reapply for approval.

Please respond to cheetah bot's comment. It is informed that the tutorial needs to be original and unique.

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @seydabozkurt I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x