什么是 xml?
xml 是一种数据存储格式。它没有定义保存什么数据,也没有定义数据的格式。xml 只是定义了标记和这些标记的属性。格式良好的 xml 标记看起来像这样:
<name>jack herrington</name>
dom读取 xml
<?php
$doc = new domdocument();
$doc->load( 'books.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";
}
?>
用 dom 编写 xml
<?php
$books = array();
$books [] = array(
'title' => 'php hacks',
'author' => 'jack herrington',
);
$doc = new domdocument(); //创建dom对象
$doc->formatoutput = true;
$r = $doc->createelement( "books" );//创建标签
$doc->appendchild( $r ); //将$r标签,加入到xml格式中。
foreach( $books as $book )
{
$b = $doc->createelement( "book" ); //创建标签
$author = $doc->createelement( "author" );
$author->appendchild($doc->createtextnode( $book['author'] )); //给标签添加内容
$b->appendchild( $author ); //将子标签 加入父标签
$r->appendchild( $b ); //加入父标签中!
}
echo $doc->savexml();
?>
以上就是这2段读取和编写xml的dom代码了,小伙伴们了解了没,有什么疑问可以给我留言
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
荐
数据库的设计
数据库的设计一 为什么需要规范的数据库设计数据库设计的步骤收集信息标识实体标识每个实体需要存储的详细信息标识实体...
[阅读全文]
-
-
-
-
-
-
-
-
-
-
网友评论