PHP的XML模式详解
研究与 php(做为现在的主流开发语言) 5 捆绑在一起的 xml(标准化越来越近了)Reader 库,它使 php(做为现在的主流开发语言) 页面能够以高效的流模式来处理 xml(标准化越来越近了) 文档。
php(做为现在的主流开发语言) 5 引入了新的类 xml(标准化越来越近了)Reader,用于读取可扩展标记语言(Extensible Markup Language,xml(标准化越来越近了))。与 Simplexml(标准化越来越近了) 或文档对象模型(Document Object Model,DOM)不同,xml(标准化越来越近了)Reader 以流模式进行操作。即它从头到尾读取文档。在文档后面的内容编译完成之前,可以先处理已编译好的文档前面的内容,从而实现非常快速、非常高效、非常节省地使用内存。需要处理的文档越大,这个特点就越发重要。
libxml(标准化越来越近了)
这里所说的 xml(标准化越来越近了)Reader API 位于 Gnome Project 中用于 C 和 C++ 的 libxml(标准化越来越近了) 库之上。实际上 xml(标准化越来越近了)Reader 只是在 libxml(标准化越来越近了) 的 xml(标准化越来越近了)TextReader API 之上的很薄的 php(做为现在的主流开发语言) 层。xml(标准化越来越近了)TextReader 本身是模仿 .NET 的 xml(标准化越来越近了)TextReader 类和 xml(标准化越来越近了)Reader 类,尽管并不具有与这些类相似的代码。
与 Simple API for xml(标准化越来越近了) (SAX) 不同,xml(标准化越来越近了)Reader 是推解析器,而不是拉解析器。这意味着程序是可以控制的。您将告诉解析器何时获取下一个文档片段,而不是在解析器看到文档后告诉您所看到的内容。您将请求内容,而不是对内容进行反应。从另一个角度来考虑这个问题:xml(标准化越来越近了)Reader 是 Iterator 设计模式的实现,而不是 Observer 设计模式的实现。
示例问题
先从简单例子开始讨论。假定正在编写 php(做为现在的主流开发语言) 脚本,用来接收 xml(标准化越来越近了)-RPC 请求并生成响应。更具体一些,假定请求如清单 1 所示。文档的根元素是 methodCall,它包含 methodName 元素和 params 元素。方法的名称是 sqrt.params 元素包含一个 param 元素,param 元素包含 double,double 的平方根是希望得到的值。没有使用名称空间。
清单 1. xml(标准化越来越近了)-RPC 请求
<b>以下是引用片段:<br></b><?xml <font class=reblank>(标准化越来越近了) version="1.0"?> <br><methodcall> <br> <methodname>sqrt</methodname> <br> <params> <br> <param> <br> <value><double>36.0</double></value> <br> <br> </params> <br></methodcall> Copy after login |
下面是 php(做为现在的主流开发语言) 脚本需要完成的工作:
1、检查方法名,如果不是 sqrt(它是该脚本懂得如何处理的惟一方法),则生成错误响应。
2、找到参数,如果参数不存在或参数类型错误,则生成错误响应。
3、另外,计算平方根。
4、在表单中返回结果,如清单 2 所示。
清单 2. xml(标准化越来越近了)-RPC 响应
<b>以下是引用片段:<br></b><?xml <font class=reblank>(标准化越来越近了) version="1.0"?> <br><methodresponse> <br> <params> <br> <param> <br> <value><double>6.0</double></value> <br> <br> </params> <br></methodresponse> Copy after login |
下面我们逐步展开说明。
初始化解析器并载入文档
第一步是创建新的解析器对象。创建操作很简单:
<b>以下是引用片段:<br></b>$reader = new xml<font class="reblank">(标准化越来越近了)</font>Reader(); Copy after login |
接着,需要为它提供一些用于解析的数据。对于 xml(标准化越来越近了)-RPC,这是超文本传输协议(Hypertext Transfer Protocol,HTTP)请求的原始主体。然后可以将该字符串传递到读取器的 xml(标准化越来越近了)() 函数:
填充原始发送数据
<b>以下是引用片段:<br></b> $request = $HTTP_RAW_POST_DATA; <br> $reader->xml<font class="reblank">(标准化越来越近了)</font>($request); Copy after login |
如果发现 $HTTP_RAW_POST_DATA 是空的,则将以下代码行添加到 php(做为现在的主流开发语言).ini 文件:
<b>以下是引用片段:<br></b> always_populate_raw_post_data = On Copy after login |
可以解析任何字符串,无论它是从何处获取的。例如,可以是程序中的一串文字或从本地文件读取。还可以使用 open() 函数从外部 URL 载入数据。例如,下面的语句准备解析其中一个 Atom 提要:
<b>以下是引用片段:<br></b> $reader->xml<font class="reblank">(标准化越来越近了)</font>('http://www.cafeaulait.org/today.atom'); Copy after login |
无论是从何处获取原始数据,现在已建立了阅读器并为解析做好准备。
读取文档
read() 函数使解析器前进到下一个标记。最简单的方法是在 while 循环中遍历整个文档:
<b>以下是引用片段:<br></b> while ($reader->read()) { <br> // processing code goes here... <br> } Copy after login |
完成遍历后,关闭解析器以释放它所持有的任何资源,并且重置解析器以便用于下一个文档:
<b>以下是引用片段:<br></b> $reader->close(); Copy after login |
在循环内部,将解析器放置在特殊节点上:元素的起点、元素的终点、文本节点、注释等等。通过检查以下属性,可以发现解析器正在查看的内容:
localName 是本地的、未带前缀的节点名。
name 是可能的节点前缀名。对于像注释这种没有名称的节点,包括 #comment、#text、#document 等等,与 DOM 中的一样。
namespaceURI 是节点名称空间的统一资源标识符(Uniform Resource Identifier,URI)。
nodeType 是代表节点类型的整数 —— 例如,2 代表属性节点,7 代表处理指令。
prefix 是节点的名称空间前缀。
value 是节点的下一个文本内容。
如果节点有文本值,hasValue 值为 true;否则,值为 false.

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

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

AdobeAcrobatReader is a powerful tool for viewing and editing PDF files. The software is available in both free and paid versions. If you need to use Adobe Acrobat Reader to edit PDF files, you need to purchase its paid plan. To keep Adobe Acrobat Reader up to date with the latest enhancements and security fixes, the software enables automatic updates by default. However, you can choose to disable automatic updates if you wish. This article will show you how to disable automatic updates in Adobe Acrobat Reader. How to disable automatic updates in Adobe Acrobat Reader us

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible
