开发者

PHP XMLREADER - QUESTION

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜