这篇文章主要为大家详细介绍了PHP读取和编写XML DOM代码的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!
代码如下:
// 用 DOM 读取 XML
$doc = new DOMDocument();
$doc->load(‘test.xml');
$books = $doc->getElementsByTagName(“book”);
foreach( $books as $book ){
$authors = $book->getElementsByTagName(“author”);
$author = $authors->item(0)->nodeValue; // nodeValue属 性可根据节点的类型来设置或返回某个节点的值。
$publishers = $book->getElementsByTagName(“publisher”);
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( ”title” );
$title = $titles->item(0)->nodeValue;
echo ”Title: $title <br> Author: $author <br> Publisher: $publisher<br><hr><br>”;
}
代码如下:
// 用 SAX 解析器读取 XML
$g_books = array();
$g_elem = null;
function startElement( $parser, $name, $attrs ){
global $g_books, $g_elem;
if ( $name == 'BOOK' ) $g_books []= array();
$g_elem = $name;
}
function endElement( $parser, $name ){
global $g_elem;
$g_elem = null;
}
function textData( $parser, $text ){
global $g_books, $g_elem;
if ( $g_elem == 'AUTHOR' || $g_elem == 'PUBLISHER' || $g_elem == 'TITLE' ){
$g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text;
}
}
$parser = xml_parser_create();
xml_set_element_handler( $parser, ”startElement”, ”endElement” );
xml_set_character_data_handler( $parser, ”textData” );
$f = fopen( 'test.xml', 'r' );
while( $data = fread( $f, 4096 ) ){
xml_parse( $parser, $data );
}
xml_parser_free( $parser );
foreach( $g_books as $book ){
echo $book['TITLE'].” - ”.$book['AUTHOR'].” - ”;
echo $book['PUBLISHER'].”\n”;
}
代码如下:
$xml = ”";
$f = fopen( 'test.xml', 'r' );
while( $data = fread( $f, 4096 ) ) { $xml .= $data; }
fclose( $f );
preg_match_all( ”/\<book\>(.*?)\<\/book\>/s”, $xml, $bookblocks );
foreach( $bookblocks[1] as $block ){
preg_match_all( ”/\<author\>(.*?)\<\/author\>/”, $block, $author );
preg_match_all( ”/\<title\>(.*?)\<\/title\>/”, $block, $title );
preg_match_all( ”/\<publisher\>(.*?)\<\/publisher\>/”, $block, $publisher );
echo( $title[1][0].” - ”.$author[1][0].” - ”. $publisher[1][0].”\n” );
}
代码如下:
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher' => ”O'Reilly”
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington',
'publisher' => ”O'Reilly”
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( ”books” );
$doc->appendChild( $r );
foreach( $books as $book ){
$b = $doc->createElement( ”book” );
$author = $doc->createElement( ”author” );
$author->appendChild( $doc->createTextNode( $book['author'] ) );
$b->appendChild( $author );
$title = $doc->createElement( ”title” );
$title->appendChild( $doc->createTextNode( $book['title'] ) );
$b->appendChild( $title );
$publisher = $doc->createElement( ”publisher” );
$publisher->appendChild( $doc->createTextNode( $book['publisher'] ) );
$b->appendChild( $publisher );
$r->appendChild( $b );
}
//echo $doc->saveXML();
代码如下:
<?php
PHP 编写xml
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher' => ”O'Reilly”
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington',
'publisher' => ”O'Reilly”
);
?>
<books>
<?php
foreach( $books as $book )
{
?>
<book>
<title><?php echo( $book['title'] ); ?></title>
<author><?php echo( $book['author'] ); ?>
</author>
<publisher><?php echo( $book['publisher'] ); ?>
</publisher>
</book>
<?php
}
?>
</books>
代码如下:
<?xml version=”1.0″ encoding=”utf8″?>
<books>
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
<book>
<author>Jack Herrington</author>
<title>Podcasting Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</books>
本文来自:http://www.q1010.com/173/13696-0.html
注:关于PHP读取和编写XML DOM代码的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。