note 104640 modified in xmlreader.read by danbrown

From: Date: Mon, 27 Jun 2011 18:05:21 +0000
Subject: note 104640 modified in xmlreader.read by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181005@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. --was-- 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! http://php.net/manual/en/xmlreader.read.php

« previous php.notes (#181005) next »