我收到的xml看起来像:
<?xml version="1.0" encoding="utf-8"?>
<ChangePassPhraseRequestResponse
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="www.envmgr.com/LabelService">
<Status>2012</Status>
<ErrorMessage>Account Error encountered (Log ID: 11845)</ErrorMessage>
</ChangePassPhraseRequestResponse>
我正在尝试编码为json(其中xml =以上响应)
$data = simplexml_load_string($xml);
echo json_encode($data);
我得到的错误是:
simplexml_load_string(): namespace warning : xmlns: URI www.envmgr.com/LabelService is not absolute
SimpleXMLElement::__construct(): Entity: line 4: parser error : Start tag expected, '<' not found
答案 0 :(得分:0)
您需要使用preg_replace()
来删除命名空间。
$xml_string = preg_replace('/xmlns[^=]*="[^"]*"/i', '', $xml);
$data = simplexml_load_string($xml_string);
echo json_encode($data);
<强>结果:强>
{"Status":"2012","ErrorMessage":"Account Error encountered (Log ID: 11845)"}
答案 1 :(得分:0)
问题是xmlns URL(www.envmgr.com/LabelService)没有指定方案(http:// OR https://)。如果可能的话,将其添加到xmlns网址或尝试使用以下代码禁止警告:
$data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOWARNING );
echo json_encode($data);