note 45934 added to function.dom-domdocument-loadxml

From: Date: Thu, 23 Sep 2004 22:56:33 +0000
Subject: note 45934 added to function.dom-domdocument-loadxml
Groups: php.notes 
Request: Send a blank email to php-notes+get-77269@lists.php.net to get a copy of this message
Here is an example of loading DocBook XML from a string and saving it nicely formated to a file, with no warnings. It omits proper return code checking for errors to make it easier to read. $txt =<<<EOT <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> <article><title>Why&nbsp;?</title></article> EOT; # create empty DomDocument so flags can be set # before loading xml $xml = new DOMDocument(); # must set before the load() # otherwise formatOutput below will not work $xml->preserveWhiteSpace = FALSE; # must be set before load # without this you get warning: # Entity 'nbsp' not defined in Entity, line: 4 # &nbsp; is legal DocBook, bit not legal XML until # the DOCTYPE is parsed $xml->resolveExternals = TRUE; # the above line may make things REALLY slow unless # your local catalogs are setup well # now load the xml text $xml->loadXML( $txt ); # do any editing on $xml here # probably not needed, but ensures no adjacent #text nodes # that might have been created by operations on $xml $xml->normalize(); # we want it nice and pretty $xml->formatOutput = TRUE; # save to file to finish $xml->save('sample.xml'); The saved file looks like this: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"> <article> <title>Why&nbsp;?</title> </article> ---- Manual Page -- http://www.php.net/manual/en/function.dom-domdocument-loadxml.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45934 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45934&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45934&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#77269) next »