when i use simplexml_load_string, i find a question, lose data, after use it.
$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));
running result:
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
)
)
where is the data ': An Efficient Algorithm for the One-to-Some Shortest Path Problem on Road Maps.'?
i hope xml to array.but data lost?thanks.
i want the result:
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
)
)
)
The first rule of SimpleXMLElement is: you don't print_r() or var_dump() SimpleXMLElement Objects
As for why you can't see the information, see this note from the 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.
To access the title, you can do:
$xmlObj = simplexml_load_string($xml);
$title = (string) $xmlObj->inproceedings->title;
The properties of a SimpleXMLElement objects are objects themselves, so you need to cast them as strings by adding (string) at the beginning (or by using strval()). Now their values will be cast to a string instead of an object.
What should you use when you want to inspect SimpleXML objects?
You can use IMSoP's simplexml_dump() and simplexml_tree() helper functions. Here's the project on GitHub.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With