simplexml - PHP - String could not be parsed as XML when using SimpleXMLElement -
this question has answer here:
- “string not parsed xml” php error 1 answer
i have string field in database value:
<productorderitem> <product> <productoffering> <id>1</id> </productoffering> <componentproduct> <productoffering> <id>10</id> </productoffering> <characteristicvalue> <characteristic> <name>color</name> </characteristic> <value>black</value> </characteristicvalue> <characteristicvalue> <characteristic> <name>imei</name> </characteristic> <value>imei100</value> </characteristicvalue> </componentproduct> <componentproduct> <productoffering> <id>11</id> </productoffering> <characteristicvalue> <characteristic> <name>msisdn</name> </characteristic> <value>063</value> </characteristicvalue> <characteristicvalue> <characteristic> <name>imsi</name> </characteristic> <value>064</value> </characteristicvalue> </componentproduct> </product> </productorderitem>
when try:
$xml = new simplexmlelement($field);
i getting exception:
string not parsed xml
i validated xml in notepad++ , says "no error detected". must have xml value in field string field. problem? thank you
update: field has value:
<?xml version="1.0" encoding="utf-8"?> <productorderitem><product><productoffering><id>1</id></productoffering><componentproduct><productoffering><id>10</id></productoffering><characteristicvalue><characteristic><name>color</name></characteristic><value>black</value></characteristicvalue><characteristicvalue><characteristic><name>imei</name></characteristic><value>imei100</value></characteristicvalue></componentproduct><componentproduct><productoffering><id>11</id></productoffering><characteristicvalue><characteristic><name>msisdn</name></characteristic><value>063</value></characteristicvalue><characteristicvalue><characteristic><name>imsi</name></characteristic><value>064</value></characteristicvalue></componentproduct></product></productorderitem>
but still same exception.
$filed='<?xml version="1.0" encoding="utf-8"?> <productorderitem> <product> <productoffering> <id>1</id> </productoffering> <componentproduct> <productoffering> <id>10</id> </productoffering> <characteristicvalue> <characteristic> <name>color</name> </characteristic> <value>black</value> </characteristicvalue> <characteristicvalue> <characteristic> <name>imei</name> </characteristic> <value>imei100</value> </characteristicvalue> </componentproduct> <componentproduct> <productoffering> <id>11</id> </productoffering> <characteristicvalue> <characteristic> <name>msisdn</name> </characteristic> <value>063</value> </characteristicvalue> <characteristicvalue> <characteristic> <name>imsi</name> </characteristic> <value>064</value> </characteristicvalue> </componentproduct> </product> </productorderitem>'; $xml = new simplexmlelement($filed); print_r($xml );
use function check xml
function isxml($xml){ libxml_use_internal_errors(true); $doc = new domdocument('1.0', 'utf-8'); $doc->loadxml($xml); $errors = libxml_get_errors(); if(empty($errors)){ return true; } $error = $errors[0]; if($error->level < 3){ return true; } $explodedxml = explode("r", $xml); $badxml = $explodedxml[($error->line)-1]; $message = $error->message . ' @ line ' . $error->line . '. bad xml: ' . htmlentities($badxml); return $message; }
sample echo isxml($filed);
Comments
Post a Comment