我尝试解析XML文件,但是当我的文件中有重音(é,à,...)时,php xml解析器会剪切字符串。
function __construct(){
$this->xml_parser = xml_parser_create("UTF-8");
xml_set_object ( $this->xml_parser, $this );
xml_set_element_handler($this->xml_parser, "startTagArticle", "endTagArticle");
xml_set_character_data_handler($this->xml_parser, "contentsArticle");
}
如果我的文件包含以下字符串:cccccékkkkkkllllllll,它将在我的网络浏览器中显示为ékkkkklllllll,我不知道为什么。
<?xml version="1.0" encoding="utf-8"?>
<XML>
<TITRE>cccccékkkkkkéllllllll</TITRE>
<RESUME>Ceci est le premieré article de blog et l'aut</RESUME>
<CONTENT>Ceci l'aut est effectivement mon premier article de blog
et c'est un test
</CONTENT>
<FILE_COMMENTS>com1.xml</FILE_COMMENTS>
<VISIBLE>true</VISIBLE>
<TAG>Cool</TAG>
<TAG>article</TAG>
</XML>
基本解析功能:
function startTagArticle($parser, $data){ switch ($data){ case "RESUME":
$this->articleSection = 1;
break; case "CONTENT":
$this->articleSection = 2;
break; case "FILE_COMMENTS":
$this->articleSection = 3;
break; case "VISIBLE":
$this->articleSection = 4;
break; case "TITRE":
$this->articleSection = 5;
break; case "TAG":
$this->articleSection = 6;
break; default:
$this->articleSection = 0;
break; } }
/** Do not work **/
function contentsArticle($parser, $data){
if ($this->articleSection == 1){
$this->resumeArticleCourant = $data;
}
if ($this->articleSection == 2){
$this->contentArticleCourrant = $data;
}
if ($this->articleSection == 3){
$this->fichier_comArticleCourant = $data;
$this->comm = new commentaire();
$this->comm->init($this->comm_rep.$data);
}
if ($this->articleSection == 4){
$this->visibleArticleCourant = $data;
}
if ($this->articleSection == 5){
$this->titreArticleCourant = $data;
}
if ($this->articleSection == 6){
array_push($this->tag_array,$data);
}
}
奇怪的是,如果我使用以下内容的文章功能,我已经替换了= by。=,它工作正常。 Accents charactere似乎削减/停止了XML流程。
/** work **/
function contentsArticle($parser, $data){
if ($this->articleSection == 1){
$this->resumeArticleCourant .= $data;
}
if ($this->articleSection == 2){
$this->contentArticleCourrant .= $data;
}
if ($this->articleSection == 3){
$this->fichier_comArticleCourant = $data;
$this->comm = new commentaire();
$this->comm->init($this->comm_rep.$data);
}
if ($this->articleSection == 4){
$this->visibleArticleCourant = $data;
}
}
if ($this->articleSection == 5){
$this->titreArticleCourant .= $data;
}
if ($this->articleSection == 6){
array_push($this->tag_array,$data);
}
}
答案 0 :(得分:0)
尝试;
//add xml_parser_set_option($xml_parser,XML_OPTION_TARGET_ENCODING, "ISO-8859-1"). //and the encoding is <?xml version="1.0" encoding="utf-8"?>
希望有所帮助