MySQL PHP生成XML

时间:2017-05-18 18:30:14

标签: php mysql xml

团队我在从MySQL生成XML

时遇到问题

我需要具有此结构的XML

<markers>
<line width="4" html="CAR1" colour="#0000FF" label="CAR1" >
<point label="04:57PM" lat="32.75" lng="-117.27" html="CAR1" />
<point label="04:58PM" lat="32.72" lng="-117.33" html="CAR1" />
</line>
<line width="4" html="CAR2" colour="#0000FF" label="CAR2" >
<point label="04:53PM" lat="32.75" lng="-117.22" html="CAR2" />
<point label="04:54PM" lat="32.75" lng="-117.23" html="CAR2" />
<point label="04:55PM" lat="32.78" lng="-117.27" html="CAR2" />
</line>

<line width="4" html="IMSI3" colour="#0000FF" label="CAR3" >
<point label="04:53PM" lat="32.73" lng="-117.22" html="CAR3" />
<point label="04:54PM" lat="32.75" lng="-117.27" html="CAR3" />
<point label="04:55PM" lat="32.72" lng="-117.33" html="CAR3" />
<point label="04:56PM" lat="32.67" lng="-117.27" html="CAR3" />
<point label="04:57PM" lat="32.65" lng="-117.15" html="CAR3" />
<point label="04:58PM" lat="32.62" lng="-117.03" html="CAR3" />
</line>
</markers>

我真的让自己变成了一个网格

我已经使用了类似的东西

while ($row = @mysqli_fetch_assoc($result)){
  // Add to XML document node
  $node = $dom->createElement("markers");
  $newnode = $parnode->appendChild($node);
      $newnode->setAttribute("car",$row['car']);
      $newnode->setAttribute("timestamp", $row['timestamp']);
      $newnode->setAttribute("lat", $row['lat']);
      $newnode->setAttribute("lng", $row['lng']);
      $newnode->setAttribute("label", $row['label']);
}

在通过数据库运行时,每次LABEL更改时如何进行内部操作?请帮忙!!

我设法得到了类似的东西

//while($row =  @mysqli_fetch_assoc($resultactivos)) {
//    $activos[$index] = $row;
//  $index++;
}
//foreach($activos as $nombre){
//  $node=$dom->createElement("makers");
//  $newnode = $parnode->appendChild($node);
//  $newnode->setAttribute("activo",$nombre);
//  $query = "SELECT * from recorrido WHERE activo = '$nombre'";
//  $result = mysqli_query($connection,$query);
//  while ($row = @mysqli_fetch_assoc($result)){
//          // Add to XML document node
//          $newnode->setAttribute("tiempo", $row['timestamp']);
//          $newnode->setAttribute("lat", $row['lat']);
//          $newnode->setAttribute("lng", $row['lng']);
//          $newnode->setAttribute("car", $row['car']);
//}
//}

但仍然悲惨地罢了......

1 个答案:

答案 0 :(得分:0)

在开始新元素之前,针对之前的值添加测试。

# Initialize Empty Var for Label Comparison
$prev_label = "";
while ($row = @mysqli_fetch_assoc($result)){
  # New Node if Label Changes
  if ($prev_label != $row['label']) {
    // Add to XML document node
    $node = $dom->createElement("markers");
  }
  $prev_label = $row['label'];

  # Add child to Node
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("car",$row['car']);
  $newnode->setAttribute("timestamp", $row['timestamp']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("label", $row['label']);
}