PHP创建递归文档
我试图从数据库表中创建以下XML。这是XMLPHP创建递归文档,php,xml,recursion,Php,Xml,Recursion,我试图从数据库表中创建以下XML。这是XML <groups> <group id="1" name="DEBUG"/> <group id="2" name="ORSP"> <group id="3" name="PRE"> <group id="4" name="TRANS"/> <group id="5" name="OPP"
<groups>
<group id="1" name="DEBUG"/>
<group id="2" name="ORSP">
<group id="3" name="PRE">
<group id="4" name="TRANS"/>
<group id="5" name="OPP"/>
</group>
<group id="6" name="POST">
<group id="7" name="DGM"/>
</group>
</group>
</groups>
这是我目前的代码。我试图返回一个domeElement,但是我永远无法得到上面的XML。使用字符串,我能够生成XML。欢迎任何建议
public function process(){
$xml = '<groups>';
$xml .= $this->getAllGroups();
$xml .= '</groups>';
return $xml;
}
public function getAllGroups($groupID=null){
$where = null;
$placeholder = array();
$xml = null;
if (is_null($groupID)){
$where = " GROUP_PARENT IS NULL ";
}
else {
$where = " GROUP_PARENT=?";
$placeholder=array($groupID);
}
$sql = "SELECT
GROUP_ID,
GROUP_NAME
FROM
GROUPS
WHERE
$where";
$data = $this->dbh->executeSql($sql,$placeholder);
foreach ($data["records"] as $row) {
$groupID = $row["GROUP_ID"];
$groupName = $row["GROUP_NAME"];
//$groupElement = $this->xml->createElement("group");
//$groupElementID = $this->xml->createAttribute("id");
//$groupElementName = $this->xml->createAttribute("name");
//$groupElementID->value = $groupID;
//$groupElementName->value = $groupName;
//$groupElement->appendChild($groupID);
//$groupElement->appendChild($groupName);
//$passedXML->appendChild($groupElement);
//$this->getAllGroups($groupID);
$xml .= "<group id='$groupID' name='$groupName'>";
$xml .= $this->getAllGroups($groupID);
$xml .= "</group>";
}
return $xml;
}
在玩了代码之后,我神奇地找到了一个解决方案。我的头在墙上撞了一天半。希望这能在将来帮助别人
public function process(){
$xml = $this->getAllGroups();
return $xml;
}
public function getAllGroups(\DOMElement &$xml=null,$groupID=null){
$where = null;
$placeholder = array();
if (is_null($groupID)){
$where = " GROUP_PARENT IS NULL ";
$xml = $this->xml->createElement("groups");
}
else {
$where = " GROUP_PARENT=?";
$placeholder=array($groupID);
}
$sql = "SELECT
GROUP_ID,
GROUP_NAME
FROM
GROUPS
WHERE
$where";
$data = $this->dbh->executeSql($sql,$placeholder);
foreach ($data["records"] as $row) {
$groupID = $row["GROUP_ID"];
$groupName = $row["GROUP_NAME"];
$groupElement = $this->xml->createElement("group");
$groupElementID = $this->xml->createAttribute("id");
$groupElementName = $this->xml->createAttribute("name");
$groupElementID->value = $groupID;
$groupElementName->value = $groupName;
$groupElement->appendChild($groupElementID);
$groupElement->appendChild($groupElementName);
$xml->appendChild($groupElement);
$this->getAllGroups($groupElement,$groupID);
}
return $xml;
}
我认为你应该发布你想要修复的代码,而不是有效的代码。
public function process(){
$xml = $this->getAllGroups();
return $xml;
}
public function getAllGroups(\DOMElement &$xml=null,$groupID=null){
$where = null;
$placeholder = array();
if (is_null($groupID)){
$where = " GROUP_PARENT IS NULL ";
$xml = $this->xml->createElement("groups");
}
else {
$where = " GROUP_PARENT=?";
$placeholder=array($groupID);
}
$sql = "SELECT
GROUP_ID,
GROUP_NAME
FROM
GROUPS
WHERE
$where";
$data = $this->dbh->executeSql($sql,$placeholder);
foreach ($data["records"] as $row) {
$groupID = $row["GROUP_ID"];
$groupName = $row["GROUP_NAME"];
$groupElement = $this->xml->createElement("group");
$groupElementID = $this->xml->createAttribute("id");
$groupElementName = $this->xml->createAttribute("name");
$groupElementID->value = $groupID;
$groupElementName->value = $groupName;
$groupElement->appendChild($groupElementID);
$groupElement->appendChild($groupElementName);
$xml->appendChild($groupElement);
$this->getAllGroups($groupElement,$groupID);
}
return $xml;
}