如何使用SimpleXMLElement获取CDATA

时间:2014-09-02 18:17:32

标签: php xml xml-parsing

我试图解析以下XML结果

<?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括号内的内容,但似乎无法弄清楚如何到达那里。当我将其加载到SimpleXMLElement

时,XML对象看起来像这样
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 个答案:

答案 0 :(得分: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
"