用PHP读取XML

时间:2012-10-16 13:43:27

标签: php xml-parsing

我正在尝试使用simplexml_load_file读取XML。以下是我的XML:

<map_style>
    <background_color>FFFFFF</background_color>         

    <state_color>c5d577</state_color>
    <label_color>212121</label_color>

    <state_hover_color>AECE13</state_hover_color>
    <label_hover_color>FFFFFF</label_hover_color>

    <state_inactive_color>dbe99c</state_inactive_color>
    <label_inactive_color>212121</label_inactive_color>

    <stroke_color>AECE13</stroke_color>

    <lake_color>ffffff</lake_color>
    <lake_stroke_color>ffffff</lake_stroke_color>

    <other_color>FFFFFF</other_color>
    <other_stroke_color>FFFFFF</other_stroke_color>
</map_style>

<!------for map on home page---------------->
<mapstyle_home mapOutlineColor="c2da78" mapRollOverColor="AECE13" mapGradientColor="AECE13"></mapstyle_home>

<!------for featured properties settings---------------->
<featured_property_style>
    <layout width="203" height="182" background="#FFFFFF" imageBackground="#aec565" showGrid="1" autoPlayDefault="1" />
    <buttons navigationDots="1" playPause="1" />
    <text option="1" />
<featured_property_style>

我可以阅读XML </map_style>。但是,只要我添加:

<!------for map on home page---------------->
<mapstyle_home mapOutlineColor="c2da78" mapRollOverColor="AECE13" mapGradientColor="AECE13"></mapstyle_home>

<!------for featured properties settings---------------->
<featured_property_style>
    <layout width="203" height="182" background="#FFFFFF" imageBackground="#aec565" showGrid="1" autoPlayDefault="1" />
    <buttons navigationDots="1" playPause="1" />
    <text option="1" />
<featured_property_style>

尝试阅读XML然后它会出错。

以下是我的代码:

$xml = simplexml_load_file($strCurlUrl);

    //$xml = new SimpleXMLElement($strCurlUrl);

    //echo $xml->getName() . "<br />";
    $this->m_arrXml = array();
    foreach($xml->children() as $child)
    {
      //echo $child->getName() . ": " . $child . "<br />";
      $this->m_arrXml[$child->getName()] = $child;

    }

其中$strCurlUrl高于xml文件。

1 个答案:

答案 0 :(得分:5)

看起来您的XML无效。 XML总是只需要一个根节点!在你的例子中有三个......