It is important that you select the correct method of loading the XML in depending on the format of your XML file. If you use the wrong function to load it in you will get some strange errors.
To take care of proper error checking, one must use try-catch blocks: <?php try { $sxe = new SimpleXMLElement($xmlstring); } catch (Exception $e) { echo "bad xml"; } ?> To get rid of warnings, use @ in front of new: <?php try { $sxe = @new SimpleXMLElement($xmlstring); } catch (Exception $e) { echo "bad xml"; } ?>
Instead of using <?php eval($code) ?> you can use this method, <?php $node = "node"; $XML = simplexml_load_file($file); $text = (string)$XML->nodes->{$node}; // works // or $text2 = (string)$XML->{$node}->child; // also works fine ?> I think it is much more easier instead of eval().
When using simplexml to access a element the returned object may be a SimpleXMLElement instead of a string. Example: <?php $string = <<<XML <?xml version='1.0'?> <document> <cmd>login</cmd> <login>Richard</login> </document> XML; $xml = simplexml_load_string($string); print_r($xml); $login = $xml->login; print_r($login); $login = (string) $xml->login; print_r($login); ?> Expected result: ---------------- SimpleXMLElement Object ( [cmd] => login [login] => Richard ) Richard Richard Actual result: -------------- SimpleXMLElement Object ( [cmd] => login [login] => Richard ) SimpleXMLElement Object ( [0] => Richard ) Richard But this is an intended behavior. See http://bugs.php.net/bug.php?id=29500
Коментарии
It is important that you select the correct method of loading the XML in depending on the format of your XML file.
If you use the wrong function to load it in you will get some strange errors.
To take care of proper error checking, one must use try-catch blocks:
<?php
try {
$sxe = new SimpleXMLElement($xmlstring);
} catch (Exception $e) {
echo "bad xml";
}
?>
To get rid of warnings, use @ in front of new:
<?php
try {
$sxe = @new SimpleXMLElement($xmlstring);
} catch (Exception $e) {
echo "bad xml";
}
?>
Instead of using <?php eval($code) ?>
you can use this method,
<?php
$node = "node";
$XML = simplexml_load_file($file);
$text = (string)$XML->nodes->{$node}; // works
// or
$text2 = (string)$XML->{$node}->child; // also works fine
?>
I think it is much more easier instead of eval().
When using simplexml to access a element the returned object may be a SimpleXMLElement instead of a string.
Example:
<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
<cmd>login</cmd>
<login>Richard</login>
</document>
XML;
$xml = simplexml_load_string($string);
print_r($xml);
$login = $xml->login;
print_r($login);
$login = (string) $xml->login;
print_r($login);
?>
Expected result:
----------------
SimpleXMLElement Object
(
[cmd] => login
[login] => Richard
)
Richard
Richard
Actual result:
--------------
SimpleXMLElement Object
(
[cmd] => login
[login] => Richard
)
SimpleXMLElement Object
(
[0] => Richard
)
Richard
But this is an intended behavior. See http://bugs.php.net/bug.php?id=29500