2011-12-24 17 views
1

XMLファイルを解析しようとしましたが、ファイルにアクセント(é、à、...)があると、php xmlパーサーが文字列を切り捨てます。アクセントを含む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ékkkkkkéllllllllを、それは私のWebブラウザでékkkkkélllllllとして表示されます、と私は理由を知りません。

<?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> 

基本的な解析機能:。私はで=を交換した次contentsArticle機能を使用する場合

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); 
     } 
    } 

奇妙なことがある=、それが正常に動作します。アクセント文字は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

を役に立てば幸い? 例:header( "Content-Type:text/xml; charset = utf-8");ブラウザで「ソースを表示」すると、完全な文字列または切り取られた文字列が表示されますか?あなたが切り取ったものを見ると、問題は解析中です。さもなければ、おそらく符号化の問題です。 – Yaniro

+0

XMLファイルをブラウザで直接開くと、完全な文字列が表示されます。だから、解析の問題が多いと思います。 – psic

+0

同じ振る舞いで、同じ診断で解決されました。私は '='を '。='に変更し、単語は分割されません。とにかくなぜか分からない。 – Sebastian

答えて

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"?> 

は、それはあなたがブラウザに適切なヘッダーを送信しました

+0

ありがとうございますが、動作しません。それはちょうど "é"文字をbyで置き換えます。 – psic

+0

はあなたのXMLエンコーディングutf-8 ..ですか? –

+0

私は信じています。しかし、それをどうやって確認するのですか? – psic

関連する問題