在 PHP 中合并 XML [英] Merging XML in PHP

查看:22
本文介绍了在 PHP 中合并 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里出了点问题,似乎无法合并 XML DOMDocumet 我收到错误:致命错误:调用 C:\xampp\htdocs\xmltest\xmltest.php 中的未定义方法 DOMElement::importNode() 在线27" 谁能帮我把这段代码改正...

Somthing wrong here, dont seem to be able to merge XML DOMDocumet's I get the error, "Fatal error: Call to undefined method DOMElement::importNode() in C:\xampp\htdocs\xmltest\xmltest.php on line 27" can anybody help me put this code right...

<?php
// include required files
include 'XML/Query2XML.php';
include 'MDB2.php';

// prepare xml document
$dom = new DomDocument('1.0');

// create tables section
$tables = $dom->appendChild($dom->createElement('tables')); 

// create container for tblclients
$tblclients = $tables->appendChild($dom->createElement('tblclients')); 

try {
// initialize Query2XML object
$q2x = XML_Query2XML::factory(MDB2::factory('mysql://root:@localhost/db_solconmgr'));

// generate SQL query
$sql = "SELECT ClientID, Client, Contacts, 'Address Line 1' as AddressLine1, 'Address Line 2' as AddressLine2, City, County, 'Zip Code' as ZipCode, Telephone, Fax, Mobile, 'E-mail Address' as EmailAddress, Date FROM tblclients";

// get results as XML
$tblclientsXML = $q2x->getFlatXML($sql);

//$dom = $tblclientsXML;

$node = $tblclients->importNode($tblclientsXML, true);

// send output to browser
header('Content-Type: text/xml');
$dom->formatOutput = true;
echo $dom->saveXML();
} catch (Exception $e) {
echo $e->getMessage();
}
?>

推荐答案

importNode() 是属于文档的方法,而不是元素.您想导入文档 ($dom) 然后附加到元素 ($tblclients).

importNode() is a method belonging to the document, not an element. You want to import into the document ($dom) then append to the element ($tblclients).

导入时需要一个节点(并且$tblclientsXML 不是一个节点,它是一个文档)所以导入生成的XML 看起来类似 到:

A node is needed when importing (and $tblclientsXML is not a node, it's a document) so importing the generated XML would look similar to:

$tblclientsElement = $tblclientsXML->documentElement;
$tblclientsXML = $dom->importNode($tblclientsElement, TRUE);
$tblclients->appendChild($tblclientsXML);

这篇关于在 PHP 中合并 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆