从表中获取数据并将其保存在XML文件中

时间:2014-01-17 08:12:13

标签: mysql xml

我从mySQL表中获取一组记录,然后将其存储在XML文件中,如下所示:

 while($rows = mysqli_fetch_array($result))
 {
 $deviceNumber=$rows['deviceid'];
 $videoNumber=$rows['videoid'];
 $paymentDate=$rows['paymentdate'];
 $paymentTime=$rows['timenow'];

 $xml = new DOMDocument();
 $xmlroot = $xml->createElement("xml");
 $payments = $xml->createElement("payments");
 $deviceid = $xml->createElement("deviceid", $deviceNumber);
 $payments->appendChild( $deviceid );
 $videonumber = $xml->createElement("videonumber");
 $videoid = $xml->createElement("videoid", $videoNumber);
 $payments->appendChild( $videoid );
 $paymentdate = $xml->createElement("paymentdate");
 $paiddate = $xml->createElement("paiddate", $paymentDate);
 $payments->appendChild( $paiddate );
 $paymenttime = $xml->createElement("paymenttime");
 $timenow = $xml->createElement("timenow", $paymentTime);
 $payments->appendChild( $timenow );
 $xmlroot->appendChild( $payments );
 $xml->appendChild( $xmlroot );

//Save the XML file
 $xml->save("userPaymentDetails.xml");
  }

上述方法存在的问题是,它只保存一条记录,如下所示:

 <xml>
 <payments>
 <deviceid>43CA3KZXYQGBK</deviceid>
 <videoid>1234567</videoid>
 <paiddate>2014-01-09</paiddate>
 <timenow>19:38:19</timenow>
 </payments>
 </xml>

我想在XML文件中有多个值。 我想实现:

 <xml>
 <payments>
 <deviceid>43CA3KZXYQGBK</deviceid>
 <videoid>1234567</videoid>
 <paiddate>2014-01-09</paiddate>
 <timenow>19:38:19</timenow>
 </payments>
 <payments>
 <deviceid>76QL6DFBAQHJL</deviceid>
 <videoid>1348921</videoid>
 <paiddate>2014-01-10</paiddate>
 <timenow>10:20:12</timenow>
 </payments>     
 <payments>
 <deviceid>93CH3GHTYQGPM</deviceid>
 <videoid>890345</videoid>
 <paiddate>2014-01-15</paiddate>
 <timenow>14:32:40</timenow>
 </payments>
 </xml>

我做错了什么?

2 个答案:

答案 0 :(得分:0)

您可以在while循环中创建XML文件,以便每个周期覆盖现有数据。你应该在循环之外创建xml(XMLDocument和root元素)并在循环之后保存它。

答案 1 :(得分:0)

您是否尝试过保存$ xml-&gt; save(“userPaymentDetails.xml”);和$ xml = new DOMDocument();跳出循环。如果是,那么会发生什么。