可捕获的致命错误:传递给DOMDocument :: saveXML()的参数1必须是DOMNode的一个实例,字符串

时间:2017-09-08 14:52:56

标签: php xml

当没有像orders.xml这样的现有XML时,将数据保存到XML时出现问题。但是当存在现有文件时它会保存。它显示

  

捕获致命错误:传递给DOMDocument :: saveXML()的参数1必须是DOMNode的一个实例,在第75行的C:\ xampp \ htdocs \ CWP413 \ MidtermExam \ buy.php中给出的字符串“...

第一行是用于从另一种形式购买产品来加载表单。

这是我的代码:

// script by candy21  
$products =simplexml_load_file("product.xml") or die("ERROR");

foreach ($products->product as $product){
        if($product['id']==$_GET['id']){
            $id = $product['id'];
            $name =$product->name;
            $price = $product->price;
            break;
        }

    }


if(isset($_POST['submitSave'])){
    $quan = $_POST['quan'];

    $total = $price * $quan;

    $xml = new DOMDocument("1.0","UTF-8");
    $xml -> preserveWhiteSpace = TRUE;
    $xml -> formatOutput = true;

    if(file_exists("orders.xml")){
        $xml = simplexml_load_file('orders.xml');
        $order = $xml->addChild('order');
        $order->addChild('id', $id);
        $order->addChild('name', $name);
        $order->addChild('price', $price);
        $order->addChild('quan', $quan);
        $order->addChild('total', $total);

        //echo "<xmp>" .$xml -> saveXML(). "</xmp>";
        $xml->saveXML("orders.xml");
        $xml=simplexml_load_file("orders.xml") or die("ERROR");
        $a=1;
        foreach($xml->children() as $order)
        {
        echo"<div class = 'orderlist' ";
        echo"<b>Order No.:". $a++."</b><br>";
        echo"Product Id: ".$order->id."<br>";
        echo"Product Name: ".$order->name."<br>";
        echo"Price: ".$order->price."<br>";
        echo"Quantity Ordered: ".$order->quan."<br>";
        echo"Total Price: ".$order->total."<br>";
        echo"</div><br>";
        }

    }
    else{
        $orders = $xml -> createElement("orders");
        $xml -> appendChild($orders);

        $order = $xml -> createElement("order");
        $orders -> appendChild($order);

        $id = $xml -> createElement("id", $id);
        $order -> appendChild($id);



        $name = $xml -> createElement("name", $name);
        $order -> appendChild($name);

        $price = $xml -> createElement("price", $price);
        $order -> appendChild($price);

        $quan = $xml -> createElement("quan", $quan);
        $order -> appendChild($quan);

        $total = $xml -> createElement("total", $total);
        $order -> appendChild($total);

        $xml->saveXML("orders.xml");
        $xml->saveXML($order->item(0));

        $xml=simplexml_load_file("orders.xml") or die("ERROR");
        $a=1;
        foreach($xml->children() as $order)
        {
        echo"<div class = 'orderlist' ";
        echo"<b>Order No.:".$a++."</b><br>";
        echo"Product Id: ".$order->id."<br>";
        echo"Product Name: ".$order->name."<br>";
        echo"Price: ".$order->price."<br>";
        echo"Quantity Ordered: ".$order->quan."<br>";
        echo"Total Price: ".$order->total."<br>";
        echo"</div><br>";
        }
    }

1 个答案:

答案 0 :(得分:0)

DOMDocument::saveXML()不接受文件名参数。它将结果作为字符串返回,您需要自己将其写入文件。所以改变:

$xml->saveXML("orders.xml");

为:

file_put_contents("orders.xml", $xml->saveXML());