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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.
