熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

PHP cdata 處理(詳細介紹)

2022-06-13   來源: JSP教程 
之前為項目趕進度用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]]] > 出現了連續的個] 造成上面的解析函數不能處理

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

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

包含DOMElement DOMDocument DOMNodeList DOMNode幾個 component

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

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

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


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20144.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.