PHP XML operation (reading) encapsulation class instance
The example of this article describes the XML operation (reading) encapsulation class implemented by PHP. Share it with everyone for your reference, the details are as follows:
<?xml version="1.0" encoding="utf-8" standalone="no"?> <CustomizationSetting version='1.0' > <!-- 各通用配置信息,作为Settings子节点,方便统一解析 --> <!-- AgentSettings 由网页定制生成,默认无次节点--> <AgentSettings> <!-- 以Setting 作为节点名,考虑扩展时,不需修改客户端接口。 type类型方便网页进行显示控件区分 --> <!-- 代理定制类型,默认:Normal。在网页区分语言定制的情况下,以英文、或列表第一种语言的修改为准 --> <Setting name='CustomizeType' type='enum'>Normal|DR</Setting> <!-- 产品名称,AgentType为DR时,用于获取代理购买地址 --> <Setting name='AppName' type='string'>Wondershare Vedio Convertor</Setting> <!-- 品牌名称,AgentType为DR时,用于获取代理购买地址 --> <Setting name='Publisher' type='string'>Wondershare</Setting> <!-- 购买地址生成方式,默认:Customize,由网页生成,默认无次节点 --> <Setting name='OrderPageGenType' type='enum'>Customize|Affiliate</Setting> <!-- Affiliate ID, OrderPageGenType为Affiliate才有效,默认为空值 --> <Setting name='AID' type='string'>143724583</Setting> <!-- 启动Affiliat 购买地址启用短链接, OrderPageGenType为Affiliate才有效,默认:0 --> <Setting name='UseShortAffiliateOrderLink' type='bool'>0|1</Setting> <!-- 定制输出名称 --> <Setting name='OutputFileName' type='string'><![CDATA[1464646.mac-transpod-lite_full879.dmg]]></Setting> </AgentSettings> <!-- 各语言定制信息,作为Languages子节点,方便统一解析, 以及扩展 --> <Languages> <!-- 语言名称,统一使用CBS后台语言数据规范 --> <Language name='English' > <Settings> <!-- 产品ID,多语言中可被定制 --> <Setting name='PID' type='stirng'>11642</Setting> <!-- 支持弹出广告,默认:1,最终能否弹广告还取决于网站的设置,这里主要用于关闭的总控制 --> <Setting name='EnabelAD' type='bool'>1|0</Setting> <!-- 支持升级,默认:1 --> <Setting name='EnableUpdate' type='bool'>1|0</Setting> <!-- CBS跳转链接类型,只对InstallJumpPage,UninstallJumpPage,RegisterJumpPage,UpdateJumpPage有效,默认:Jump --> <Setting name='CBSJumpType' type='enum'>Jump|Hide|Disable</Setting> <!-- 支持Tisp消息--> <Setting name='TipsEnable' type='bool'>1|0</Setting> <!-- 支持弹出产品目录推荐, 默认:1--> <Setting name='EnableProductSuit' type='bool'>1|0</Setting> </Settings> <!-- 链接地址作为URLS子节点,方便统一解析 --> <URLS> <!-- 不使用'**Page'直接作为节点名称,避免自定义地址的名称与XML节点名称规范冲突,如就数字开头,同时支持网页界面根据不同产品显示链接名称,而且扩展时,而不需修改客户端接口 --> <!-- 购买地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <!-- 区分地址类型order, jump, comm, custom方便网页显示归类 --> <URL name='OrderPage' type='order' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 安装跳转地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='InstallJumpPage' type='jump' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 卸载跳转地址,Mac产品无此项。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='UninstallJumpPage' type='jump' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 注册跳转地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='RegisterJumpPage' type='jump' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 升级跳转地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='UpdateJumpPage' type='jump' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 公司主页地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='HomePage' type='comm' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 产品主页地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='ProductPage' type='comm' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 支持地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='SupportPage' type='comm' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 联系客服地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='ContactPage' type='comm' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 在线帮助地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='HelpOnlinePage' type='comm' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 自定义地址,最多可定义5个。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='3DPage' type='custom' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> <!-- 自定义地址,最多可定义5个。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='GPUPage' type='custom' updateReserve='0|1'><![CDATA[http://www.php.cn/]]></URL> </URLS> <!-- 待扩展信息,在客户端接口中预先处理 --> </Language> <!-- 产品不支持某种语言,则不需要该<Language>节点 --> <Language name='Traditional Chinese(HK)'> <!-- 同上 --> </Language> <Language name='Chinese'> <!-- 同上 --> </Language> <Language name='Portuguese'> <!-- 同上 --> </Language> <Language name='Japanese'> <!-- 同上 --> </Language> <Language name='Korean'> <!-- 同上 --> </Language> <Language name='Hebrew'> <!-- 同上 --> </Language> <Language name='Arabic'> <!-- 同上 --> </Language> <Language name='Spanish'> <!-- 同上 --> </Language> <Language name='Polish'> <!-- 同上 --> </Language> <Language name='German'> <!-- 同上 --> </Language> <Language name='French'> <!-- 同上 --> </Language> <Language name='Italian'> <!-- 同上 --> </Language> <Language name='Spanish(Spain)'> <!-- 同上 --> </Language> <Language name='Spanish(Mexico)'> <!-- 同上 --> </Language> <Language name='Traditional Chinese(TW)'> <!-- 同上 --> </Language> <Language name='Dutch'> <!-- 同上 --> </Language> <Language name='Russian'> <!-- 同上 --> </Language> </Languages> </CustomizationSetting>
Encapsulates the read xml operation class:
<?php /** * xml操作基类 */ class XmlBase { public function __construct() { } /** * 加载xml * @param string $xml xml路径或xml字符串 * @param bool $isXmlStr 是否是xml字符串 */ protected function _loadXml($xml, $isXmlStr = FALSE) { $xmlObj = new DOMDocument(); if ($isXmlStr) { $xmlObj->loadXML($xmlNewStr); } else { $xmlObj->load($xml); } return $xmlObj; } /** * 获取某个标签节点 * @param $parentnode 父节点 * @param $tagName 标签名 // 不区分大小写 * @return node 节点对象 */ protected function _getNode($parentNode, $tagName) { $childNodes = $this->_getNodes($parentNode); foreach ($childNodes as $node) { $nodeName = strtolower(trim($node->nodeName)); $tagName = strtolower(trim($tagName)); if ($nodeName == $tagName) { return $node; } } return NULL; } /** * 获取xml子节点 * @param $parentNode 父节点 * @param $tagName 子节点标签 // 不区分大小写 */ protected function _getNodes($parentNode, $tagName = '') { $nodes = array(); if ($tagName) { $nodes = $this->_getSpecialNodes($parentNode, $tagName); } else { $nodes = $this->_getAllNodes($parentNode); } return $nodes; } /** * 获取所有的节点 * @param $parentNode 父节点 */ protected function _getAllNodes($parentNode) { $nodes = array(); foreach ($parentNode->childNodes as $node) { if ($node->nodeType == 1) { $nodes[] = $node; } } return $nodes; } /** * 获取指定标签的节点 * @param $parentNode 父节点 * @param $tagName 节点名称 */ protected function _getSpecialNodes($parentNode, $tagName) { $nodes = array(); $tagName = strtolower(trim($tagName)); foreach ($parentNode->childNodes as $node) { $nodeName = strtolower(trim($node->nodeName)); if ($node->nodeType == 1 && $nodeName == $tagName) { $nodes[] = $node; } } return $nodes; } /** * 获取节点属性值 * * @param $node 节点对象 * @param $attrName 节点名字 // 不区分大小写 */ protected function _getAttr($node, $attrName) { $attrName = strtolower($attrName); foreach ($node->attributes as $attr) { $nodeName = strtolower($attr->nodeName); //$nodeValue = strtolower($attr->nodeValue); $nodeType = strtolower($attr->nodeType); if ($nodeType == 2 && $nodeName == $attrName) { unset($attrName, $nodeName, $nodeType); return $attr->nodeValue; } } return ''; } } ?>
For more articles related to PHP’s XML operation (reading) encapsulation class examples, please pay attention to the PHP Chinese website!

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,

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

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.

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.
