这里有些错误,似乎无法合并XML DOMDocumet,这是我收到的错误,“致命错误:在第27行的C:\ xampp \ htdocs \ xmltest\ xmltest.php中调用未定义的方法DOMElement :: importNode()”可以任何人都可以帮助我正确编写此代码…
<?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()
$dom
$tblclients
导入时需要一个 节点 (而$tblclientsXML不是一个节点,它是一个文档),因此导入生成的XML看起来 类似于 :
$tblclientsXML
$tblclientsElement = $tblclientsXML->documentElement; $tblclientsXML = $dom->importNode($tblclientsElement, TRUE); $tblclients->appendChild($tblclientsXML);