php simplexml_load_file and CDATA has data missing completely
Get the solution ↓↓↓I have the following xml file
<?xml version="1.0" encoding="UTF-8"?>
<data>
<item name="general.global.Event"><![CDATA[EVENT!]]></item>
<item name="general.global.CompanyName"><![CDATA[some name]]></item>
<item name="general.global.CompanyImprint"><![CDATA[Legal information]]></item>
</data>
and my code is as follows
$xml = simplexml_load_file("general.xml") or die("Error: Cannot create object");
print_r($xml);
and my output is missing the CDATA.. how?
SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => general.global.Event
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => general.global.CompanyName
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[name] => general.global.CompanyImprint
)
)
)
)
Answer
Solution:
Text nodes are not exposed withprint_r
.
You can see the data there is you look at it explicitly:
print $xml->item[0];
Answer
Solution:
The CDATA is being read, read this answer and you'll see that if youprint_r($xml->asXML())
; The parser recompiles the CDATA information just fine.
For some reason, PHP'svar_dump
andprint_r
don't have accurate representation of XML objects. Try this and you can still access the data:
foreach ($xml->item as $item) {
if ('general.global.CompanyImprint' === (string)$item['name']) {
var_dump((string)$item);
}
}
// prints
string(17) "Legal information"
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: target class [commandmakecommand] does not exist.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.