DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> PHP cdata 處理(詳細介紹)
PHP cdata 處理(詳細介紹)
編輯:關於JavaScript     
之前為項目趕進度用php 的simplexml來解析 xml, 當時發現 simplexml不支持<![CDATA標簽, 所有處於這個標簽內的值都沒有辦法取到  

當時在網上找了一個CDATA的轉換器, 修改之後, 將CDATA標簽給過濾掉。如下

復制代碼 代碼如下:
 // States:
        //
        //     'out'
        //     '<'
        //     '<!'
        //     '<!['
        //     '<![C'
        //     '<![CD'
        //     '<![CDAT'
        //     '<![CDATA'
        //     'in'
        //     ']'
        //     ']]'
        //
        // (Yes, the states a represented by strings.)
        //
        $state = 'out';
        $a = str_split($xml);
        $new_xml = '';
        foreach ($a AS $k => $v) {
            // Deal with "state".
            switch ( $state ) {
                case 'out':
                    if ( '<' == $v ) {
                        $state = $v;
                    } else {
                        $new_xml .= $v;
                    }
                break;
                case '<':
                    if ( '!' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                 case '<!':
                    if ( '[' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '<![':
                    if ( 'C' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '<![C':
                    if ( 'D' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '<![CD':
                    if ( 'A' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '<![CDA':
                    if ( 'T' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '<![CDAT':
                    if ( 'A' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case '<![CDATA':
                    if ( '[' == $v  ) {
                        $cdata = '';
                        $state = 'in';
                    } else {
                        $new_xml .= $state . $v;
                        $state = 'out';
                    }
                break;
                case 'in':
                    if ( ']' == $v ) {
                        $state = $v;
                    } else {
                        $cdata .= $v;
                    }
                break;
                case ']':
                    if (  ']' == $v  ) {
                        $state = $state . $v;
                    } else {
                        $cdata .= $state . $v;
                        $state = 'in';
                    }
                break;
                case ']]':
   if (  '>' == $v  ) {
    $new_xml .= htmlentities($cdata);
#       $new_xml.= $cdata;
//                        $new_xml .= str_replace('>','>',
  //                                  str_replace('>','<',
    //                                str_replace('"','"',
      //                              str_replace('&','&',
        //                            $cdata))));
                        $state = 'out';
                    } else {
                        $cdata .= $state . $v;
                        $state = 'in';
                    }
                break;
            } // switch
        }
        //
        // Return.
        //
            return $new_xml;


最近發現,總是有alert發出來, 說是simplexml解析出錯。

發現是原來有xml的數據是<![CDATA[domain[test]]] >. 出現了連續的3個], 造成上面的解析函數不能處理。

而且這個問題很難修正, 你不知道下次會不會有4, 5個]出現。

所以決定還是將這段解析 的代碼換成DOM XML,本身 DOM的處理還是比較簡單的,

包含DOMElement, DOMDocument, DOMNodeList, DOMNode幾個 component.

對於 DOMNode有nodeValue, nodeType, nodeName的成員函數。

首先先用loadXML將string轉化為DOMDocument對像, 再用getElementsByTagName轉化為DOMNodeList對像, 再使用->item(0)轉化為DOMNOde, 然後就可以使用上面的三種方法了。

對於 <aa color='red'>test</aa>這種xml標簽, 要使用 attribute函數。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved