如何将XML解析为php?

时间:2012-03-26 13:43:36

标签: php codeigniter xml-parsing

如何将其转换为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>

5 个答案:

答案 0 :(得分: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>";
  }
?> 

//这样的出局     马克 - 27 - 5000美元     杰克 - 25 - 4000美元     nav - 25 - $ 4000

答案 1 :(得分:3)

我真的不明白你的问题,但如果你想用php解析xml,你可以使用xml扩展名:http://php.net/manual/en/book.xml.php

然后,您就可以根据自己的喜好打印出数据了

答案 2 :(得分:3)

你可以看到这个讨论:你可以在那里找到答案:

Best XML Parser for PHP

如果您正在寻找CodeIgniter中的解决方案。这几个链接可能会有所帮助

  1. Parsing XML files using CodeIgniter?
  2. http://blog.insicdesigns.com/2009/03/parsing-xml-file-using-codeigniters-simplexml-library/

答案 3 :(得分:3)

认为您的XML缺少根条目,因此解析将坚持下去。但是,除了这个问题外,查找simplexml_load_file和simplexml_load_string。这些是在PHP风格的结构中访问XML的最简单方法。

在您的XML示例中,我插入了一个通用的“记录”条目。例如:

$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 );

由于您可能不需要验证警告,因此功能会被警告抑制。无论如何,在这一点上,解析的XML将如下所示:

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
                )
        )
)

答案 4 :(得分:0)

@ pp19dd说你缺少一个根节点,你当前的xml是无效的。

<?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>

使用DomDocument解析并编译成临时数组

$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;
}

输出到视图(列表/表格)

<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>