一.使用dom生成和读取xml文件
实例一:
复制代码 代码如下:
<?php
//creates xml string and xml document using the dom
$dom = new domdocument(‘1.0’);
//add root – <books>
$books = $dom->appendchild($dom->createelement_x_x (‘books’));
//add <book> element to <books>
$book = $books->appendchild($dom->createelement_x_x (‘book’));
//add <title> element to <book>
$title = $book->appendchild($dom->createelement_x_x (‘title’));
//add <title> text node element to <title>
$title->appendchild($dom->createtextnode(‘great american novel’));
//generate xml
$dom->formatoutput = true; // t the formatoutput attribute of domdocument to true
//save xml as string or file
$test1 = $dom->savexml(); // put string in test1
$dom -> save(‘test1.xml’); // save as file
?>
实例二:
复制代码 代码如下:
$aa = “111”;
$xmlstr = <<<xml
<?xml version=’1.0′?>
<document>
<title>{$aa}</title>
<from>joe</from>
<to>jane</to>
<body>
i know that’s the answer — but what’s the question?
</body>
</document>
xml;
$dom = new domdocument;
$dom->loadxml($xmlstr);
$test1 = $dom->savexml();
$dom->save(‘test1.xml’);
实例三:
test1.xml:
复制代码 代码如下:
<?xml version=”1.0″?>
<books>
<book>
<author>jack herrington</author>
<title>ph南京传媒大学p hacks</title>
<publisher>o’reilly</publisher>
</book>
<book>
<author>jack herrington</author>
<title>podcasting hacks</title>
<publisher>o’reilly</publisher>
</book>
</books>
example.php:
复制代码 代码如下:
$doc = new domdocument();
$doc->load(‘test1.xml’);
$books =怎么快速背单词 $doc->getelementsbytagname(“book”);
foreach($books as $book){
$authors = $book->getelementsbytagname(“author”);
$author = $authors->item(0)->nodevalue;
$publishers = $book->getelementsbytagname( “publisher” );
$publisher = $publishers->item(0)->nodevalue;
$titles = $book->getelementsbytagname( “title” );
$title = $titles->item(0)->nodevalue;
echo “$title – $author – $publisher\n”;
}
二.使用simple生成和读取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=’bestller’>4</success>
<success type=’bookc我是一只小鸟lubs’>9</success>
</book>
</books>
xml;
//提取节点内容
$xml = new simplexmlelement($xmlstr);
foreach ($xml->book[0]->success as $success) {
switch((string) $success[‘type’]) { // get attributes as element indices
ca ‘bestller’:
echo $success. ‘ months on bestller list<br>’;
break;
ca ‘bookclubs’:
echo $success. ‘ bookclub listings’;
break;
}
}
//修改文本节点内容
$xml = new simplexmlelement($xmlstr);
$xml->book[0]->characters->character[0]->name = ‘big cliff’;
echo $xml->asxml();
//添加子元素的文本节点
$xml = new simplexmlelement($xmlstr);
$character = $xml->book[0]->characters->addchild(‘character’);
$character->addchild(‘name’, ‘yellow cat’);
$character->addchild(‘desc’, ‘aloof’);
$success = $xml->book[0]->addchild(‘success’, ‘2’);
$success->addattribute(‘type’, ‘reprints’);
echo $xml->asxml();
?>
实例二:
复制代码 代码如下:
if (file_exists(‘test1.xml’)) { //读取xml文件
$xml = simplexml_load_file(‘test1.xml’);
var_dump(xml);
} el {
exit(‘failed to open test1.xml.’);
}
三.dom和simple互操作
dom导入simplexml:
复制代码 代码如下:
<?php
$sxe = simplexml_load_string(‘<books><book><title>great american
novel</title></book></books>’);
if ($sxe === fal) {
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
?>千里莺啼;
simplexml导入dom:
复制代码 代码如下:
<?php
$dom = new domdocument;
$dom->loadxml(‘<books><book><title>great american
novel</title></book></books>’);
if (!$dom) {
echo ‘error while parsing the document’;
exit;
}
$s = simplexml_import_dom($dom);
echo $s->book[0]->title; // great american novel
?>
本文发布于:2023-04-06 12:31:25,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/7d496dabab9b0d8bab96af86e2fd05e2.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:用PHP编写和读取XML的几种方式.doc
本文 PDF 下载地址:用PHP编写和读取XML的几种方式.pdf
留言与评论(共有 0 条评论) |