2014-09-02 42 views
1

我試圖解析以下XML獲得CDATA結果如何使用的SimpleXMLElement

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 

<!DOCTYPE WEBCNPDATA [<!ELEMENT WEBCNPDATA (SUCCESS,COUNT,SEARCHID,REPORTID,DATA)><!ELEMENT SUCCESS (#PCDATA)><!ELEMENT COUNT (#PCDATA)><!ELEMENT SEARCHID (#PCDATA)><!ELEMENT REPORTID (#PCDATA)><!ELEMENT DATA (#PCDATA)><!ATTLIST DATA ENCODING CDATA #FIXED "base64"><!ATTLIST DATA COMPRESSION CDATA #FIXED "gzip">]><WEBCNPDATA><SUCCESS>true</SUCCESS><COUNT>9</COUNT><SEARCHID>11</SEARCHID><REPORTID>1</REPORTID><DATA ENCODING="base64" COMPRESSION="gzip"><![CDATA[H4sIAMsHBlQAA71YTW8cNwy951cYPmsGIiXq47gF0l5q1+gE2aPh2ttgiwYObDct9tf3kZpdb9KL 
5EMNQyONl29F6vGR8sPdy93z7mX/4J73Lzt9/PXbH7t7fbFc3W5sOOi42GDT68enz8vu5TR9eQKI 
+3j1Xg3a49Cey/pYl0fD10Uz3fzwIyxtPNhjaWNbHK1O82b08+YaRjYe7LG0sS2ORqd5M7r++CuM 
bDzYY2ljWxyNTvNmtFW/tubVVn3amkfbV3+233rz4YN6o+PBHksb2+LkzXHejJYWvWWN3tKit6zR 
]]></DATA></WEBCNPDATA> 

我想有什麼CDATA括號內,但似乎無法弄清楚如何到達那裏。 XML對象看起來是這樣的,當我將其加載到SimpleXMLElement

object(SimpleXMLElement)#1 (5) { 
    ["SUCCESS"]=> 
    string(4) "true" 
    ["COUNT"]=> 
    string(1) "9" 
    ["SEARCHID"]=> 
    string(2) "11" 
    ["REPORTID"]=> 
    string(1) "1" 
    ["DATA"]=> 
    object(SimpleXMLElement)#2 (1) { 
    ["@attributes"]=> 
    array(2) { 
     ["ENCODING"]=> 
     string(6) "base64" 
     ["COMPRESSION"]=> 
     string(4) "gzip" 
    } 
    } 
} 

如何得到這些數據有什麼建議?謝謝!

回答

1

您需要的DATA元素顯式轉換爲字符串:

$xml = <<<EOF 
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>  
<!DOCTYPE WEBCNPDATA [<!ELEMENT WEBCNPDATA (SUCCESS,COUNT,SEARCHID,REPORTID,DATA)><!ELEMENT SUCCESS (#PCDATA)><!ELEMENT COUNT (#PCDATA)><!ELEMENT SEARCHID (#PCDATA)><!ELEMENT REPORTID (#PCDATA)><!ELEMENT DATA (#PCDATA)><!ATTLIST DATA ENCODING CDATA #FIXED "base64"><!ATTLIST DATA COMPRESSION CDATA #FIXED "gzip">]><WEBCNPDATA><SUCCESS>true</SUCCESS><COUNT>9</COUNT><SEARCHID>11</SEARCHID><REPORTID>1</REPORTID><DATA ENCODING="base64" COMPRESSION="gzip"><![CDATA[H4sIAMsHBlQAA71YTW8cNwy951cYPmsGIiXq47gF0l5q1+gE2aPh2ttgiwYObDct9tf3kZpdb9KL 
5EMNQyONl29F6vGR8sPdy93z7mX/4J73Lzt9/PXbH7t7fbFc3W5sOOi42GDT68enz8vu5TR9eQKI 
+3j1Xg3a49Cey/pYl0fD10Uz3fzwIyxtPNhjaWNbHK1O82b08+YaRjYe7LG0sS2ORqd5M7r++CuM 
bDzYY2ljWxyNTvNmtFW/tubVVn3amkfbV3+233rz4YN6o+PBHksb2+LkzXHejJYWvWWN3tKit6zR 
]]></DATA></WEBCNPDATA> 
EOF; 

$document = simplexml_load_string($xml); 
// Cast to string 
$data = (string) $document->DATA; 
var_dump($data); 

輸出:

string(308) "H4sIAMsHBlQAA71YTW8cNwy951cYPmsGIiXq47gF0l5q1+gE2aPh2ttgiwYObDct9tf3kZpdb9KL 
5EMNQyONl29F6vGR8sPdy93z7mX/4J73Lzt9/PXbH7t7fbFc3W5sOOi42GDT68enz8vu5TR9eQKI 
+3j1Xg3a49Cey/pYl0fD10Uz3fzwIyxtPNhjaWNbHK1O82b08+YaRjYe7LG0sS2ORqd5M7r++CuM 
bDzYY2ljWxyNTvNmtFW/tubVVn3amkfbV3+233rz4YN6o+PBHksb2+LkzXHejJYWvWWN3tKit6zR 
"