我有一个带有返回XML的API调用的响应字符串。我需要从响应中删除doctype和其他顶级名称空间声明,以将值解析为SimpleXMLElement。我试图创建一个字符串正则表达式,它可以工作但是有一种更通用的方法来从响应中删除声明吗? 这是响应字符串和我用来删除声明的代码:
代码
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$retValue = curl_exec($ch);
curl_close($ch);
return json_encode(array("response" => $retValue));
生成的JSON编码XML响应
{
"response": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE list_tests [\n<!ENTITY % ext1 PUBLIC \"-\/\/W3C\/\/LIST TESTS for XHTML\/\/EN\" \"http:\/\/www.test.com\/api\/list_tests.ent\">\n<!ENTITY % ent1 PUBLIC \"-\/\/W3C\/\/ENTITIES for XHTML\/\/EN\" \"http:\/\/www.test.com\/api\/xhtml.ent\">\n%ext1;\n%ent1;\n]>\n<list_tests xmlns=\"http:\/\/www.test.com\/schema\">\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test1<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test2<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_nameTest3<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test4<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test5<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test6<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test7<\/campaign_name>\n <\/campaigns>\n<\/list_tests>\n\n"
}
使用以下代码,我可以删除doctype和其他声明。
$retValue = str_replace('<!DOCTYPE list_account_campaigns [
<!ENTITY % ext1 PUBLIC "-//W3C//LIST ACCOUNT CAMPAIGNS for XHTML//EN" "http://www.mongoosemetrics.com/api/list_account_campaigns.ent">
<!ENTITY % ent1 PUBLIC "-//W3C//ENTITIES for XHTML//EN" "http://www.mongoosemetrics.com/api/xhtml.ent">
%ext1; %ent1; ]>', '', $retValue);
return json_encode(array("response" => $retValue));
产生所需的响应
{
"response": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<list_tests xmlns=\"http:\/\/www.test.com\/schema\">\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test1<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test2<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_nameTest3<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test4<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test5<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test6<\/campaign_name>\n <\/campaigns>\n <campaigns>\n <campaign_code>1234567<\/campaign_code>\n <campaign_name>Test7<\/campaign_name>\n <\/campaigns>\n<\/list_tests>\n\n"
}
除了使用正则表达式之外,还有更通用的方法从curl响应中删除命名空间声明吗?感谢。