【问题标题】:Create new xml file and save in system using symfony2.8使用 symfony2.8 创建新的 xml 文件并保存在系统中
【发布时间】:2016-10-21 11:23:29
【问题描述】:

我想创建 .xml 文件并使用 symfony2.8 直接从控制器保存,xml 文件应该是自动附加一次创建了一天。

帮助将不胜感激

$mainNode = new \SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><items></items>"); 
$productNode = $mainNode->addChild('product');
$productNode->addChild( 'productCode', '1235846' );
$mainNode->asXML();

【问题讨论】:

  • 请分享您当前的工作和代码。
  • 嗨@LBA这里是我的演示代码$mainNode = new \SimpleXMLElement( "&lt;?xml version='1.0' encoding='UTF-8'?&gt;&lt;items&gt;&lt;/items&gt;" ); $productNode = $mainNode-&gt;addChild('product'); $productNode-&gt;addChild( 'productCode', '1235846' ); $mainNode-&gt;asXML();
  • 不,抱歉,它不是这样工作的。请通过任何可能有助于我们帮助您的努力和代码来更新您的问题。请具体说明什么不起作用,发生了什么样的错误等。
  • @KartikPatel - 请注意,您可以编辑您的问题并在那里添加您的代码详细信息。粘贴到 cmets 中的代码大多不可读。
  • 到目前为止您尝试过什么。您需要提供到目前为止的代码和结果。

标签: php xml symfony xml-parsing save


【解决方案1】:

你可以这样做:

public function updateTodaysXMLAction()
{
    $todaysFile = '/path/to/file/' . date('d-m-Y').'.xml';
    $xmlFileContents = file_get_contents($todaysFile);
    $xml = new \SimpleXMLElement($xmlFileContents);
    //make updates as needed
    $xml->addChild('product')->addChild( 'productCode', '1235846' );
    // Save out the file
    $xml->asXML($todaysFile);
}

【讨论】:

    【解决方案2】:
                $filePath = __DIR__ . '/../../../app/logs/xml/';
                $fileName = 'xmlLog_' . date('d.m.Y') . '.xml';
    
                if (file_exists($filePath . $fileName)) {
                    // append data if file already exist
                    $xml = simplexml_load_file($filePath . $fileName);
                    $productList = $xml->productList;
    
                    $lastElement  = count($productList->product);
    
                    foreach ($logArray as $key => $data) {
                        $rN = $productList->addChild('product');
                        $rN->addChild('id', $lastElement);
                        $rN->addChild('productId', $logArray[$key]['productId']);
                        $rN->addChild('type', $logArray[$key]['type']);
                        $rN->addChild('oldValue', $logArray[$key]['oldValue']);
                        $rN->addChild('adjustment', $logArray[$key]['adjustment']);
                        $rN->addChild('newValue', $logArray[$key]['newValue']);
                        $lastElement++;
                    }
    
                    $xml->asXML($filePath . $fileName);
                } else {
                    // create new file if not exist
                    $mainNode = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><inventory></inventory>');
    
                    $productNode = $mainNode->addChild('productList');
    
                    foreach ($logArray as $key => $data) {
                        $rN = $productNode->addChild('product');
                        $rN->addChild('id', $key + 1);
                        $rN->addChild('productId', $logArray[$key]['productId']);
                        $rN->addChild('type', $logArray[$key]['type']);
                        $rN->addChild('oldValue', $logArray[$key]['oldValue']);
                        $rN->addChild('adjustment', $logArray[$key]['adjustment']);
                        $rN->addChild('newValue', $logArray[$key]['newValue']);
                    }
    
                    $fs = new Filesystem();
                    $fs->mkdir($filePath);
    
                    $mainNode->asXML($filePath . $fileName);
                }
    

    【讨论】:

      猜你喜欢
      • 2013-08-19
      • 2016-11-22
      • 2017-01-21
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      相关资源
      最近更新 更多