How to fetch data from XML file using php

I am sharing below PHP code to a project I was working on. This project was based on extracting data from an FTP server xml file and output the result on HTML using PHP code. The result will be outputted into a tabular form with column headings.

XML File: Name file as “ftpxml.xml

[code lang=”xml”]

<?xml version=“1.0” encoding=“ISO-8859-15”?>

[/code]

PHP Code:

[code lang=”php”]

<?php

// Loading the XML file

$xml = simplexml_load_file(“ftpxml.xml);

echo “

”.$xml->getName().”


”;

echo “

”;

foreach($xml->children() as $ftpxml)

{

echo ‘

’;

echo ‘

”;

echo ‘

”;

echo ‘

”;

echo ‘

”;

echo ‘

”;

echo ‘

”;

echo ‘

’;

}

echo ‘

PIDAccountTimeHostLocalport
’ . $ftpxml->attributes()->pid . “ ’ . $ftpxml->attributes()->account . “ ’ . $ftpxml->attributes()->time . “ ’ . $ftpxml->attributes()->host . “ ’ . $ftpxml->attributes()->localhost . “ ’ . $ftpxml->attributes()->localport . “
’;

?>

[/code]

Feel free to try the script out and comment on it 🙂

 
comments powered by Disqus