What can we do with XML and PHP?

We may not be aware of the potential of these two technologies together at first, but if we look closely, we can see that XML and PHP can work in much the same way that PHP can work with a database.

Although using these two technologies together does not exclude using databases, that’s the best of all!

In the second part of this article we will see how to combine these three technologies to give free rein to our imagination.

Let’s go to trouble

To begin with, we are going to create our example XML file, which we will call “news.xml” (for being a bit original 😉 ):







Hello Conch

KaoS

Olla Kaitos to Luisete





New article in web development

Raul

Hehe hello, here we are




Well, we have already created our XML file, which, as we already know, allows us to create our own labels, although depending on what we are working on, it will be advisable to follow the standards established by the .

Now we need to create a PHP file that reads our “news.xml” file.

How can we do this? Well, it’s very simple, because PHP already includes certain functions for working with XML files. We are going to use some functions that work on PHP 4 since not everyone has PHP 5 on their server yet, but the way to work is very similar. I leave here a reference of how to work in the same way but using the functions of .

Let’s go to work.

The first thing we have to do is read the file, for this we will use our beloved function It doesn’t matter if the file is on our server or not, so if we are interested we could create a PHP file that would work equally on a remote server that reads news from our website.

See also  Composer dependency manager for PHP

//$file_path=”http://www.domain.com/news.xml”;

$file_path=”news.xml”;

$content = “”;

if($da = fopen($file_path,”r”))

{

while ($aux= fgets($da,1024))

{

$content.=$aux;

}

fclose($day);

}

else

{

echo “Error: Could not read file $file_path“;

}

If everything has gone correctly now we will have our XML file loaded in our variable $contenidoNow a detail, because our news could have special characters, to avoid errors we will put a small filter, in this case for example we will replace the tildes and the eñes in the event that there were any, for this we will use the function .

$content=ereg_replace(“á”,”a”,$content);

$content=ereg_replace(“é”,”e”,$content);

$content=ereg_replace(“í”,”i”,$content);

$content=ereg_replace(“or”,”or”,$content);

$content=ereg_replace(“ú”,”u”,$content);

$content=ereg_replace(“Á”,”A”,$content);

$content=ereg_replace(“É”,”E”,$content);

$content=ereg_replace(“Í”,”I”,$content);

$content=ereg_replace(“OR”,”OR”,$content);

$content=ereg_replace(“Ú”,”U”,$content);

$content=ereg_replace(“Ñ”,”NI”,$content);

$content=ereg_replace(“ñ”,”ni”,$content);


The next step is to load our XML file in a structure that we can work with PHP comfortably, for this task we are going to use the dom functions that are implemented from version 4 of PHP. Specifically we will use:

  • : Creates a DOM object from an XML document
  • document_element : Create a new node of type element
  • get_elements_by_tagname: Get elements by tag name
  • get_content : Gets the content of the node

$tagnames = array(“title”,”author”,”body”);

if (!$xml = domxml_open_mem($content))

{

echo “An error occurred while processing document \”$file_path\” to XML
“;

exit;

}

else

{

$root = $xml->document_element();

$size=sizeof($tagnames);

for($i=0; $i<$size; $i++)
{

$node = $root->get_elements_by_tagname($tagnames);

$j=0;

foreach ($node as $tag)

{

$array]=$tag->get_content();

$j++;

}

}

Let’s take a closer look at this last piece of code to see what it actually does. To begin with, we have created an array with the fields that each piece of news contains in the “tagnames” variable. Next we load the content variable in a DOM object, if everything went well we extract the root node, in our case “block”. The next step is to calculate the number of fields that we will obtain from each news, for this we use the function that returns the size of the array.

See also  Elements of LI lists on the same line

It is now when we extract the true information from the XML document. We are going to introduce this information in a matrix to make it easier for us to work with the data. So that the matrix would look something like this:

index \ Name Columntitleauthorbody 0Hello CaracolaKaoSOlla Kaitos to Luisete 1New article in developmentwebRaulJeje hello, here we are

The first loop extracts the tags from the nodes (first title, then author, then body).

The foreach is in charge of extracting one by one the labels of each one of the news items, so it first extracts “Hello Caracola” and in the second iteration “New article in web development”. In this way we are saving the extracted data in our matrix.

In the second iteration (repetition) of the for loop we will take the author label, and in the foreach we will extract the values ​​to introduce them into our matrix. And so until the end. Best of all, our own loop takes care of this! We only have to worry about declaring the array of labels.

Well, to make it more comfortable for us, we can create a function to which we will pass the XML file that we want it to read and return an array with the data, thus making our work cleaner and more efficient. The resulting code would be:

function LoadXML($path_file)

{

$content = “”;

if($da = fopen($file_path,”r”))

{

while ($aux= fgets($da,1024))

{

$content.=$aux;

}

fclose($da);

}

else

{

echo “Error: Could not read file $file_path“;

}

$content=ereg_replace(“á”,”a”,$content);

$content=ereg_replace(“é”,”e”,$content);

$content=ereg_replace(“í”,”i”,$content);

$content=ereg_replace(“or”,”or”,$content);

$content=ereg_replace(“ú”,”u”,$content);

$content=ereg_replace(“Á”,”A”,$content);

$content=ereg_replace(“É”,”E”,$content);

$content=ereg_replace(“Í”,”I”,$content);

$content=ereg_replace(“OR”,”OR”,$content);

$content=ereg_replace(“Ú”,”U”,$content);

$content=ereg_replace(“Ñ”,”NI”,$content);

$content=ereg_replace(“ñ”,”ni”,$content);

See also  Apache

$tagnames = array(“title”,”author”,”body”);

if (!$xml = domxml_open_mem($content))

{

echo “An error occurred while processing document \”$file_path\” to XML
“;

exit;

}

else

{

$root = $xml->document_element();

$size=sizeof($tagnames);

for($i=0; $i<$size; $i++)
{

$node = $root->get_elements_by_tagname($tagnames);

$j=0;

foreach ($node as $tag)

{

$array]=$tag->get_content();

$j++;

}

}

return $array;

}

}

Well, this is almost all set, we have already loaded an array with the content of an XML file, so now we only have to show the information we want. Let’s see in a little code how to do it.

$array=LoadXML(“news.xml”);

$num_news=sizeof($array);

for($i=0;$i<$num_news;$i++)
{

threw out ‘

‘.$array.’
‘.$array.’
‘.$array.’

‘;

}

Voilà, we already have our first example of XML+PHP. Here you can see the example working: and formatted xml file.

Well this has been all for today, I hope the article has been useful to you.

Loading Facebook Comments ...
Loading Disqus Comments ...