您好,欢迎访问一九零五行业门户网

用PHP编写和读取XML的几种方式_PHP教程

一.使用dom生成和读取xml文件
实例一:
复制代码 代码如下:
appendchild($dom->createelement_x_x ('books'));
//add element to
$book = $books->appendchild($dom->createelement_x_x ('book'));
//add element to
$title = $book->appendchild($dom->createelement_x_x ('title'));
//add text node element to
$title->appendchild($dom->createtextnode('great american novel'));
//generate xml
$dom->formatoutput = true; // set 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 =
{$aa}
joe
jane
i know that's the answer -- but what's the question?
xml;
$dom = new domdocument;
$dom->loadxml($xmlstr);
$test1 = $dom->savexml();
$dom->save('test1.xml');
实例三:
test1.xml:
复制代码 代码如下:
jack herrington
php hacks
o'reilly
jack herrington
podcasting hacks
o'reilly
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 =
great american novel
cliff
really great guy
lovely woman
matchless beauty
loyal dog
sleepy
cliff meets lovely woman. loyal dog sleeps, but wakes up to bark
at mailman.
4
9
xml;
//提取节点内容
$xml = new simplexmlelement($xmlstr);
foreach ($xml->book[0]->success as $success) {
switch((string) $success['type']) { // get attributes as element indices
case 'bestseller':
echo $success. ' months on bestseller list
';
break;
case '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);
} else {
exit('failed to open test1.xml.');
}
三.dom和simple互操作
dom导入simplexml:
复制代码 代码如下:
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:
复制代码 代码如下:
loadxml('great american
novel');
if (!$dom) {
echo 'error while parsing the document';
exit;
}
$s = simplexml_import_dom($dom);
echo $s->book[0]->title; // great american novel
?>
http://www.bkjia.com/phpjc/326360.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/326360.htmltecharticle一.使用dom生成和读取xml文件 实例一: 复制代码 代码如下: ?php //creates xml string and xml document using the dom $dom = new domdocument('1.0'); //add root - bo...
其它类似信息

推荐信息