可能重复:
Best XML Parser for PHP
How to parse XML file using php
我无法解析这个xml ......真正的xml是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>
我该如何解析它?问题来自xml文件的前两个字符,它运行了很好的远程站点的谷歌地图应用程序。看看那个数组
[0] => ÿþ<?xml version="1.0" encoding="ISO-8859-1" ?>
答案 0 :(得分:3)
您可以使用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";
}
答案 1 :(得分:1)
面向对象的方式
$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" />
<!-- Etc... -->
</eqlist>
ENDXML;
$dom = new DOMDocument();
$dom->loadXML($xml, LIBXML_NOBLANKS);
然后您可以使用DOMDocument定义的各种方法。对于使用XSD检查有效性非常有用的方法之一是schemaValidate