文章介紹了三種方式來讀取xml文件分別是new DOMDocument(),正則解析xml,用parser函數來讀取xml數據,這些方法都是可行的,但第一種和最后一種要好一些.
new DOMDocument()實例代碼如下:
- <?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";
- }
- ?>
正則解析,代碼如下:
- <?php
- $xml = "";
- $f = fopen( 'books.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.xml文件如下:
- <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>
下面就給大家舉一個小小的例子用parser函數來讀取xml數據,代碼如下:
- <?php
- $parser = xml_parser_create(); //創(chuàng)建一個parser編輯器
- xml_set_element_handler($parser, "startElement", "endElement");//設立標簽觸發(fā)時的相應函數 這里分別為startElement和endElenment
- xml_set_character_data_handler($parser, "characterData");//設立數據讀取時的相應函數
- $xml_file="1.xml";//指定所要讀取的xml文件,可以是url
- $filehandler = fopen($xml_file, "r");//打開文件
- while ($data = fread($filehandler, 4096))
- {
- xml_parse($parser, $data, feof($filehandler));
- }//每次取出4096個字節(jié)進行處理
- fclose($filehandler);
- xml_parser_free($parser);//關閉和釋放parser解析器
- $name=false;
- $position=false;
- function startElement($parser_instance, $element_name, $attrs) //起始標簽事件的函數
- {
- global $name,$position;
- if($element_name=="NAME")
- {
- $name=true;
- $position=false;
- echo "名字:";
- }
- if($element_name=="POSITION")
- {$name=false;
- $position=true;
- echo "職位:";
- }
- }
- function characterData($parser_instance, $xml_data) //讀取數據時的函數
- {
- global $name,$position;
- if($position)
- echo $xml_data."<br>";
- if($name)
- echo $xml_data."<br>";
- }
- //開源代碼Vevb.com
- function endElement($parser_instance, $element_name) //結束標簽事件的函數
- {
- global $name,$position;
- $name=false;
- $position=false;
- }
- ?>
xml文件代碼如下:
- <?xml version="1.0"?>
- <employees>
- <employee>
- <name>張三</name>
- <position age="45">經理</position>
- </employee>
- <employees>
- <employee>
- <name>李四</name>
- <position age="45">助理</position>
- </employee>
- </employees>
parser是php內置的一個用來處理xml的解析器,它的工作由三個事件組成:起始標簽、 讀取數據、結束標簽.
也就是說在對xml進行處理的時候每當遇到起始標簽、數據和結束標簽的時候函數會做相應的動作來完成對xml數據的轉換.
新聞熱點
疑難解答