Home>Article>Backend Development> Example code for PHP method of reading XML files
The examples in this article summarize the method of PHP readingXMLformat files. Share it with everyone for your reference, the details are as follows:
Jack Herrington PHP Hacks O'Reilly Jack Herrington Podcasting Hacks O'Reilly
DOMDocument method
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"; echo "
"; } ?>
2. Use SAX parser to read XML:
3. Useregular expressionto parse XML:
(.*?)\<\/book\>/s", $xml, $bookblocks ); foreach( $bookblocks[1] as $block ){ preg_match_all( "/\(.*?)\<\/author\>/", $block, $author ); preg_match_all( "/\ (.*?)\<\/title\>/", $block, $title ); preg_match_all( "/\ (.*?)\<\/publisher\>/", $block, $publisher ); echo( $title[1][0]." - ".$author[1][0]." - ".$publisher[1][0]."\n" ); } ?>
4. Parse XML intoarray
简单的XML数据 "; $parser = xml_parser_create(); //创建解析器 xml_parse_into_struct($parser, $data, $values, $index); //解析到数组 xml_parser_free($parser); //释放资源 //显示数组结构 echo "\n索引数组\n"; print_r($index); echo "\n数据数组\n"; print_r($values); ?>
5. Check whether the XML is valid
"; //输出错误行,列及其错误信息 $error_line = xml_get_current_line_number($xml_parser); $error_row = xml_get_current_column_number($xml_parser); $error_string = xml_error_string(xml_get_error_code($xml_parser)); $message = sprintf("[第%d行,%d列]:%s", $error_line, $error_row, $error_string); echo $message; } else { echo "该XML文档是结构良好的。"; } //关闭XML解析器指针,释放资源 xml_parser_free($xml_parser); ?>
6. Can be used to read XML accurately
test.xml
100 123456 20040605 153020 1 1 010 北京 010 北京 0 0 15933626501 8 5618常年供应苗木,品种有玉兰、黄叶杨等。联系人:张三,电话:1234567890。 100 9588
test. php:
-1) { try { //加载解析xml $xml = simplexml_load_string($file); if($xml) { //echo $this->result; //获取节点值 $CONNECT_ID = $xml->CONNECT_ID; $MO_MESSAGE_ID = $xml->MO_MESSAGE_ID; $RECEIVE_DATE = $xml->RECEIVE_DATE; $RECEIVE_TIME = $xml->RECEIVE_TIME; $GATEWAY_ID = $xml->GATEWAY_ID; $VALID = $xml->VALID; $CITY_CODE = $xml->CITY_CODE; $CITY_NAME = $xml->CITY_NAME; $STATE_CODE = $xml->CITY_CODE; $STATE_NAME = $xml->STATE_NAME; $TP_PID = $xml->TP_PID; $TP_UDHI = $xml->TP_UDHI; $MSISDN = $xml->MSISDN; $MESSAGE_TYPE = $xml->MESSAGE_TYPE; $MESSAGE = $xml->MESSAGE;//短信 $LONG_CODE = $xml->LONG_CODE; $SERVICE_CODE = $xml->SERVICE_CODE; preg_match("/(561)\d{1,2}/", $MESSAGE, $code); switch($code[0]) { case 5618 : $myData[message] = $MESSAGE; break; default : $myData[] = '没有短消息。'; break; } } else { echo "加载xml文件错误。"; } } catch(exception $e){ print_r($e); } } else { echo "没有该XML文件。"; } echo ""; print_r($myData); echo "
"; echo $myData[messageThe above is the detailed content of Example code for PHP method of reading XML files. For more information, please follow other related articles on the PHP Chinese website!