这篇文章主要为大家详细介绍了php生成xml的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!
当处理基于XML应用程序时,开发者经常需要建立XML编码数据结构。例如,Web中基于用户输入的XML状态模板,服务器请求XML语句,以及基于运行时间参数的客户响应。代码如下:
// 四海网网 Www.q1010.com
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// save and display tree
echo $dom->saveXML();
代码如下:
<?php
// 四海网网 Www.q1010.com
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// save and display tree
echo $dom->saveXML();
?>
代码如下:
<?xml version="1.0"?>
<toppings>
<item>pepperoni</item>
</toppings>
代码如下:
<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create another text node
$text = $dom->createTextNode("tomato");
$item->appendChild($text);
// save and display tree
echo $dom->saveXML();
?>
代码如下:
<?xml version="1.0"?>
<toppings>
<item>pepperoni</item>
<item>tomato</item>
</toppings>
代码如下:
<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// save and display tree
echo $dom->saveXML();
?>
代码如下:
<?xml version="1.0"?>
<toppings>
<item price="4">pepperoni</item>
</toppings>
代码如下:
<?php
// create doctype
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save and display tree
echo $dom->saveXML();
?>
代码如下:
<?xml version="1.0"?>
<toppings>
<item price="4">pepperoni</item>
<![CDATA[
Customer requests that pizza be sliced into 16 square pieces
]]>
<?pizza bake()?>
</toppings>
代码如下:
<?php
// create doctype
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
$dom->formatOutput=true;
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be
sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save tree to file
$dom->save("order.xml");
// save tree to string
$order = $dom->save("order.xml");
?>
代码如下:
<?
$conn = mysql_connect('localhost', 'root', '123456') or die('Could not connect: ' . mysql_error());
mysql_select_db('vdigital', $conn) or die ('Can\'t use database : ' . mysql_error());
$str = "SELECT id,username FROM `admin` GROUP BY `id` ORDER BY `id` ASC";
$result = mysql_query($str) or die("Invalid query: " . mysql_error());
if($result)
{
$xmlDoc = new DOMDocument();
if(!file_exists("01.xml")){
$xmlstr = "<?xml version='1.0' encoding='utf-8' ?><message></message>";
$xmlDoc->loadXML($xmlstr);
$xmlDoc->save("01.xml");
}
else { $xmlDoc->load("01.xml");}
$Root = $xmlDoc->documentElement;
while ($arr = mysql_fetch_array($result)){
$node1 = $xmlDoc->createElement("id");
$text = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["id"]));
$node1->appendChild($text);
$node2 = $xmlDoc->createElement("name");
$text2 = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["username"]));
$node2->appendChild($text2);
$Root->appendChild($node1);
$Root->appendChild($node2);
$xmlDoc->save("01.xml");
}
}
mysql_close($conn);
?>
代码如下:
<?
$xmlDoc = new DOMDocument();
$xmlDoc->load("http://localhost/xml/xml.php");
$x=$xmlDoc->getElementsByTagName('name');
for ($i=0; $i<=$x->length-1; $i++)
{
if(strpos($x->item($i)->nodeValue,"fang")!==false)
{
echo $x->item($i)->parentNode->childNodes->item(1)->nodeValue;
}
}
?>
本文来自:http://www.q1010.com/173/13902-0.html
注:关于php生成xml的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。