繁体   English   中英

在树中为 output 文件格式化 XML 不起作用

[英]Formatting XML for output file in a tree doesn't work

我尝试以多种方式使用 php 代码以树格式保存到 xml 文件,但没有成功。 有什么不对劲,我不知道。 output 格式仍然是一个长字符串,如下所示:

<products><product id="p02"><name>Name 2</name><price currency="USD">200</price></product></products>

我需要一些帮助。 这是我的代码:

  <?php
  if(isset($_POST['submitSave'])) {
    $products = new DOMDocument('1.0');
    $products->preserveWhiteSpace = false;
    $products->formatOutput = true;
$doc->load('data/product.xml');
$product = $products->addChild('product');
$product->addAttribute('id', $_POST['id']);
$product->addChild('name', $_POST['name']);
$product->addChild('price', $_POST['price']);
file_put_contents('data/product.xml', $doc->saveXML());
header('location:index.php');
}
?>

output 文件是 product.xml。 对于测试,我使用http://localhost/teste.php?id=p02&name=Name2&price=200&currency=USD

$xmlstr= "<products></products>";
$sxe = new SimpleXMLElement($xmlstr);
$product = $sxe->addChild('product');
$product->addAttribute('id', $_REQUEST['id']);
$product->addChild('name', $_REQUEST['name']);
$price = $product->addChild('price', $_REQUEST['price']);
$price->addAttribute('currency', $_REQUEST['currency']);
$xmlOutput = $sxe->asXML();
file_put_contents('product.xml',$xmlOutput);

尝试这个:

<?php

if(isset($_POST['submitSave'])){

    $products = new DOMDocument('1.0');
    $products->preserveWhiteSpace = false;
    $products->formatOutput = true;

    // $doc->load('data/product.xml');

    $product = $products->createElement('product');
    $product->setAttribute('id', $_POST['id']);
    $name = $products->createElement('name', $_POST['name']);
    $price = $products->createElement('price', $_POST['price']);

    $products->appendChild($product);
    $product->appendChild($name);
    $product->appendChild($price);

    file_put_contents('data/product.xml', $products->saveXML());

    // echo $products->saveXML();
}
?>

这是没有 POST 参数的ideone演示

这是我在本地机器上的 output,使用后参数

代码输出

编辑:这里要求的是保留旧数据的代码

<?php

if(isset($_POST['submitSave'])){

    // Disable errors due to empty xml files
    error_reporting(E_ALL & ~E_WARNING);

    $domDoc = new DOMDocument('1.0');
    $domDoc->preserveWhiteSpace = false;
    $domDoc->formatOutput = true;

    // load xml file
    try {
        $domDoc->load('./data/product.xml');
    } catch (\Throwable $th) {
        //throw $th;
    }

    if($domDoc->getElementsByTagName('products')->length>0){
        // If we already have products tag defined
        $products = $domDoc->getElementsByTagName('products')[0];
    }else{
        // If we don't have any products tag, i.e. file is empty
        $products = $domDoc->createElement('products');
    }

    // Create child node for product and set id(attribute), name(child), price(child)
    $product = $domDoc->createElement('product');
    $product->setAttribute('id', $_POST['id']);
    $name = $domDoc->createElement('name', $_POST['name']);
    $price = $domDoc->createElement('price', $_POST['price']);

    $domDoc->appendChild($products);
    $products->appendChild($product);
    $product->appendChild($name);
    $product->appendChild($price);

    file_put_contents('./data/product.xml', $domDoc->saveXML());

}
?>

这是我本地机器上的 output :

代码输出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM