10

How to convert this to php output?

<?xml version="1.0" encoding="iso-8859-1"?>    
    <employee>
          <name>Mark</name>
          <age>27</age>
          <salary>$5000</salary>
          </employee>
          <employee>
          <name>Jack</name>
          <age>25</age>
          <salary>$4000</salary>
          </employee>
          <employee>
          <name>nav</name>
          <age>25</age>
          <salary>$4000</salary>
    </employee>
Quasdunk
  • 14,944
  • 3
  • 36
  • 45
  • means how i got xml file output in php? –  Mar 26 '12 at 13:59
  • You can use DomDocument to create and parse xml http://php.net/manual/en/class.domdocument.php – Philip Mar 26 '12 at 15:02
  • Mark - 27 - $5000 Jack - 25 - $4000 nav - 25 - $4000 , i need to get output like this –  Mar 27 '12 at 05:01
  • load( 'test1.xml' ); $employees = $doc->getElementsByTagName( "employee" ); foreach( $employees as $employee ) { $names = $employee->getElementsByTagName( "name" ); $name = $names->item(0)->nodeValue; $ages= $employee->getElementsByTagName( "age" ); $age= $ages->item(0)->nodeValue; $salaries = $employee->getElementsByTagName( "salary" ); $salary = $salaries->item(0)->nodeValue; echo "$name - $age - $salary\n
    "; } ?>
    –  Mar 28 '12 at 05:46

5 Answers5

20
<?php
$doc = new DOMDocument();
$doc->load( 'test1.xml' );//xml file loading here

$employees = $doc->getElementsByTagName( "employee" );
foreach( $employees as $employee )
{
  $names = $employee->getElementsByTagName( "name" );
  $name = $names->item(0)->nodeValue;

  $ages= $employee->getElementsByTagName( "age" );
  $age= $ages->item(0)->nodeValue;

  $salaries = $employee->getElementsByTagName( "salary" );
  $salary = $salaries->item(0)->nodeValue;

  echo "<b>$name - $age - $salary\n</b><br>";
  }
?> 

// the out put like this Mark - 27 - $5000 Jack - 25 - $4000 nav - 25 - $4000

Naveenbos
  • 2,532
  • 3
  • 34
  • 60
3

I don't really get your question, but f you want to parse xml using php, you can use the xml extension : http://php.net/manual/en/book.xml.php

You will then be able to print out the data to your liking

grifos
  • 3,321
  • 1
  • 16
  • 14
  • Mark - 27 - $5000 Jack - 25 - $4000 nav - 25 - $4000 i need to get output like this –  Mar 27 '12 at 05:03
3

You can see this discussion: You can find the answer there:

Best XML Parser for PHP

And if you are looking for the solution in CodeIgniter. These couple of links might help

  1. Parsing XML files using CodeIgniter?
  2. http://blog.insicdesigns.com/2009/03/parsing-xml-file-using-codeigniters-simplexml-library/
Community
  • 1
  • 1
jems
  • 128
  • 5
  • load( 'test1.xml' ); $employees = $doc->getElementsByTagName( "employee" ); foreach( $employees as $employee ) { $names = $employee->getElementsByTagName( "name" ); $name = $names->item(0)->nodeValue; $ages= $employee->getElementsByTagName( "age" ); $age= $ages->item(0)->nodeValue; $salaries = $employee->getElementsByTagName( "salary" ); $salary = $salaries->item(0)->nodeValue; echo "$name - $age - $salary\n
    "; } ?>
    –  Mar 28 '12 at 05:47
3

Think your XML is lacking a root entry, so, parsing will stick. However, that issue aside, lookup simplexml_load_file and simplexml_load_string. Those are the simplest ways to access XML in a PHP-style structure.

In your XML sample, I've inserted a generic 'records' entry. For example:

$t = <<< EOF
<?xml version="1.0" encoding="iso-8859-1"?>    
<records>
    <employee>
          <name>Mark</name>
          <age>27</age>
          <salary>$5000</salary>
    </employee>
    <employee>
          <name>Jack</name>
          <age>25</age>
          <salary>$4000</salary>
    </employee>
    <employee>
          <name>nav</name>
          <age>25</age>
          <salary>$4000</salary>
    </employee>
</records>
EOF;

$x = @simplexml_load_string( $t );

print_r( $x );

Function is warning-suppressed since you probably don't want validation warnings. Anyhow, at this point, the parsed XML will look like this:

SimpleXMLElement Object
(
    [employee] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [name] => Mark
                    [age] => 27
                    [salary] => $5000
                )
            [1] => SimpleXMLElement Object
                (
                    [name] => Jack
                    [age] => 25
                    [salary] => $4000
                )
            [2] => SimpleXMLElement Object
                (
                    [name] => nav
                    [age] => 25
                    [salary] => $4000
                )
        )
)
pp19dd
  • 3,625
  • 2
  • 16
  • 21
  • its working but i need output like; Mark - 27 - $5000 Jack - 25 - $4000 nav - 25 - $4000 –  Mar 27 '12 at 06:38
  • You just have to echo with your formating: `foreach ($x[employee] as $employe){ echo $employe['name'] .' - ' . $employe['age'] . ' - ' . $employe['salary'] . ' ';}` – grifos Mar 27 '12 at 14:37
  • How to give CSS to the output of this xml ??;load( 'test1.xml' ); $employees = $doc->getElementsByTagName( "employee" ); foreach( $employees as $employee ) { $names = $employee->getElementsByTagName( "name" ); $name = $names->item(0)->nodeValue; $ages= $employee->getElementsByTagName( "age" ); $age= $ages->item(0)->nodeValue; $salaries = $employee->getElementsByTagName( "salary" ); $salary = $salaries->item(0)->nodeValue; echo "$name - $age - $salary\n
    "; } ?>
    –  Mar 28 '12 at 06:11
0

@pp19dd says your missing a root node, your current xml is in-valid.

<?xml version="1.0" encoding="iso-8859-1"?>

    <document>
        <employee>
            <name>Mark</name>
            <age>27</age>
            <salary>$5000</salary>
        </employee>
        <employee>
            <name>Jack</name>
            <age>25</age>
            <salary>$4000</salary>
        </employee>
        <employee>
            <name>nav</name>
            <age>25</age>
            <salary>$4000</salary>
        </employee>
    </document>

Parse with DomDocument and compile into a temp array

$dom = new DOMDocument();

$dom->load('data.xml');

$employees = $dom->getElementsByTagName('employee');

foreach($employees as $employee)
{
    $names = $employee->getElementsByTagName( "name" );
    $name  = $names->item(0)->nodeValue;

    $ages = $employee->getElementsByTagName( "age" );
    $age  = $ages->item(0)->nodeValue;

    $salaries = $employee->getElementsByTagName( "salary" );
    $salary  = $salaries->item(0)->nodeValue;

    $tmp[] = array(
        'name'  =>  $name,
        'age'  =>  $age,
        'salary'  =>  $salary
    );
    continue;
}

Output to a view(list/table)

<table>
<tbody>
    <?php foreach($tmp as $staff):?>
    <tr>
        <td><?php echo $staff['name'];?></td>
        <td><?php echo $staff['age'];?></td>
        <td style="color:red;"><?php echo $staff['salary'];?></td>
    </tr>
    <?php endforeach;?>
</tbody>
</table>
Philip
  • 4,592
  • 2
  • 20
  • 28