如何将 XML 解析为 php?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9873275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse XML into php?
提问by Naveenbos
How to convert this to php output?
如何将其转换为 php 输出?
<?xml version="1.0" encoding="iso-8859-1"?>
<employee>
<name>Mark</name>
<age>27</age>
<salary>00</salary>
</employee>
<employee>
<name>Hyman</name>
<age>25</age>
<salary>00</salary>
</employee>
<employee>
<name>nav</name>
<age>25</age>
<salary>00</salary>
</employee>
回答by Naveenbos
<?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 Hyman - 25 - $4000 nav - 25 - $4000
// 像这样的输出 Mark - 27 - $5000 Hyman - 25 - $4000 nav - 25 - $4000
回答by grifos
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
我真的不明白你的问题,但如果你想使用 php 解析 xml,你可以使用 xml 扩展名:http: //php.net/manual/en/book.xml.php
You will then be able to print out the data to your liking
然后,您将能够根据自己的喜好打印数据
回答by jems
You can see this discussion: You can find the answer there:
你可以看到这个讨论:你可以在那里找到答案:
And if you are looking for the solution in CodeIgniter. These couple of links might help
如果您正在 CodeIgniter 中寻找解决方案。这几个链接可能会有所帮助
回答by pp19dd
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.
认为您的 XML 缺少根条目,因此解析会坚持下去。但是,抛开这个问题,查找 simplexml_load_file 和 simplexml_load_string。这些是访问 PHP 样式结构中的 XML 的最简单方法。
In your XML sample, I've inserted a generic 'records' entry. For example:
在您的 XML 示例中,我插入了一个通用的“记录”条目。例如:
$t = <<< EOF
<?xml version="1.0" encoding="iso-8859-1"?>
<records>
<employee>
<name>Mark</name>
<age>27</age>
<salary>00</salary>
</employee>
<employee>
<name>Hyman</name>
<age>25</age>
<salary>00</salary>
</employee>
<employee>
<name>nav</name>
<age>25</age>
<salary>00</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:
函数被警告抑制,因为您可能不想要验证警告。无论如何,此时,解析的 XML 将如下所示:
SimpleXMLElement Object
(
[employee] => Array
(
[0] => SimpleXMLElement Object
(
[name] => Mark
[age] => 27
[salary] => 00
)
[1] => SimpleXMLElement Object
(
[name] => Hyman
[age] => 25
[salary] => 00
)
[2] => SimpleXMLElement Object
(
[name] => nav
[age] => 25
[salary] => 00
)
)
)
回答by Philip
@pp19dd says your missing a root node, your current xml is in-valid.
@pp19dd 说您缺少根节点,您当前的 xml 无效。
<?xml version="1.0" encoding="iso-8859-1"?>
<document>
<employee>
<name>Mark</name>
<age>27</age>
<salary>00</salary>
</employee>
<employee>
<name>Hyman</name>
<age>25</age>
<salary>00</salary>
</employee>
<employee>
<name>nav</name>
<age>25</age>
<salary>00</salary>
</employee>
</document>
Parse with DomDocument and compile into a temp array
用 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;
}
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>