note 104640 added to xmlreader.read

From: Date: Mon, 27 Jun 2011 17:49:37 +0000
Subject: note 104640 added to xmlreader.read
Groups: php.notes 
Request: Send a blank email to php-notes+get-181004@lists.php.net to get a copy of this message
It is interesting to note that this function will stop on closing tags as well. I have an XML document similar to the following: <root> <columns> <column>columnX</column> <column>columnY</column> </columns> <table> <row> <columnX>38</columnX> <columnY>50</columnY> </row> <row> <columnX>82</columnY> <columnY>28</columnY> </row> ... </table> </root> I need to parse the <columns> object to know what attributes to check for from each <row> node. Therefore I was doing the following: <?php while ($xml->read()) { if ($xml->name === 'column') { //parse column node to into $columns array } elseif ($xml->name === 'row') { //parse row node, using constructed $columns array } } ?> This kind of worked in that I ended up with an array of all the data I wanted, but the array I constructed was twice as large as I expected and every other entry was empty. Took me a while to debug, but finally figured out that checking <?php $xml->name === 'row' ?> matches both <row> and </row>, so the check should really be something more like: <?php if ($xml->name === 'row' && $xml->nodeType == XMLReader::ELEMENT) { // parse row node } ?> I would have liked to use the next() function instead, but as I needed to parse 2 different subtrees, I couldn't figure out how to find all the columns, reset the pointer, and then find all the rows. If someone has a better solution than what I posted, I'd love to hear it! ---- Server IP: 69.147.83.197 Probable Submitter: 98.191.147.2 ---- Manual Page -- http://www.php.net/manual/en/xmlreader.read.php Edit -- https://master.php.net/note/edit/104640 Del: integrated -- https://master.php.net/note/delete/104640/integrated Del: useless -- https://master.php.net/note/delete/104640/useless Del: bad code -- https://master.php.net/note/delete/104640/bad+code Del: spam -- https://master.php.net/note/delete/104640/spam Del: non-english -- https://master.php.net/note/delete/104640/non-english Del: in docs -- https://master.php.net/note/delete/104640/in+docs Del: other reasons-- https://master.php.net/note/delete/104640 Reject -- https://master.php.net/note/reject/104640 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#181004) next »