使用 simplexml 解析 xml 时出现问题

发布于 2024-11-29 06:24:10 字数 757 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七七 2024-12-06 06:24:10

尝试 $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.

陌伤ぢ 2024-12-06 06:24:10

要打印 SimpleXML 对象,您必须使用 $xml->asXML() (打印到文件 $xml->asXML(file_name))。

如果仍然有错误:
当您在 $xml = new SimpleXMLElement($result); 之后添加此代码时,会打印什么

echo "<pre>";
var_dump($xml);
echo "</pre>";
die();

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);
?

echo "<pre>";
var_dump($xml);
echo "</pre>";
die();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文