无法使用 simplexml_load_string 读取 XML 文件

发布于 2024-12-07 22:11:12 字数 667 浏览 0 评论 0原文

我一直在尝试读取 xml 文件,但它给了我一个奇怪的错误。 我的 XML 如下

<?xml version='1.0' encoding='UTF-8'?>
<response>
    <url>http://xyz.com</url>
    <token>xxxxxxx<token>
</response>

要阅读此内容,我正在使用它

simplexml_load_string(variable containing xml goes here)

,但它给了我这个错误

警告:simplexml_load_string() [function.simplexml-load-string]: 实体:第 1 行:解析器错误:需要开始标记,“<”未找到于 第 47 行

警告:simplexml_load_string() [function.simplexml-load-string]:1 在第 47 行

警告:simplexml_load_string() [function.simplexml-load-string]:^ 在第 47 行

I have been trying to read the xml file, but it is giving me a strange error.
My XML is as follows

<?xml version='1.0' encoding='UTF-8'?>
<response>
    <url>http://xyz.com</url>
    <token>xxxxxxx<token>
</response>

To read this I am using

simplexml_load_string(variable containing xml goes here)

but it is giving me this error

Warning: simplexml_load_string() [function.simplexml-load-string]:
Entity: line 1: parser error : Start tag expected, '<' not found in
on line 47

Warning: simplexml_load_string() [function.simplexml-load-string]: 1
in on line 47

Warning: simplexml_load_string() [function.simplexml-load-string]: ^
in on line 47

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小…红帽 2024-12-14 22:11:12

感谢大家的关注和回复,
但问题出在其他地方
在我的curl请求中,我没有将CURLOPT_RETURNTRANSFER设置为true,这导致XML没有被记录在变量中,并且以某种方式打印在屏幕上,给人一种它来自变量的错觉,但事实并非如此。
但是我将 CURLOPT_RETURNTRANSFER 设置为 1,现在它给了我正确的结果。
很抱歉犯了愚蠢的错误。
谢谢大家。

Thanx all for the attention and replies,
But the problem lied somewhere else
In my curl request the i had not set CURLOPT_RETURNTRANSFER to true and that was causing the XML not to be recorded in the variable and was somehow was getting printed on the screen giving the illusion that it is coming from the variable but it was not.
However i set CURLOPT_RETURNTRANSFER to 1 and it is giving me correct results now.
Sorry for the silly mistake.
and thanx all.

把昨日还给我 2024-12-14 22:11:12

您从 API 获得的响应不是格式良好/有效的 XML:

<token>xxxxxxx<token>
              ^ missing /

由于这是一个 API 响应,您需要先修复它,然后 simplexml 才能实际读取它。您可以利用 整洁的扩展文档解决这个问题:

$config = Array(
    'input-xml' => 1,
);
$xml = tidy_repair_string($xml, $config);

$xmlObj = simplexml_load_string($xml);

$doc = dom_import_simplexml($xmlObj)->ownerDocument;

$xpath = new DOMXpath($doc);

foreach($xpath->query('//token[not(node())]') as $node)
{
    $node->parentNode->removeChild($node);
}

echo $xmlObj->asXML();

这将产生以下 XML首先修复未闭合的标签,然后删除空的 token 元素:

<?xml version="1.0" encoding="utf-8"?>
<response>
<url>http://xyz.com</url>
<token>xxxxxxx
</token>
</response>

相关:

The response you get from the API is not well-formed/valid XML:

<token>xxxxxxx<token>
              ^ missing /

As this is an API response you need to fix it prior simplexml can actually read it in. You can make use of the tidy extension Docs to solve this:

$config = Array(
    'input-xml' => 1,
);
$xml = tidy_repair_string($xml, $config);

$xmlObj = simplexml_load_string($xml);

$doc = dom_import_simplexml($xmlObj)->ownerDocument;

$xpath = new DOMXpath($doc);

foreach($xpath->query('//token[not(node())]') as $node)
{
    $node->parentNode->removeChild($node);
}

echo $xmlObj->asXML();

This will produce the following XML by first fixing unclosed tags and then removing empty token elements:

<?xml version="1.0" encoding="utf-8"?>
<response>
<url>http://xyz.com</url>
<token>xxxxxxx
</token>
</response>

Related:

强辩 2024-12-14 22:11:12

正如您正确指出的那样,CURLOPT_RETURNTRANSFER 设置为 1 非常重要!

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0 ); // set to 1 for debugging
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return from sms server

As you rightly pointed out, CURLOPT_RETURNTRANSFER set to 1 is very important!

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0 ); // set to 1 for debugging
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return from sms server
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文