PHP DOM代码将子项添加到子项中,而不是将所有子项添加到与兄弟项目相同的父项

时间:2018-03-12 19:18:54

标签: php xml appendchild

我已经从here复制了代码,用一个父(标记)和多个兄弟(标记)创建xml。我复制的代码是:

    // Start XML file, create parent node
$dom = new DOMDocument('1.0');
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
//header("Content-type: text/xml");

// Select all the rows in the markers table
$SQL = "SELECT * FROM markers";
$query = $this->db->query($SQL);
if ($query->num_rows() > 0) {
    // Iterate through the rows, adding XML nodes for each
    foreach ($query->result_array() as $row) {
      // Add to XML document node
      $node = $dom->createElement("marker");
      $newnode = $parnode->appendChild($node);
      $newnode->setAttribute("id",$row['id']);
      $newnode->setAttribute("name",$row['name']);
      $newnode->setAttribute("address", $row['address']);
      $newnode->setAttribute("lat", $row['lat']);
      $newnode->setAttribute("lng", $row['lng']);
      $newnode->setAttribute("type", $row['type']);
    }

}

示例显示了这样的输出,这就是我想要的,其中所有的孩子都是兄弟姐妹:

<markers>
<marker id="1" name="Love.Fish" address="580 Darling Street, Rozelle, NSW" lat="-33.861034" lng="151.171936" type="restaurant"/>
<marker id="2" name="Young Henrys" address="76 Wilford Street, Newtown, NSW" lat="-33.898113" lng="151.174469" type="bar"/>
<marker id="3" name="Hunter Gatherer" address="Greenwood Plaza, 36 Blue St, North Sydney NSW" lat="-33.840282" lng="151.207474" type="bar"/>
<marker id="4" name="The Potting Shed" address="7A, 2 Huntley Street, Alexandria, NSW" lat="-33.910751" lng="151.194168" type="bar"/>
<marker id="5" name="Nomad" address="16 Foster Street, Surry Hills, NSW" lat="-33.879917" lng="151.210449" type="bar"/>
<marker id="6" name="Three Blue Ducks" address="43 Macpherson Street, Bronte, NSW" lat="-33.906357" lng="151.263763" type="restaurant"/>
<marker id="7" name="Single Origin Roasters" address="60-64 Reservoir Street, Surry Hills, NSW" lat="-33.881123" lng="151.209656" type="restaurant"/>
<marker id="8" name="Red Lantern" address="60 Riley Street, Darlinghurst, NSW" lat="-33.874737" lng="151.215530" type="restaurant"/>
</markers>

但我得到了这个,其中marker8是marker7的孩子,它是marker6的孩子,.........并且只有marker1是标记的直接子项:

   <markers>
  <marker id="1" name="Love.Fish" address="580 Darling Street, Rozelle, NSW" lat="-33.861034" lng="151.171936" type="restaurant">
     <marker id="2" name="Young Henrys" address="76 Wilford Street, Newtown, NSW" lat="-33.898113" lng="151.174469" type="bar">
        <marker id="3" name="Hunter Gatherer" address="Greenwood Plaza, 36 Blue St, North Sydney NSW" lat="-33.840282" lng="151.207474" type="bar">
           <marker id="4" name="The Potting Shed" address="7A, 2 Huntley Street, Alexandria, NSW" lat="-33.910751" lng="151.194168" type="bar">
              <marker id="5" name="Nomad" address="16 Foster Street, Surry Hills, NSW" lat="-33.879917" lng="151.210449" type="bar">
                 <marker id="6" name="Three Blue Ducks" address="43 Macpherson Street, Bronte, NSW" lat="-33.906357" lng="151.263763" type="restaurant">
                    <marker id="7" name="Single Origin Roasters" address="60-64 Reservoir Street, Surry Hills, NSW" lat="-33.881123" lng="151.209656" type="restaurant">
                       <marker id="8" name="Red Lantern" address="60 Riley Street, Darlinghurst, NSW" lat="-33.874737" lng="151.215530" type="restaurant" />
                    </marker>
                 </marker>
              </marker>
           </marker>
        </marker>
     </marker>
  </marker>

我如何纠正这一点,以便所有“标记”都是“标记”的兄弟姐妹和直接孩子?

谢谢!

0 个答案:

没有答案