I am building an XML document successfully with the following code:
我正在使用以下代码成功构建XML文档:
public function build($result) {
$root = $this->append(new xmlElement('data'));
$root->append(new xmlElement('collection'));
while($row = pg_fetch_assoc($result)){
foreach($row as $fieldname => $fieldvalue){
$second = $root->append(new xmlElement($fieldname));
$second->write($fieldvalue);
// $seconds_child = $second->append(new xmlElement('second child child'));
// $seconds_child->write("second's child content");
}
}
}
My question is, what is the best way to do this recursively?
我的问题是,递归执行此操作的最佳方法是什么?
1 个解决方案
#1
$current = $root;
foreach($row as $fieldname => $fieldvalue) {
$next = $current->append(new xmlElement($fieldname));
$current->write($fieldvalue);
$current = $next;
}
I have a feeling that object-reference reassignment will mess this up; if it doesn't work let me know.
我有一种感觉,对象引用重新分配会搞砸这个;如果它不起作用,请告诉我。
#1
$current = $root;
foreach($row as $fieldname => $fieldvalue) {
$next = $current->append(new xmlElement($fieldname));
$current->write($fieldvalue);
$current = $next;
}
I have a feeling that object-reference reassignment will mess this up; if it doesn't work let me know.
我有一种感觉,对象引用重新分配会搞砸这个;如果它不起作用,请告诉我。