Home Backend Development PHP Tutorial PHP XML operation (reading) encapsulation class instance

PHP XML operation (reading) encapsulation class instance

Feb 25, 2017 pm 04:30 PM

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>
Copy after login

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;;
  }
}
?>
Copy after login

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

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

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.

What is REST API design principles? What is REST API design principles? Apr 04, 2025 am 12:01 AM

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.

How do you handle exceptions effectively in PHP (try, catch, finally, throw)? How do you handle exceptions effectively in PHP (try, catch, finally, throw)? Apr 05, 2025 am 12:03 AM

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.

What are anonymous classes in PHP and when might you use them? What are anonymous classes in PHP and when might you use them? Apr 04, 2025 am 12:02 AM

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.

What is the difference between include, require, include_once, require_once? What is the difference between include, require, include_once, require_once? Apr 05, 2025 am 12:07 AM

In PHP, the difference between include, require, include_once, require_once is: 1) include generates a warning and continues to execute, 2) require generates a fatal error and stops execution, 3) include_once and require_once prevent repeated inclusions. The choice of these functions depends on the importance of the file and whether it is necessary to prevent duplicate inclusion. Rational use can improve the readability and maintainability of the code.

Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles