Click to See Complete Forum and Search --> : [RESOLVED] HTMLEntities Warning: DOMDocument::load()


Nibinaear
March 2nd, 2008, 02:40 PM
I'm trying to put html into xml and then print it out at different places and I'm finding it a bit difficult. For starters xml doesn't support html tags as far as I can see, which means that I've got to encode them so that they will work. I'm using htmlentities function in php to do this and then using html_entity_decode when parsing the xml back out. The only problem with this is that I keep receiving errors like:


Warning: DOMDocument::load() [function.DOMDocument-load]: Entity 'Acirc' not defined in file:///C%3A/webpages/rspca/xml/about.xml, line: 17 in C:\webpages\rspca\public_html\xml.php on line 13
\r\n
Warning: DOMDocument::load() [function.DOMDocument-load]: Entity 'pound' not defined in file:///C%3A/webpages/rspca/xml/about.xml, line: 17 in C:\webpages\rspca\public_html\xml.php on line 13
\r\n


All I entered was a pound symbol like this £ and it tried to put in acirc (whatever that is) and pound (which is what it should have done.) The weirdest part is when the decode function comes along, because it doesn't even understand the pound entity.

This is how I'm using my decode function:


html_entity_decode($details[$i],ENT_QUOTES,"ISO-8859-1");

PeejAvery
March 2nd, 2008, 10:43 PM
Why don't you just use CDATA? CDATA will not be parsed by any XML reader.

Nibinaear
March 3rd, 2008, 05:31 AM
Of course. It's been a while since I learnt XML soI didn't consider such things. That'll probably save me a lot of work thanks.

Nibinaear

Edited: And it works. Only someone like me would go to all the effort of encoding my html to put in xml.

PeejAvery
March 3rd, 2008, 07:56 AM
Only someone like me would go to all the effort of encoding my html to put in xml.
I did it a very long time ago, and that's why I understood what you needed.

Nibinaear
March 3rd, 2008, 08:11 AM
Now I'm just trying to work out how to alter elements in XML in the same way. All I can find are the:

DOMCharacterData->appendData() - Append a string to the end of the character data of the node

DOMCharacterData->deleteData() - Remove a range of characters from the node

DOMCharacterData->insertData() - Insert a string at the specified 16-bit unit offset

DOMCharacterData->replaceData() - Replace a substring within the DOMCharacterData node

DOMCharacterData->substringData()

functions. All of these imply that you are altering specific data of a textnode, not acually just replacing the entire node with new data. I've searched google about 40 times and found nothing that will help.

EDITED

You know I searched the entire web and got nothing. It isn't detailed anywhere how to change xml elements with php, only how to append new elements.

So here it is, the defninitive way to "Change XML elements with PHP" rather than adding / appending new ones. This uses XPATH:



// Create a DOMDocument instance
$xml = new DOMDocument;

// Ignore whitespace between nodes (default: true)
$xml->preserveWhiteSpace = false;

$file='about.xml';

// Load the XML data source
$xml->Load($file);

$xpath = new DOMXPath($xml);

$query='/regions/branch';

$entries = $xpath->query($query);

foreach ($entries as $entry)
{
$entry->firstChild->nodeValue="like this!";
echo $entry->firstChild->nodeValue;
}

$xml->save($file);//you must always save your dom back to file.