如何解析XML到php?

前端之家收集整理的这篇文章主要介绍了如何解析XML到php?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何转换为PHP输出
<?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>
<?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>";
  }
?>

//这样的出来马克 – 27 – $5000杰克 – 25 – $4000nav – 25 – $4000@H_502_10@

猜你在找的XML相关文章