1
votes

I am trying to load and xml file (that's created through a web form) into a php page. The directories are created as products get added and an XML written for each product which is to later be called back by the page.

This is the error that I get.

Warning: DOMDocument::load() [domdocument.load]: Entity 'nbsp' not defined in /home/content/41/11699441/html/nc/products/belly-dump/234efg/data.xml, line: 4 in /home/content/41/11699441/html/nc/includes/_xml.php on line 4

Here's the XML loading code:

  1. $xmlDoc = new DOMDocument();
  2. $xmlDoc->load('data.xml');

XML File:

<?xml version="1.0"?>
<xml>
<model>
    <type>Gravel Box</type>
    <titleXML>345FGH - Tridem Clamshell</titleXML>
    <features>here are features</features>
    <options>here are options</options>
    <gallery>my photo gallery directory</gallery>
    <info>additional information</info>
</model>
</xml>

PHP Code writing the XML Doc

`$xml = new SimpleXMLElement('');

$model = $xml->addChild('model');
$model->addChild('type', $type);
$model->addChild('titleXML', $titleXML);
$model->addChild('features', $feature);
$model->addChild('options', $option);
$model->addChild('gallery', $gallery);
$model->addChild('info', $info);

$xml->saveXML($target_Path.'data.xml');`

1
Can you include sample XML in your question? - Amal Murali
Yes, we would need to see the XML. Also, have you tried SimpleXMLElement()? - kwolfe
I have tried SimpleXMLElement, loadHTML, load and I keep getting errors. - eklassen
DOMDocument::load() does only process valid XML. You can make use of the $doc->recover = TRUE; switch which will the drop unknown characters (e.g. those with undefined entity names like &nbsp;). Alternatively if you know that the string is somewhat well-formed you can process entities your own but take care about cdata sections. - hakre
@Eric Klassen: Can you please review your question? The XML you've added there does not contain the &nbsp; entity. This looks pretty wrong in context of your error message. Please double-check and correct as needed. - hakre

1 Answers

0
votes

You can use DomDocument::loadHTML(); for your problem. The entity   is not within the known XML entities. All entities in XML have to be numeric entities except of < & " > and '. You XML file seems not to be a valid XML file. So you can load it with the loadHTML Method, that has mostly no problems with invalid XML.

The best solution would be, that the data.xml file is valid. So if you can, just get the writing process instead of the reading process fixed.