search
HomeBackend DevelopmentPHP Tutorial用PHP与XML联手进行网站编程代码实例_php技巧

一、小序 

  HTML简单易学又通用,一般的PHP程序就是嵌入在HTML语言之中实现的。但是随着WEB越来越广泛的应用,HTML的弱点也越来越明显了。XML的出现,弥补了这些不足,它提供了一个能够处理互联网上全部数据的通用方法。

  二、HTML的局限性分析

  1、 HTML的可扩展性差。虽然作为一般的应用,HTML应经够用了,但是在处理数学和化学等符号时,HTML有明显的缺点,而且它无法进行扩展,这样使它的发展收到了极大的限制。

  2、 链路丢失后不能自动纠正。由于Web页面的URL地址经常改变,而在改变URL地址时必须手工修改这些信息,否则就会遇到“404URL地址未找到”的信息,这大大加重了Web页面的维护工作量。

  3、 数据搜索的时间长。由于HTML主要用来对网页的显示进行控制,导致了同一个数据在不同的网页中有不同的存储格式,这样在进行数据搜索时就无法快速找到所需的资料。

  4、 HTML对双字节或者多国文字的支持不够。例如中文信息页面在不同的平台下会出现无法显示等问题。

  正是由于这些缺点,人们研究了能够代替HTML的Web页面制作语言。其中已经投入使用的有:可扩展标记语言XML、层叠样式表(CSS)以及动态HTML(DHTML)等。

  三、XML的组成

  这里简要列举几种主要的XML技术:

  1、 DTD(文档类型声明)

  DTD的主要功能是定义XML的内容模式;限制XML标记的数据范围;定义属性的数据类型。但由于它不是用XML编写的,因此扩展性比较差;而且只提供了有限的几种数据类型,因此它的作用是有限的。

  2、 XML Schema

  XML Schema的作用和DTD类似。但不同的是,Schema文件所描述的是引用它的XML文件中的元素和属性的具体类型。另外,由于它是由XML编写的,Schema和DTD相比较还有以下优点:

  ·XML Schema内容模型是开放的,可以随意扩充,而DTD无法解析扩充的内容。

  ·DTD只能把内容类型定义为一个字符串,而XML Schema允许把内容类型定义为整型、浮点型、布尔型或者许多其它的简单数据类型。

  ·XML Schema利用Namespaces将文档中特殊的节点与Schema相联系,一个XML文件可以有多个对应的Schema,而一个XML文件只能有一个DTD。

  3、 XLink

  作为一种Web语言,XML的链接能力是非常重要的。XML的链接和定址机制包括XLink、XPath和XPointer。XLink提供功能强大的链接方法,可以在文档之间建立单向或多向的复杂联结关系,还有注释链接、概要链接、扩展链接集等多种链接功能。XPath在XSLT和XPointer中使用,支持在XML文档中相对于节点和节点集的定位。XPointer在XPath的基础上提供对XML文档的内容的内部结构(如一个字符串或者选择的一个段落)的定位。XML的链接能力比HTML有了很大的增强。

  4、 CSS与XSL

  XML的一大特点就是内容与格式分离,也就是说,XML文档中并不包含如何显示/表示文档的信息。CSS与XSL(XML Style Language)解决了XML文档的显示问题。

  CSS(层叠样式表)也可以用在HTML和XML中。XSL完全使用XML的语法,功能比CSS要强大得多。

  5、 DOM

  文档对象模型(DOM)是一个与平台、语言无关的程序接口,它提供了动态访问和更新文档的内容、结构与风格的手段。可以对文当作进一步的处理,并将处理的结果更新到表示页面。

  DOM的目标就是为XML和HTML定义一个标准的编程接口,它包括核心、HTML和XML三部分。DOM的核心部分建立了一套底层的对象集,它们可以表示任何结构化的文档。HTML和XML提供了高层的接口,可以作为更方便的文档视图。DOM规范由对象和方法组成。程序员使用它们可以更容易地对特定类型的文档进行访问和操作。

  6、 Namespaces

  Namespaces是用URL加以区别的、在XML文件的元素和属性中出现的所有名称的集合。在XML中,用户可以自己定义标记和元素。因此,如果把多个XML文件合并为一个,就很可能出现冲突。Namespaces则解决了这个问题。


  四、PHP对XML的支持

  PHP对XML提供了的强大的支持。它使用了一个XML的“解析器”,并且为了支持这个解析器,它提供了20(PHP4)个XML的解析函数。下面是几个最常用的PHP解析函数。

  1. xml_parse

boolean xml_parse(int parser, string data, int [isFinal]); 

  本函数用来解析 XML 格式的文件资料。参数 parser 为解析代码。参数 data 为解析的资料区块 (chunk)。参数 isFinal 可省略,若设为 true 则系统会自动送出最后的资料部分 (piece) 给 data 参数。若无错误则返回 true 值。

  2. xml_parser_create

int xml_parser_create(string [encoding]); 

  本函数用来初始化一个新的 XML 解析器。参数 encoding 可省略,为 XML 使用的字符集,默认值为 ISO-8859-1,其它尚有 US-ASCII、UTF-8 二种。成功则返回 parser 代码供其它函数使用,失败则返回 false 值。

  3. xml_set_element_handler

boolean xml_set_element_handler(int parser, string startElementHandler, string endElementHandler); 

  本函数配置元素的标头供 xml_parse() 函数使用。参数 parser 为解析代码。参数 startElementHandler 及 endElementHandler 分别为元素开始与结束的标头,其中的 startElementHandler 必须包括解析代码、名称、与属性,而 endElementHandler 参数包括了解析代码及名称二个参数。若无错误则返回 true 值。

  4. xml_set_character_data_handler

boolean xml_set_character_data_handler(int parser, string handler); 


  本函数配置字符资料的标头。参数 parser 为解析代码。参数 handler 包括解析代码及资料字符串等二个元素。若无错误则返回 true 值。

  5. xml_get_error_code

int xml_get_error_code(int parser); 

  本函数可取得 XML 在处理时的错误代码。参数 parser 为解析代码。若 parser 有错则返回 false 值,否则就返回错误代码 (如 XML_ERROR_BINARY_ENTITY_REF .... 等等)。

  6. xml_error_string

string xml_error_string(int code); 

  本函数可取得 XML 在处理时的错误代码。参数 code 为解析错误代码。若无错误返回值为代码的文字描述字符串。

  7. xml_get_current_line_number

int xml_get_current_line_number(int parser);   

  本函数用来取得目前 XML 解析所正在处理的行号。参数 parser 为解析代码。若 parser 有错则返回 false 值,若无错误则返回行号数字。

  8. xml_parser_free

boolean xml_parser_free(int parser); 

  本函数用来释放目前 XML 解析所使用的内存。参数 parser 为解析代码。若没有错误则返回 true 值,否则返回 false 值。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor