search
HomeBackend DevelopmentPHP TutorialPHP 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=&#39;1.0&#39; >
  <!-- 各通用配置信息,作为Settings子节点,方便统一解析 -->
  <!-- AgentSettings 由网页定制生成,默认无次节点-->
  <AgentSettings>
    <!-- 以Setting 作为节点名,考虑扩展时,不需修改客户端接口。 type类型方便网页进行显示控件区分 -->
    <!-- 代理定制类型,默认:Normal。在网页区分语言定制的情况下,以英文、或列表第一种语言的修改为准 -->
    <Setting name=&#39;CustomizeType&#39; type=&#39;enum&#39;>Normal|DR</Setting>
    <!-- 产品名称,AgentType为DR时,用于获取代理购买地址 -->
    <Setting name=&#39;AppName&#39; type=&#39;string&#39;>Wondershare Vedio Convertor</Setting>
    <!-- 品牌名称,AgentType为DR时,用于获取代理购买地址 -->
    <Setting name=&#39;Publisher&#39; type=&#39;string&#39;>Wondershare</Setting>
    <!-- 购买地址生成方式,默认:Customize,由网页生成,默认无次节点 -->
    <Setting name=&#39;OrderPageGenType&#39; type=&#39;enum&#39;>Customize|Affiliate</Setting>
    <!-- Affiliate ID, OrderPageGenType为Affiliate才有效,默认为空值 -->
    <Setting name=&#39;AID&#39; type=&#39;string&#39;>143724583</Setting>
    <!-- 启动Affiliat 购买地址启用短链接, OrderPageGenType为Affiliate才有效,默认:0 -->
    <Setting name=&#39;UseShortAffiliateOrderLink&#39; type=&#39;bool&#39;>0|1</Setting>
    <!-- 定制输出名称 -->
    <Setting name=&#39;OutputFileName&#39; type=&#39;string&#39;><![CDATA[1464646.mac-transpod-lite_full879.dmg]]></Setting>
  </AgentSettings>
  <!-- 各语言定制信息,作为Languages子节点,方便统一解析, 以及扩展 -->
  <Languages>
    <!-- 语言名称,统一使用CBS后台语言数据规范 -->
    <Language name=&#39;English&#39; >
      <Settings>
        <!-- 产品ID,多语言中可被定制 -->
        <Setting name=&#39;PID&#39; type=&#39;stirng&#39;>11642</Setting>
        <!-- 支持弹出广告,默认:1,最终能否弹广告还取决于网站的设置,这里主要用于关闭的总控制 -->
        <Setting name=&#39;EnabelAD&#39; type=&#39;bool&#39;>1|0</Setting>
        <!-- 支持升级,默认:1 -->
        <Setting name=&#39;EnableUpdate&#39; type=&#39;bool&#39;>1|0</Setting>
        <!-- CBS跳转链接类型,只对InstallJumpPage,UninstallJumpPage,RegisterJumpPage,UpdateJumpPage有效,默认:Jump -->
        <Setting name=&#39;CBSJumpType&#39; type=&#39;enum&#39;>Jump|Hide|Disable</Setting>
        <!-- 支持Tisp消息-->
        <Setting name=&#39;TipsEnable&#39; type=&#39;bool&#39;>1|0</Setting>
        <!-- 支持弹出产品目录推荐, 默认:1-->
        <Setting name=&#39;EnableProductSuit&#39; type=&#39;bool&#39;>1|0</Setting>
      </Settings>
      <!-- 链接地址作为URLS子节点,方便统一解析 -->
      <URLS>
        <!-- 不使用&#39;**Page&#39;直接作为节点名称,避免自定义地址的名称与XML节点名称规范冲突,如就数字开头,同时支持网页界面根据不同产品显示链接名称,而且扩展时,而不需修改客户端接口 -->
        <!-- 购买地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <!-- 区分地址类型order, jump, comm, custom方便网页显示归类 -->
        <URL name=&#39;OrderPage&#39; type=&#39;order&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 安装跳转地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;InstallJumpPage&#39; type=&#39;jump&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 卸载跳转地址,Mac产品无此项。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;UninstallJumpPage&#39; type=&#39;jump&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 注册跳转地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;RegisterJumpPage&#39; type=&#39;jump&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 升级跳转地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;UpdateJumpPage&#39; type=&#39;jump&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 公司主页地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;HomePage&#39; type=&#39;comm&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 产品主页地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;ProductPage&#39; type=&#39;comm&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 支持地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;SupportPage&#39; type=&#39;comm&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 联系客服地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;ContactPage&#39; type=&#39;comm&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 在线帮助地址。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;HelpOnlinePage&#39; type=&#39;comm&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 自定义地址,最多可定义5个。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;3DPage&#39; type=&#39;custom&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
        <!-- 自定义地址,最多可定义5个。属性updateReserve表示产品升级保留定制信息,默认:0, 进入定制页面后,默认修改为1 -->
        <URL name=&#39;GPUPage&#39; type=&#39;custom&#39; updateReserve=&#39;0|1&#39;><![CDATA[http://www.php.cn/]]></URL>
      </URLS>
      <!-- 待扩展信息,在客户端接口中预先处理 -->
    </Language>
    <!-- 产品不支持某种语言,则不需要该<Language>节点 -->
    <Language name=&#39;Traditional Chinese(HK)&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Chinese&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Portuguese&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Japanese&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Korean&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Hebrew&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Arabic&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Spanish&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Polish&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;German&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;French&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Italian&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Spanish(Spain)&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Spanish(Mexico)&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Traditional Chinese(TW)&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Dutch&#39;>
        <!-- 同上 -->
    </Language>
    <Language name=&#39;Russian&#39;>
        <!-- 同上 -->
    </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 = &#39;&#39;) {
    $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 &#39;&#39;;
  }
}
?>

For more articles related to PHP’s XML operation (reading) encapsulation class examples, please pay attention to the PHP Chinese website!

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
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

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),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function