如何在加载xml文件时避免php破坏变音字符?

时间:2012-10-10 08:58:11

标签: php xml-parsing

我有一个用PHP解析的xml,它包含一些变音字符。 包含字符串的每个节点都有一个包装在cdata标签中的字符串,但我的问题在解析xml之前就开始了:当我加载文件时(我还试图用file_get_contests打印出文件的内容,结果相同),变音字符被破坏,所以例如ü变成了Ã。运行htmlentities()是徒劳的,因为角色已经在这一点上被破坏了。 xml编码是utf-8,所以我不知道还有什么办法可以避免这个问题。有人可以帮帮我吗?

编辑: xml示例'locations.xml':

<?xml  version="1.0" encoding="utf-8"?>
<locations>
  <location>
    <id>481</id>
    <city><![CDATA[Zürich]]></city>
  </location>
</locations>

php代码:

function parseLocations(){
    $xml = new DOMDocument();
$xml->load('locations.xml');
$xml->preserveWhiteSpace = false;
$data = array();
    $locations = $xml->childNodes->item(0);
    for($i=0; $i<$locations->childNodes->length; $i++){
        $location = $locations->childNodes->item($i);
        if($location->nodeName=="location"){
            $tmp = parseVenue($location);
            $data[] = $tmp;
        }
    }
    echo var_export($data, true); 
}

function parseVenue($location){
    //I need to exclude some of the nodes
    $exclude = array('#text'); 
    $data = array();
    for($i=0; $i<$location->childNodes->length; $i++){
        $tag = $location->childNodes->item($i);
        if(!in_array($tag->nodeName, $exclude)){
            $data[$tag->nodeName] = $tag->nodeValue;
        }

    }
    return $data;
}

回显输出:

array ( 0 => array ( 'id' => '481', 'city' => 'Zürich'), ) 

0 个答案:

没有答案
相关问题