简述PHP4和PHP5版本下解析XML文档的操作方法
在PHP网站开发与建设过程中,时常会碰到需要对XML文档进行解析,PHP4版本自带了XML解析器(sax),PHP5版本增加了SimpleXML(基于dom)的XML扩展,对XML的解析更是非常方便,今天和大家分享下在不同环境下对XML文档进行解析的方法。
XML文档
1 |
version="1.0"
encoding="gbk"?> |
PHP5版本下SimpleXML的使用方法
1 |
$info
= simplexml_load_file('leapsoulcn.xml'); |
代码注释
第1行:simplexml_load_file:读取一个xml文档作为操作对象,可以读取本地或者远程xml文档;simplexml_load_string:读取一个xml字符串作为操作的对象
第3行:如果不清楚如何获取某个节点的信息,可用print_r函数打印输出查看具体的结构,simplexml解析返回的对象具有数组结构。
第5~8行:以对象方式读取某个XML文档节点信息,读取方式:句柄->节点元素名->子节点,如果相同的节点元素有多个,则以数组(array)方式读取
注:由于simplexml解析返回的信息是UTF8格式的,如果网站使用的是GBK的,则需要转码,你可以使用iconv函数或者其他的utf8与gbk转换函数进行操作,如:$name = iconv(’utf-8′,’gbk’,$name);
第9~17行:以遍历的形式,读取所有元素下的子节点信息
第19~21行:simplexml的xpath函数是用来查询XML数据的,比如这里查询的是所有name节点的值
第23~26行:children函数是用来找寻某个特定节点下所有子节点的值。getName函数用来获得每个子节点的元素名称
第28~29行:addChild函数用来在某个特定节点下增加一个子节点;asXML函数对已做过改动的XML文档进行保存
点击查看更多SimpleXML函数说明。
PHP4版本自带的XML函数解析方法
1 |
function doStartElement($xmlParser,$name,$attr) |
代码注释
第1~15行:定义开始元素,结尾元素以及元素间字符数据的处理函数
第16行:建立一个新的XML解析器并返回可被其它XML函数使用的资源句柄,输入默认编码方式为 “ISO-8859-1”。同时,支持的编码方式还有“UTF-8”和 “US-ASCII”,输出数据编码方式是和xml_parser_create函数处定义相一致。
第18行:建立起始和终止元素处理器
第20行:建立字符数据处理器,处理元素之间的相关数据
注:XML语法解析器不会加上或者去掉任何空格,空格的取舍将由开发者自己决定。
第22行:启动解析器xml_parse函数第二个参数是字符型的,所以需要读取相关的XML文档信息,此处我用的是file_get_contents函数,本地远程都可行,当然你也可以使用fopen函数
第24行:XML解析完成后,释放解析器占用的内存
自带的XML函数解析思路
第一步:自定义开始元素,结尾元素以及元素间字符数据的处理函数;第二步:建立一个XML解析器;第三步:建立起始和终止元素处理器;第四步:建立字符数据处理器,处理元素之间的相关数据;第五步:启动解析器;第六步:XML解析完成后,释放解析器占用的内存。
总结:
总的来说PHP自带的XML解析器使用起来相当繁琐,需要有较强的逻辑,而且如果XML文档结构不同,元素、字符等3个函数需要重新定义,而在PHP5版本增加了SimpleXML扩展后,XML解析起来非常简单,当然如果你需要进行更复杂的XML文档解析操作,你也可以下载安装并使用libxml,功能相当强。
注:PHP网站开发教程-leapsoul.cn版权所有,转载时请以链接形式注明原始出处及本声明,谢谢。

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



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

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of
