【问题标题】:Writing and Appending to an XML file写入和附加到 XML 文件
【发布时间】:2016-10-27 03:05:16
【问题描述】:

您好,我在网上找到了以下代码,它创建并添加数据到 xml 文件:

public function index()
    {
        $html = "";

        try {
            $employees = array();
            $employees [] = array(
                'name' => 'Albert',
                'age' => '34',
                'salary' => "$10000"
            );
            $employees [] = array(
                'name' => 'Claud',
                'age' => '20',
                'salary' => "$2000"
            );

            $doc = new DOMDocument();
            $doc->formatOutput = true;

            $r = $doc->documentElement->firstChild;
            $doc->appendChild( $r );

            foreach( $employees as $employee )
            {
                $b = $doc->createElement( "employee" );

                $name = $doc->createElement( "name" );
                $name->appendChild(
                    $doc->createTextNode( $employee['name'] )
                );
                $b->appendChild( $name );

                $age = $doc->createElement( "age" );
                $age->appendChild(
                    $doc->createTextNode( $employee['age'] )
                );
                $b->appendChild( $age );

                $salary = $doc->createElement( "salary" );
                $salary->appendChild(
                    $doc->createTextNode( $employee['salary'] )
                );
                $b->appendChild( $salary );

                $r->appendChild( $b );
            }

            $doc->save(".." . DIRECTORY_SEPARATOR . "storage" . DIRECTORY_SEPARATOR . "logs" . DIRECTORY_SEPARATOR . "employees.xml") ;


            return response()->json();
        }
        catch (\Exception $e)
        {
            return response()->json(
                [
                    "message" => $e->getMessage(),
                    "status_code" => $e->getCode(),
                    "data" => []
                ]);
        }

    }

这是输出:

<?xml version="1.0"?>
<employees>
  <employee>
    <name>Albert</name>
    <age>34</age>
    <salary>$10000</salary>
  </employee>
  <employee>
    <name>Claud</name>
    <age>20</age>
    <salary>$2000</salary>
  </employee>
</employees>

但是,当我再次运行代码时,它会给出相同的输出。我希望将新输出附加到 xml 文件中的先前数据,以便我有这个:

    <?xml version="1.0"?>
    <employees>
      <employee>
        <name>Albert</name>
        <age>34</age>
        <salary>$10000</salary>
      </employee>
      <employee>
        <name>Claud</name>
        <age>20</age>
        <salary>$2000</salary>
      </employee>
      <employee>
        <name>Albert</name>
        <age>34</age>
        <salary>$10000</salary>
      </employee>
      <employee>
    <name>Claud</name>
    <age>20</age>
    <salary>$2000</salary>
  </employee>
</employees>

【问题讨论】:

    标签: php xml laravel


    【解决方案1】:

    问题是脚本总是写入同一个文件 - 所以它覆盖了旧文件。

    解决方案 1:

    最简单的方法是在文件名后面加上时间戳,如下所示:

    $doc->save(".." . DIRECTORY_SEPARATOR . "storage" . DIRECTORY_SEPARATOR . "logs" . DIRECTORY_SEPARATOR . "employees-".date("Y/m/d - h:m:s").".xml") ;
    

    对于脚本的每次运行,您都会获得一个包含新内容的单独文件。

    解决方案 2:

    要将新数据附加到旧文件,您需要先加载旧文件,然后将其附加新内容。 DOMDocument 没有自动将新内容附加到旧内容的方法或功能。

    做完之后

    $doc = new DOMDocument();
    

    你可以load已经存在的文件

    $oldContent = $doc->load(<PATH TO YOUR XML FILE>);
    

    现在您可以将两者合并并保存。如何合并这两个文件已经here详细解释了。

    【讨论】:

    • 你的意思是我不能将数据附加到同一个 XML 文件中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2013-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多