


// Read XML using DOM
$doc = new DOMDocument();
$doc->load(‘test.xml’);
$books = $doc->getElementsByTagName(“book”);
foreach( $books as $book ){
$authors = $book->getElementsByTagName(“author”);
$author = $authors->item(0)->nodeValue; // The nodeValue attribute can set or return the value of a node according to the node type.
$publishers = $book->getElementsByTagName(“publisher”);
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( ”title” );
$title = $titles->item(0)->nodeValue;
echo ”Title: $title
Author: $author
Publisher: $publisher
”;
}
/*
The script first creates a new DOMdocument object, and uses the load method to load the book XML into this object. Afterwards, the script uses the getElementsByName method to get a list of all elements under the specified name.
In the loop of the book node, the script uses the getElementsByName method to obtain the nodeValue of the author, publisher and title tags. nodeValue is the text in the node. The script then displays these values.
*/
// Read XML with SAX parser
$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”;
}
/*
The script first sets up the g_books array, which holds all the books and book information in memory, and the g_elem variable holds the name of the tag the script is currently processing. The script then defines the callback function. In this example, the callback functions are startElement, endElement, and textData. When opening and closing the mark, call the startElement and endElement functions respectively. Call textData on the text between the opening and closing tags.
In this example, the startElement tag looks for the book tag to start a new element in the book array. The textData function then looks at the current element to see if it is a publisher, title, or author tag. If so, the function puts the current text into the current book.
To allow parsing to continue, the script creates a parser using the xml_parser_create function. Then, set the callback handle. Afterwards, the script reads the file and sends chunks of the file to the parser. After the file is read, the xml_parser_free function removes the parser. The end of the script prints the contents of the g_books array.
*/
// Parse XML with regular expressions
$xml = ”";
$f = fopen( 'test.xml', 'r' );
while( $data = fread( $f, 4096 ) ) { $xml .= $ data; }
fclose( $f );
preg_match_all( ”/
foreach( $bookblocks[1] as $block ){
preg_match_all( ”/
preg_match_all ( ”/
preg_match_all( ”/
echo( $title[1][0].” - ”.$author[1][0].” - ”. $publisher[1][0].”n ” );
}
/*
I never recommend using regular expressions to read XML, but sometimes it is the most compatible way because the regular expression functions are always available. Do not use regular expressions to read XML directly from the user because you have no control over the format or structure of such XML. You should always use a DOM library or a SAX parser to read XML from the user.
*/
//Write XML using DOM
$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();
/*
在脚本的顶部,用一些示例图书装入了 books 数组。这个数据可以来自用户也可以来自数据库。
示例图书装入之后,脚本创建一个 new DOMDocument,并把根节点 books 添加到它。然后脚本为每本书 的 author、title 和 publisher 创建节点,并为每个节点添加文本节点。每个 book 节点的最后一步是重新把它添加到根节 点 books。
使用 DOM 的真正价值在于它创建的 XML 总是格式正确的。但是如果不能用 DOM 创建 XML 时该怎么办?
Xml代码
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”
);
?>
foreach( $books as $book )
{
?>
}
?>
实例中用到的 test.xml 如下:

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools
