使用 simplexml 解析 xml 时出现问题
我正在尝试使用 simplexml 和 php 解析一些 xml...它从这样的服务返回给我:
<DMResponse><Code>2</Code><Description>Your request was successfully received You will receive notification once the process has been completed.</Description><ResultData><Explanation> The job name is 578bbn004 </Explanation></ResultData></DMResponse>
我从 firebug 中的 .net 面板粘贴了它。
这就是我尝试使用 php 进行解析的方式:
$result = curl_exec($ch);
print 'xml ' . $result . ' xml';
$xml = new SimpleXMLElement($result);
$code = $xml->code;
echo $code;
$result is deinfitely populated...我将其返回到上面的 print 语句中...它包含我发布的 xml 结构。
我收到的错误是“字符串无法解析为 XML”。我不明白它为什么要这样做。有什么想法吗?
I'm trying to parse some xml using simplexml and php...it's being returned to me from a service like so:
<DMResponse><Code>2</Code><Description>Your request was successfully received You will receive notification once the process has been completed.</Description><ResultData><Explanation> The job name is 578bbn004 </Explanation></ResultData></DMResponse>
I pasted this from the .net panel in firebug.
This is how I'm trying to parse is using php:
$result = curl_exec($ch);
print 'xml ' . $result . ' xml';
$xml = new SimpleXMLElement($result);
$code = $xml->code;
echo $code;
$result is deinfitely populated...I get it back in the print statement above....it contains the xml structure I've posted.
The error I'm getting is 'String could not be parsed as XML'. I don't understand why it's doing this. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
$xml = simplexml_load_string($result);
...我从来没有按照你的方式做过。但这并不是说它不起作用。编辑:如果
simplexml_load_string()
不起作用,您的字符串可能包含一些(不可打印的)无效字符或类似的字符。Try
$xml = simplexml_load_string($result);
... I've never done it the way you're doing it. That's not to say that it won't work, though.Edit: If
simplexml_load_string()
doesn't work, your string may contain some (nonprintable) invalid characters or something like that.要打印 SimpleXML 对象,您必须使用
$xml->asXML()
(打印到文件$xml->asXML(file_name)
)。如果仍然有错误:
当您在
$xml = new SimpleXMLElement($result);
之后添加此代码时,会打印什么?
To print your SimpleXML Object, you have to use
$xml->asXML()
(to print into a file$xml->asXML(file_name)
).If you still have an error :
What is print when you add this code just after
$xml = new SimpleXMLElement($result);
?