php - 从 PSQL 结果集递归构建 XML(使用 PHP)

标签 php xml postgresql recursion

我正在使用以下代码成功构建 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");
        }
    }
}

我的问题是,递归执行此操作的最佳方法是什么?

最佳答案

$current = $root;
foreach($row as $fieldname => $fieldvalue) {
    $next = $current->append(new xmlElement($fieldname));
    $current->write($fieldvalue);
    $current = $next;
}

我觉得对象引用重新分配会把事情搞砸;如果它不起作用,请告诉我。

关于php - 从 PSQL 结果集递归构建 XML(使用 PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1103642/

相关文章:

Android:JSON 或 XML,缓存

sql子查询加入分组依据

php - 为什么不能在静态上下文中使用任何魔术常量?

php - 来自 mySQL 数据库的数据以 JSON 形式传递

java - 使用 apache xmlbeans 将命名空间定义添加到 xml

java - 如何在 Java 中解析 .plist 文件?

postgresql - 连接数据库后切换角色

postgresql - SQLAlchemy 和 postgres 反射 : erroneously reflecting all schemas

cookies - Cookie 仍设置在 HTTP 上,ini_set ('session.cookie_secure' ,1);

php - fatal error : Cannot use object of type MongoCursor as array