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:
Normal|DR Wondershare Vedio Convertor Wondershare Customize|Affiliate 143724583 0|1 11642 1|0 1|0 Jump|Hide|Disable 1|0 1|0
Encapsulates the read xml operation class:
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 readingxmlfiles
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!