首页 > 编程知识 正文

php文件生成xml文档,php读取xml接口数据

时间:2023-12-27 22:27:02 阅读:325337 作者:RJYL

本文目录一览:

如何用php从数据库读取数据并生成xml文件

我的思路是,直接使用动态的xml,让flash读取这个文档,这样就不用实时的去生成xml文件了。当然,这个xml文件是.php格式的,所以你必须在flash中吧读取的文件地址改成php的,就跟你写一个php页面一样,不同的是这个php文件输出的内容是一个xml格式的文本。

比如你现在建立文件 xml.php

?php

echo "?xml version="1.0" encoding="utf-8"?

gallery

settings";

//若此处也有动态信息 按需要进行调用

echo"/settings

items";

//在此循环你的图片数据

$data = ??

while( $data ) {

echo "item source="".$data['source']."" description="".$data['description']."" /";

}

echo '/items';

?

如何用php生成xml文件 高手进入 有加分

可以使用php里的dom,如下:

$xmlStr=[文件位置];

$dom = new DOMDocument('1.0');

$dom -loadXML($xmlStr);

//根

$vogue=$dom-createElement('vogue');

$dom-appendChild($vogue);

//第一级子目录

$level0=$dom-createElement('level0');

//第一级子目录的属性

$attr=$dom-createAttribute('parentTypeID');

$attr-appendChild($dom-createTextNode('-1'));

........

//保存

$dom-saveXML($xmlStr);

但是我更推荐使用print来打印后输入文件。

因为dom的输出无法分行,样例如下:

?php

$books = array();

$books [] = array(

'title' = 'PHP Hacks',

'author' = 'Jack Herrington',

'publisher' = "O'Reilly"

);

$books [] = array(

'title' = 'Podcasting Hacks',

'author' = 'Jack Herrington',

'publisher' = "O'Reilly"

);

?

books

?php

foreach( $books as $book )

{

?

book

title?php echo( $book['title'] ); ?/title

author?php echo( $book['author'] ); ?

/author

publisher?php echo( $book['publisher'] ); ?

/publisher

/book

?php

}

?

/books

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文件

?php

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

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

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

$module=$dom-createElement('newmodule');// root node

$dom-appendChild($module);

$year=$dom-createElement('year'); // add attribute node

$name=$dom-createAttribute('name');

$name-nodeValue="最新动态";

$year-setAttributeNode($name);

$module-appendChild($year);

$news=$dom-createElement('news');

$year-appendChild($news);

$date=$dom-createElement('date');

$date_value=$dom-createTextNode('01-24');

$date-appendChild($date_value);

$news-appendChild($date);

$title=$dom-createElement('title');

$title_value=$dom-createTextNode('![CDATA[最新动态]]');

$title-appendChild($title_value);

$news-appendChild($title);

$info=$dom-createElement('info');

$info_value=$dom-createTextNode('![CDATA[ 表面采用进口楸木木皮拼贴成精美的拼花,自然清晰的木材纹理得到完美的呈现,各种材质的合理搭配缔造了雅意系列精致的家具产品。

]]');

$info-appendChild($info_value);

$news-appendChild($info);

echo $dom-saveXML();

$dom-save($path);

?

php 生成xml高级

楼主啊,我看了你这段语句里没有做任何可能影响xml结构的运算干嘛非要用DomDocument来生成啊?

直接echo就完了,又简单又明了。虽然有点土。但是土的东西就是专门用来对付简单问题的。不过echo的时候要注意:

?xml version="1.0" encoding="utf-8"?

之前不能有输出。

还有之前的所有include都必须去掉?结尾符号,否则一些版本的php会在之前输出一大段空格,导致xml头无法被浏览器识别到。

php生成xml代码

$docu=new DOMDocument('1.0','utf-8');//声明DOMDocument对象

$docu-formatOutput=true;//使用xml标准化格式输出

$request=$docu-createElement('request');//使用createElement创建一个request标签

$action=$docu-createElement('action');//在request标签下创建action标签

$value=$docu-createTextNode('regAndenter');//在action里插入字符串

$action-appendChild($value);//

$request-appendChild($action);//将创建的action标签添加到xml文件里

$xmlid=$docu-createElement('id');

$value=$docu-createTextNode($dataid);

$xmlid-appendChild($value);

$request-appendChild($xmlid);

$phonenumber =$docu-createElement('phonenumber');

$value=$docu-createTextNode($uname);

$phonenumber-appendChild($value);

$request-appendChild($phonenumber);

$password =$docu-createElement('password');

$value=$docu-createTextNode($pass);

$password-appendChild($value);

$request-appendChild($password);

$email=$docu-createElement('email');

$value=$docu-createTextNode($emai);

$email-appendChild($value);

$request-appendChild($email);

$realname =$docu-createElement('realname');

$value=$docu-createTextNode($name1);

$realname-appendChild($value);

$request-appendChild($realname);

$document_id =$docu-createElement('document_id');

$value=$docu-createTextNode($idc);

$document_id-appendChild($value);

$request-appendChild($document_id);

$provincecode=$docu-createElement('provincecode');

$value=$docu-createTextNode($privince);

$provincecode-appendChild($value);

$request-appendChild($provincecode);

$citycode =$docu-createElement('citycode');

$value=$docu-createTextNode($city);

$citycode-appendChild($value);

$request-appendChild($citycode);

$grouptype =$docu-createElement('grouptype');

$value=$docu-createTextNode($type);

$grouptype-appendChild($value);

$request-appendChild($grouptype);

$college =$docu-createElement('college');

$value=$docu-createTextNode($colle);

$college-appendChild($value);

$request-appendChild($college);

$docu-appendChild($request);//将创建的request标签添加到xml文件里

//$docu-save('wang.xml');//生成xml文件

照着我这个改,希望你能看得懂

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。