如何用php解析xml? [英] How to parse xml with php?
本文介绍了如何用php解析xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
PHP 的最佳 XML 解析器
如何使用php解析XML文件
我无法解析这个 xml...真正的 xml 是 这里
<?xml version="1.0" encoding="ISO-8859-1" ?><eqlist><earhquake name="2012.12.31 18:35:13" lokasyon="CAMONU-AKHISAR (MANİSA) İlksel" lat="38.9572" lng="27.8965" mag="2.9" Depth="5.0"/><earhquake name="2012.12.31 18:54:09" lokasyon="VAN GÖLÜ İlksel" lat="38.7273" lng="43.1598" mag="2.3" Depth="2.1"/><earhquake name="2012.12.31 21:00:49" lokasyon="KUCUKESENCE-ERENLER (SAKARYA) İlksel" lat="40.7347" lng="30.4742" mag="1.9" Depth="4.4"/></eqlist>
我该如何解析它?问题来自运行良好远程站点的谷歌地图应用程序的 xml 文件的前两个字符.看看那个数组
[0] =>ÿþ<?xml version="1.0" encoding="ISO-8859-1" ?>
解决方案
您可以使用 SimpleXML_Load_String.
';$xml = <<<ENDXML<?xml version="1.0" encoding="ISO-8859-1" ?><eqlist><earhquake name="2012.12.31 18:35:13" lokasyon="CAMONU-AKHISAR (MANISA) Ilksel" lat="38.9572" lng="27.8965" mag="2.9" Depth="5.0"/><earhquake name="2012.12.31 18:54:09" lokasyon="VAN GÖLÜ Ilksel" lat="38.7273" lng="43.1598" mag="2.3" Depth="2.1"/><earhquake name="2012.12.31 21:00:49" lokasyon="KUCUKESENCE-ERENLER (SAKARYA) Ilksel" lat="40.7347" lng="30.4742" mag="1.9" Depth="4.4"/></eqlist>ENDXML;//转换为对象$obj = SimpleXML_Load_String($xml);//解析一些属性foreach ($obj as $quake){//属性名称区分大小写$loc = $quake->attributes()->lokasyon;$dep = $quake->attributes()->Depth;回声 PHP_EOL ."$loc $dep";}
Possible Duplicate:
Best XML Parser for PHP
How to parse XML file using php
i could not parse this xml... the real xml is here
<?xml version="1.0" encoding="ISO-8859-1" ?>
<eqlist>
<earhquake name="2012.12.31 18:35:13" lokasyon="CAMONU-AKHISAR (MANİSA) İlksel" lat="38.9572" lng="27.8965" mag="2.9" Depth="5.0" />
<earhquake name="2012.12.31 18:54:09" lokasyon="VAN GÖLÜ İlksel" lat="38.7273" lng="43.1598" mag="2.3" Depth="2.1" />
<earhquake name="2012.12.31 21:00:49" lokasyon="KUCUKESENCE-ERENLER (SAKARYA) İlksel" lat="40.7347" lng="30.4742" mag="1.9" Depth="4.4" />
</eqlist>
how can i parse it? the problem is coming from first two chars of the xml file which is running nice remote site's google map application. Look at that array
[0] => ÿþ<?xml version="1.0" encoding="ISO-8859-1" ?>
解决方案
You can use SimpleXML_Load_String.
<?php // RAY_temp_burhan.php
error_reporting(E_ALL);
echo '<pre>';
$xml = <<<ENDXML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<eqlist>
<earhquake name="2012.12.31 18:35:13" lokasyon="CAMONU-AKHISAR (MANISA) Ilksel" lat="38.9572" lng="27.8965" mag="2.9" Depth="5.0" />
<earhquake name="2012.12.31 18:54:09" lokasyon="VAN GÖLÜ Ilksel" lat="38.7273" lng="43.1598" mag="2.3" Depth="2.1" />
<earhquake name="2012.12.31 21:00:49" lokasyon="KUCUKESENCE-ERENLER (SAKARYA) Ilksel" lat="40.7347" lng="30.4742" mag="1.9" Depth="4.4" />
</eqlist>
ENDXML;
// CONVERT TO AN OBJECT
$obj = SimpleXML_Load_String($xml);
// PARSE OUT SOME ATTRIBUTES
foreach ($obj as $quake)
{
// ATTRIBUTE NAMES ARE CASE-SENSITIVE
$loc = $quake->attributes()->lokasyon;
$dep = $quake->attributes()->Depth;
echo PHP_EOL . "$loc $dep";
}
这篇关于如何用php解析xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文