PHP 自学之路-----XML编程(Xpath技术,simpleXml技术)基础入门
XPath的设计的核心思想,可以通过xpath迅速简介的定位到你希望查找的节点。主要目的是描述节点相对其他节点的位置,可以取得所有符合条件的节点,成为[位置路径]。
Xapth主要用来在XML文档中查询信息的工具,通过使用路径表达式可以解析XML文件,读取XML文件中的数据
下面以案例来介绍,XML 文件如下(simplexml.xml):
[php]
sb001
sb002
PHP 代码如下:
[php]
//1 创建DOMDocument对象
$dom = new DOMDocument("1.0", "UTF-8");
//2 加载Xml文件
$dom->load("simplexml.xml");
//3 转成DomXPath对象
$xpath = new DOMXPath($dom);
//4 查找工作
$node_list = $xpath->query("/books/book/title");
echo "一共有".$node_list->length."个
";
for($i=0; $ilength;$i++){
$node = $node_list->item($i);
// var_dump($node_list);
/*foreach ($node as $key=>$val){
echo
} */
echo $node->nodeName.":".$node->nodeValue."";
}
?>
输入如下:
一共有2个
title:tianlongbabu
title:xiaoaojianghu
想更深入得了解XPath的使用,
simpleXml
simpleXMl的核心思想:以面向对象的方法来操作Xml文件。simpleXml是比较简单的方式,通常会和XPath配合使用,
[php]
//装载方式1 ‘装载一个Xml文件
// $lib = simplexml_load_file("simplexml.xml");
//2 DOM 对象装载方式
//1 创建DOMDocument对象
$dom = new DOMDocument("1.0", "UTF-8");
//2 加载Xml文件
$dom->load("simplexml.xml");
$lib = simplexml_import_dom($dom);
echo "
"; <p> // var_dump($lib);/*1*/ </p> <p> echo "</p>";
//取出书,
$books = $lib->book;
/* //取出第一本书
$book1=$books[0];
//取出书名
echo $book1->title.":".$book1->author; */
for($i=0;$i $book1=$books[$i]; //取出属性 echo "属性:".$book1['house']." echo "第".$i."本书的 书名||作者 echo $book1->title."||".$book1->author." } echo "***simplexml 和xpath结合使用*** echo "取出所有的书名: $titles = $lib->xpath("//title"); foreach ($titles as $key=>$val){ echo " } echo " ?> 输出如下: [php] 属性:qinghuachubanshe 第0本书的 书名||作者 tianlongbabu||xiaoming 属性:beidachubanshe 第1本书的 书名||作者 xiaoaojianghu||daming ***simplexml 和xpath结合使用*** 取出所有的书名: 0||tianlongbabu 1||xiaoaojianghu ********************************* 第三种转载Xml方式,字符串方式,如下: [php]
echo " $string =
";
";
";
";
";
".$key."||".$val;
*********************************";
simplexml也可以直接装载一个string字符串(Xml规范)";
I know that's the answer -- but what's the question?
XML;
$xml = simplexml_load_string($string);
print_r($xml);
?>

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools
