php - 使用php将变量写入xml文件
问题描述
嘿,我正在尝试使用 php 变量生成 XML 文件。但是 echo 或 print 似乎不起作用,请看下面的代码段。我怎样才能实现我想要做的事情?
$xml = new DOMDocument();
$root = $xml->createElement('package');
$root = $xml->appendChild($root);
$title = $xml->createElement('id' , echo $_GET['bundleid']);
$title = $root->appendChild($title);
解决方案
正如 luenib 指出的那样,您通常不会将“echo”放在作为参数传递给函数的变量之前。下面是一个将 XML 输出到浏览器或写入文件的简单示例。
$xml = new DOMDocument();
$root = $xml->createElement('package');
$root = $xml->appendChild($root);
$title = $xml->createElement('id' , $_GET['bundleid']); // no "echo" before variable
//$title = $xml->createElement('id' , $_POST['bundleid']);
//$title = $xml->createElement('id' , $bundleid);
//$title = $xml->createElement('id' , 'bundleid');
$title = $root->appendChild($title);
$xml->formatOutput = true;
$xml_string = $xml->saveXML();
// Store XML to file.
file_put_contents('path/myXmlFile.xml',$xml_string);
// Output XML to browser.
//header("Content-type: text/xml");
//echo $xml_string;
推荐阅读
- angular - 导入导出核心模块和共享模块
- android - android 8 中的问题 - 使用 red5 pro 的两种方式流式传输
- go - golang 1.12 导入相对目录模块
- android - 在本地通知上打开特定页面单击 Ionic
- javascript - Notification.requestPermission() 未请求权限
- java - 在java中解密使用openSSL加密的文本
- mysql - 我可以在 MySQL 存储过程中覆盖 IN 参数吗
- java - 将代码添加到 smali 源
- image - 是否可以在音频直方图中嵌入少量带有水印的图像?
- elixir - 如何使用 elixir 测量磁盘大小?