note 29551 deleted from ref.xml by danbrown

From: Date: Thu, 30 Jun 2011 13:35:27 +0000
Subject: note 29551 deleted from ref.xml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181094@lists.php.net to get a copy of this message
Note Submitter: software at serv-a-com dot com ---- 2. Pre Parser Strings and New Line Delimited Data One important thing to note at this point is that the xml_parse function requires a string variable. You can manipulate the content of any string variable easily as we all know. A better approach to removing newlines than: <?php while ($data = fread($fp, 4096)) { $data = preg_replace("/\n|\r/","",$data); //flarp if (!xml_parse($xml_parser, $data, feof($fp))) {... ?> Above works across all 3 line-delimited text files (\n, \r, \r\n). But this could potentially (or will most likely) damage or scramble data contained in for example CDATA areas. As far as I am concerned end of line characters should not be used _within_ XML tags. What seems to be the ultimate solution is to pre-parse the loaded data this would require checking the position within the XML document and adding or subtracting (using a in-between fread temporary variable) data based on conditions like: "Is within tag", "Is within CDATA" etc. before fedding it to the parser. This of course opens up a new can of worms (as in parse data for the parser...). (above procedure would take place between fread and xml_parser calls this method would be compatible with the general usage examples on top of the page) 3. The Answer to parsing arbitrary XML and Preprocessor Revisited You can't just feed any XML document to the parser you constructed and assuming that it will work! You have to know what kind of methods for storing data are used, for example is there a end of line delimited data in the file ?, Are there any carriage returns in the tags etc... XML files come formatted in different ways some are just a one long string of characters with out any end of line markers others have newlines, carriage returns or both (Microsloth Windows). May or may not contain space and other whitespace between tags. For this reason it is important to what I call Normalize the data before feeding it to the parser. You can perform this with regular expressions or plain old str_replace and concatenation. In many cases this can be done to the file it self sometimes to string data on the fly( as shown in the example above). But I feel it is important to normalize the data before even calling the function to call xml_parse. If you have the ability to access all data before that call you can convert it to what you fell the data should have been in the first place and omit many surprises and expensive regular expression substitution (in a tight spot) while fread'ing the data.

« previous php.notes (#181094) next »