gpt4 book ai didi

php - simplexml_load_string 有错误?

转载 作者:数据小太阳 更新时间:2023-10-29 02:30:25 25 4
gpt4 key购买 nike

当我使用 simplexml_load_string 时,我发现一个问题,使用后丢失数据。

$xml = '<dblp>
<inproceedings key="conf/aaim/He07" mdate="2007-06-28">
<author>Dan He</author>
<title>
<i>BMA</i>
<sup>*</sup>
: An Efficient Algorithm for the One-to-Some Shortest Path Problem on Road Maps.
</title>
<pages>346-357</pages>
<year>2007</year>
<crossref>conf/aaim/2007</crossref>
<booktitle>AAIM</booktitle>
<ee>http://dx.doi.org/10.1007/978-3-540-72870-2_33</ee>
<url>db/conf/aaim/aaim2007.html#He07</url>
</inproceedings>
</dblp>';
print_r(simplexml_load_string($xml));

运行结果:

  SimpleXMLElement Object
(
[inproceedings] => SimpleXMLElement Object
(
[@attributes] => Array
(
[key] => conf/aaim/He07
[mdate] => 2007-06-28
)

[author] => Dan He
[title] => SimpleXMLElement Object
(
[i] => BMA
[sup] => *
)

[pages] => 346-357
[year] => 2007
[crossref] => conf/aaim/2007
[booktitle] => AAIM
[ee] => http://dx.doi.org/10.1007/978-3-540-72870-2_33
[url] => db/conf/aaim/aaim2007.html#He07
)

)

数据在哪里“:道路 map 上一对多最短路径问题的有效算法。”?
我希望 xml 到 array.but 数据丢失?谢谢。我想要结果:

Array
(
[0] => Array
(
[inproceedings] =>Array
(
[author] => Dan He
[title] => Array
(
[0] => BMA
[2] => *
[5] => : An Efficient Algorithm for the One-to-Some Shortest Path Problem on Road Maps.
)

[pages] => 346-357
[year] => 2007
[crossref] => conf/aaim/2007
[booktitle] => AAIM
[ee] => http://dx.doi.org/10.1007/978-3-540-72870-2_33
[url] => db/conf/aaim/aaim2007.html#He07
)

)

)

最佳答案

SimpleXMLElement 的第一条规则是:您不要 print_r()var_dump() SimpleXMLElement 对象

至于为什么看不到信息,看这篇来自documentation的说明:

Note: SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump() or anything else which can examine objects.

要访问标题,您可以执行以下操作:

$xmlObj = simplexml_load_string($xml);
$title = (string) $xmlObj->inproceedings->title;

SimpleXMLElement 对象的属性本身就是对象,因此您需要通过在开头添加 (string) (或使用 strval() )将它们转换为字符串。现在它们的值将被转换为字符串而不是对象。

当您想要检查 SimpleXML 对象时应该使用什么?

您可以使用 IMSoPsimplexml_dump()simplexml_tree() 辅助函数。这是 project on GitHub .

关于php - simplexml_load_string 有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21660547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com