澳门威利斯人_威利斯人娱乐「手机版」

来自 网络资讯 2019-08-17 13:01 的文章
当前位置: 澳门威利斯人 > 网络资讯 > 正文

PHP输出生成XML文件实例程序,用PHP编写和读取X

复制代码 代码如下:

方法四

<?php
$sxe = simplexml_load_string('<books><book><title>Great American
Novel</title></book></books>');
if ($sxe === false) {
echo 'Error while parsing the document';
exit;
}
$dom_sxe = dom_import_simplexml($sxe);
if (!$dom_sxe) {
echo 'Error while converting XML';
exit;
}
$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);
$test2 = $dom->saveXML(); // put string in test2
$dom -> save('test2.xml'); // save as file
?>

//为book创制标题
$title = $dom->createElement('title');
$titleText = $dom->createTextNode('PHP与AJAX');
$title->appendChild($titleText);

 代码如下

复制代码 代码如下:

复制代码 代码如下:

 

?>

<?php
header("Content-type: text/xml");
echo "<?xml version=/"1.0/" encoding=/"UTF-8/"?>";
echo "<users>";
echo "<user>";
   echo "<name>";
    echo "小小新手";
   echo "</name>";
   echo "<age>";
    echo "24";
   echo "</age>";
   echo "<sex>";
    echo "男";
   echo "</sex>";
echo "</user>";
echo "<user>";
   echo "<name>";
    echo "艳艳";
   echo "</name>";
   echo "<age>";
    echo "23";
   echo "</age>";
   echo "<sex>";
    echo "女";
   echo "</sex>";
echo "</user>";
echo "</users>";
?>

使用DomDocument生成XML文件

<?
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<books>
<book>
<title>Great American Novel</title>
<characters>
<character>
<name>Cliff</name>
<desc>really great guy</desc>
</character>
<character>
<name>Lovely Woman</name>
<desc>matchless beauty</desc>
</character>
<character>
<name>Loyal Dog</name>
<desc>sleepy</desc>
</character>
</characters>
<plot>
Cliff meets Lovely Woman. Loyal Dog sleeps, but wakes up to bark
at mailman.
</plot>
<success type='bestseller'>4</success>
<success type='bookclubs'>9</success>
</book>
</books>
XML;

//创建book元素
$book = $dom->createElement('book');
$book->appendChild($title);
$book->appendChild($isbn);

 代码如下

二.行使simple生成和读取xml文件
实例一:

其次种方式:

SimpleXML作为PHP大旨的组成都部队分,能够把XML转变为目的,不过一时,笔者供给对输出的xml格式设置编码;
代码:

复制代码 代码如下:

?>

方法二

if (file_exists('test1.xml')) { //读取xml文件
$xml = simplexml_load_file('test1.xml');
var_dump(xml);
} else {
exit('Failed to open test1.xml.');
}

复制代码 代码如下:

举例想出口:

//修改文件节点内容
$xml = new SimpleXMLElement($xmlstr);
$xml->book[0]->characters->character[0]->name = 'Big Cliff';
echo $xml->asXML();

<?php
header("Content-type: text/xml");
echo "<?xml version=/"1.0/" encoding=/"UTF-8/"?>";
echo "<users><user><name>小小菜鸟</name><age>24</age><sex>男</sex></user><user><name>艳艳</name><age>23</age><sex>女</sex></user></users>";
?>

 代码如下

实例三:
test1.xml:

//在一字符串变量中树立XML结构
$xmlString = $dom->saveXML();

输出结果:

复制代码 代码如下:

本文由澳门威利斯人发布于网络资讯,转载请注明出处:PHP输出生成XML文件实例程序,用PHP编写和读取X

关键词: 澳门威利斯人