开发者

PHP XMLREADER - QUESTION

开发者 https://www.devze.com 2022-12-29 21:49 出处:网络
I am pretty new to xml parsing and I am using XMLREADER to parse a huge file. Given the following XML (sample):

I am pretty new to xml parsing and I am using XMLREADER to parse a huge file.

Given the following XML (sample):

<hotels>
 <hotel>
   <name>Bla1</name>
 </hotel>
 <hotel>
  <name>Bla2</name>
 </hotel>
</hotels>

And then the XMLREADER in PHP which pulls up the values to my Database:

$reader = new XMLReader();
$reader->open('hotels.xml');
while ($reader->read()) {

 if ($reader->n开发者_StackOverflow社区ame == "name") {

   $reader->read();
   mysql_query("INSERT INTO MyHotels (hotelName) VALUES (\"$reader->value\")");
 }

}
$reader->close();

The problem is that I've got a single empty row between each of the parsed nodes!! Not sure why this happens!

 | ID | hotelName |
 | 1  | Bla1      |
 | 2  |           |
 | 3  | Bla2      |
 | 4  |           |

Help is greatly appreciated.


Make sure that $reader->value is not empty if you want to avoid that:

 if ($reader->name == "name") {
   $reader->read();

   if ($reader->value) {
     mysql_query("INSERT INTO MyHotels (hotelName) VALUES (\"$reader->value\")");
   }
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号