0

I have a string field in database with 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 I try:

$xml = new SimpleXMLElement($field);

I am getting exception:

String could not be parsed as XML

I validated XML in Notepad++ and it says "No Error Detected". I really must have this XML value in some field from that string field. Where is my problem? Thank you

Update: My field now 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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Veljko
  • 1,708
  • 12
  • 40
  • 80

2 Answers2

1
$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 this function for 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 . ' at line ' . $error->line . '. Bad XML: ' . htmlentities($badxml);
   return $message;
}

sample echo isXML($filed);

mohammad mohsenipur
  • 3,218
  • 2
  • 17
  • 22
-1

I have as well valid XML in a database (and also in some files on disk). I can send it to you (if you really want to see it). However only having valid XML somewhere is totally useless in the context of your code:

$xml = new SimpleXMLElement($field);

It is much more important with that string that is inside the $field variable you've got is valid XML. And if you had problems to understand the error message: You do not have any valid XML in $field.

The only thing you need to do is to put valid XML into that variable.

That works best by finding out what exactly is inside that variable first. You can then compare that with your expecatations, you'll probably immediatly spot the problem then and fix it.

HTH.

(Hint: If you still hit the wall after you've found out, provide a hex-dump of $field).

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836