新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

php数据转化xml,php数据转换

php读取数据库并生成xml文件

?php

企业官网是企业形象的一张重要名片。创新互联公司的成都官网定制服务,能够将成都网页设计与企业的实力&公信力、产品服务优势、文化价值观等有机结合,把握企业的独特之处,突出重点核心内容,并以恰如其分的设计风格,抓住目标用户的关注点和兴趣点,帮助企业塑造好第一印象,全网营销推广展现公司实力。成都官网定制,为你解决成都创新互联网营销解决方案。

//读取数据库我就不说了,从数据库取出来就行了

#使用dom生成xml,注意生成的xml中会没有空格。

$dom=new DOMDocument('1.0','utf-8');

$time = time();

$path="$time.xml"; // $path 为xml文件的存储路径。

$module=$dom-createElement('breakfast_menu');//创建一个节点

$dom-appendChild($module); //在指定元素节点的最后一个子节点之后添加节点

$food=$dom-createElement('food'); //外body

$module-appendChild($food);

$name=$dom-createElement('name'); //内table

$name_value=$dom-createTextNode('测试数据1');

$name-appendChild($name_value);

$food-appendChild($name);

$price=$dom-createElement('price'); //内table

$price_value=$dom-createTextNode('测试数据2');

$price-appendChild($price_value);

$food-appendChild($price);

$description=$dom-createElement('description'); //内table

$description_value=$dom-createTextNode('测试数据3');

$description-appendChild($description_value);

$food-appendChild($description);

$calories=$dom-createElement('calories'); //内table

$calories_value=$dom-createTextNode('测试数据4');

$calories-appendChild($calories_value);

$food-appendChild($calories);

$dom-saveXML();

$dom-save($path);

//var_dump($dom-save($path));exit;

if($dom-saveXML()){

echo "生成成功:".$dom-saveXML();

}else{

echo "生成失败";

}

?

多维PHP数组怎么转换成xml格式的数据

public function arrayToXml($arr){

$xml = "xml";

foreach ($arr as $key=$val){

if(is_array($val)){

$xml.="".$key."".arrayToXml($val)."/".$key."";

}else{

if (is_numeric($val)){

$xml.="".$key."".$val."/".$key."";

}else{

$xml.="".$key."![CDATA[".$val."]]/".$key."";

}

}

}

$xml.="/xml";

return $xml;

}

php 怎么将数组转xml的函数

没有现成函数,只能自己写;我有一个别人写的函数:

?php

class A2Xml {

private $version = '1.0';

private $encoding = 'UTF-8';

private $root = 'root';

private $xml = null;

function __construct() {

$this-xml = new XmlWriter();

}

function toXml($data, $eIsArray=FALSE) {

if(!$eIsArray) {

$this-xml-openMemory();

$this-xml-startDocument($this-version, $this-encoding);

$this-xml-startElement($this-root);

}

foreach($data as $key = $value){

if(is_array($value)){

$this-xml-startElement($key);

$this-toXml($value, TRUE);

$this-xml-endElement();

continue;

}

$this-xml-writeElement($key, $value);

}

if(!$eIsArray) {

$this-xml-endElement();

return $this-xml-outputMemory(true);

}

}

}

$res = array(

'hello' = '11212',

'world' = '232323',

'array' = array(

'test' = 'test',

'b' = array('c'='c', 'd'='d')

),

'a' = 'haha'

);

$xml = new A2Xml();

echo $xml-toXml($res);

在PHP中将数组转换为XML格式

php数组格式:

Array to XML:

通过使用PHP的扩展SimpleXML,我们将uses_array转换为xml格式。

保存成功的XML文件:

The users.xml file contains the following xml.

附注:

Insert XML Into Databse

If you want to save the XML into the database, then replace the $xml_file variable line with the following code line. Now you can insert $xml_file variable into the database.


当前名称:php数据转化xml,php数据转换
当前路径:http://www.wjwzjz.com/article/hcehoh.html
在线咨询
服务热线
服务热线:028-86922220
TOP