


XML operation implemented by php (complete instance analysis of encapsulated class
This article mainly introduces the XML operation (reading) encapsulation class implemented by PHP, gives an example of an XML format file, and analyzes the relevant operating skills of PHP traversing and reading XML format data nodes in the form of a complete example, which requires Friends can refer to
for details 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://...]]></URL> <!-- 安装跳转地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='InstallJumpPage' type='jump' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 卸载跳转地址,Mac产品无此项。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='UninstallJumpPage' type='jump' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 注册跳转地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='RegisterJumpPage' type='jump' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 升级跳转地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='UpdateJumpPage' type='jump' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 公司主页地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='HomePage' type='comm' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 产品主页地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='ProductPage' type='comm' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 支持地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='SupportPage' type='comm' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 联系客服地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='ContactPage' type='comm' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 在线帮助地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='HelpOnlinePage' type='comm' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 自定义地址,最多可定义5个。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='3DPage' type='custom' updateReserve='0|1'><![CDATA[http://...]]></URL> <!-- 自定义地址,最多可定义5个。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 --> <URL name='GPUPage' type='custom' updateReserve='0|1'><![CDATA[http://...]]></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 ''; } } ?>
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP implementation method of reading xml files
Summary of examples of common methods for php to read XML_php skills
Characteristics and syntax instructions for PHP and XML technology
The above is the detailed content of XML operation implemented by php (complete instance analysis of encapsulated class. For more information, please follow other related articles on the PHP Chinese website!

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
