note 45934 deleted from function.dom-domdocument-loadxml by didou

From: Date: Tue, 04 Jan 2005 10:17:10 +0000
Subject: note 45934 deleted from function.dom-domdocument-loadxml by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-82680@lists.php.net to get a copy of this message
Note Submitter: gem at rellim dot com ---- 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>

« previous php.notes (#82680) next »