我的代码从mysql数据集构建一个xml输出。然后我需要将其转换为HTML。我将它保存在一个php变量中,并尝试在 transformToXML($ XML)中使用它作为参数,但它提出它需要一个对象。如何将此xml输出转换为要在此处用作参数的对象。这是我的代码:
<?php
// i am using the resultset to build an XML DOM but you can do whatever you like with it !
header("Content-type: text/xml");
$conn = new mysqli("localhost", "babar", "k4541616", "spec", 3306);
// one non-recursive db call to get the message tree !
$result = $conn->query("call message_hier(1)");
//--$result = $conn->query("call message_hier_all()");
$xml = new DomDocument;
$xpath = new DOMXpath($xml);
$msgs = $xml->createElement("messages");
$xml->appendChild($msgs);
// loop and build the DOM
while($row = $result->fetch_assoc()){
$msg = $xml->createElement("message");
foreach($row as $col => $val) $msg->setAttribute($col, $val);
if(is_null($row["parent_msg_id"])){
$msgs->appendChild($msg);
}
else{
$qry = sprintf("//*[@msg_id = '%d']", $row["parent_msg_id"]);
$parent = $xpath->query($qry)->item(0);
if(!is_null($parent)) $parent->appendChild($msg);
}
}
$result->close();
$conn->close();
$save = $xml->saveXML();//-> Here i save mxl in php var
//echo $save;
$xslt = new XSLTProcessor();
$XSL = new DOMDocument();
$XSL->load( 'msg.xsl');
$xslt->importStylesheet( $XSL );
header('Content-Type: application/xml');
print $xslt->transformToXML($save); //->error: expects an object here, string given.
?>
答案 0 :(得分:0)
在使用之前,您需要通过DOMDocument::loadXML()
将XML字符串转换回对象。