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

本文目录一览:

如何用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文件

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

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/257412.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-15 12:45
下一篇 2024-12-15 12:45

相关推荐

  • Python读取CSV数据画散点图

    本文将从以下方面详细阐述Python读取CSV文件并画出散点图的方法: 一、CSV文件介绍 CSV(Comma-Separated Values)即逗号分隔值,是一种存储表格数据的…

    编程 2025-04-29
  • vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常问题的解决

    本文旨在解决vue下载无后缀名的文件被加上后缀.txt,有后缀名的文件下载正常的问题,提供完整的代码示例供参考。 一、分析问题 首先,需了解vue中下载文件的情况。一般情况下,我们…

    编程 2025-04-29
  • 如何在Java中拼接OBJ格式的文件并生成完整的图像

    OBJ格式是一种用于表示3D对象的标准格式,通常由一组顶点、面和纹理映射坐标组成。在本文中,我们将讨论如何将多个OBJ文件拼接在一起,生成一个完整的3D模型。 一、读取OBJ文件 …

    编程 2025-04-29
  • 为什么用cmd运行Java时需要在文件内打开cmd为中心

    在Java开发中,我们经常会使用cmd在命令行窗口运行程序。然而,有时候我们会发现,在运行Java程序时,需要在文件内打开cmd为中心,这让很多开发者感到疑惑,那么,为什么会出现这…

    编程 2025-04-29
  • Python中读入csv文件数据的方法用法介绍

    csv是一种常见的数据格式,通常用于存储小型数据集。Python作为一种广泛流行的编程语言,内置了许多操作csv文件的库。本文将从多个方面详细介绍Python读入csv文件的方法。…

    编程 2025-04-29
  • Python程序文件的拓展

    Python是一门功能丰富、易于学习、可读性高的编程语言。Python程序文件通常以.py为文件拓展名,被广泛应用于各种领域,包括Web开发、机器学习、科学计算等。为了更好地发挥P…

    编程 2025-04-29
  • Python将矩阵存为CSV文件

    CSV文件是一种通用的文件格式,在统计学和计算机科学中非常常见,一些数据分析工具如Microsoft Excel,Google Sheets等都支持读取CSV文件。Python内置…

    编程 2025-04-29
  • Python zipfile解压文件乱码处理

    本文主要介绍如何在Python中使用zipfile进行文件解压的处理,同时详细讨论在解压文件时可能出现的乱码问题的各种解决办法。 一、zipfile解压文件乱码问题的根本原因 在P…

    编程 2025-04-29
  • Python如何导入py文件

    Python是一种开源的高级编程语言,因其易学易用和强大的生态系统而备受青睐。Python的import语句可以帮助用户将一个模块中的代码导入到另一个模块中,从而实现代码的重用。本…

    编程 2025-04-29
  • 如何用Python统计列表中各数据的方差和标准差

    本文将从多个方面阐述如何使用Python统计列表中各数据的方差和标准差, 并给出详细的代码示例。 一、什么是方差和标准差 方差是衡量数据变异程度的统计指标,它是每个数据值和该数据值…

    编程 2025-04-29

发表回复

登录后才能评论