如何将 XML 解析为 php?
Posted
技术标签:
【中文标题】如何将 XML 解析为 php?【英文标题】:How to parse XML into php? 【发布时间】:2012-04-10 00:35:10 【问题描述】:如何将其转换为 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 中得到 xml 文件输出的? 可以使用DomDocument创建和解析xml php.net/manual/en/class.domdocument.php Mark - 27 - $5000 Jack - 25 - $4000 nav - 25 - $4000 ,我需要这样的输出 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"; ?> 【参考方案1】:我不太明白你的问题,但如果你想使用 php 解析 xml,你可以使用 xml 扩展名:http://php.net/manual/en/book.xml.php
然后您就可以根据自己的喜好打印数据了
【讨论】:
Mark - 27 - $5000 Jack - 25 - $4000 nav - 25 - $4000 我需要得到这样的输出【参考方案2】:你可以看到这个讨论:你可以在那里找到答案:
Best XML Parser for PHP
如果您正在 CodeIgniter 中寻找解决方案。这几个链接可能会有所帮助
-
Parsing XML files using CodeIgniter?
http://blog.insicdesigns.com/2009/03/parsing-xml-file-using-codeigniters-simplexml-library/
【讨论】:
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"; ?>【参考方案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
)
)
)
【讨论】:
它的工作,但我需要像这样的输出;马克 - 27 - $5000 杰克 - 25 - $4000 导航 - 25 - $4000 你只需要回应你的格式:foreach ($x[employee] as $employe) echo $employe['name'] .' - ' . $employe['age'] . ' - ' . $employe['salary'] . ' ';
如何将 CSS 赋予这个 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"; ?>【参考方案4】:
@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>
【讨论】:
【参考方案5】:<?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 美元 导航 - 25 - $4000
【讨论】:
以上是关于如何将 XML 解析为 php?的主要内容,如果未能解决你的问题,请参考以下文章