当前位置: 首页>>代码示例>>PHP>>正文


PHP DOMDocument::hasAttributes方法代码示例

本文整理汇总了PHP中DOMDocument::hasAttributes方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMDocument::hasAttributes方法的具体用法?PHP DOMDocument::hasAttributes怎么用?PHP DOMDocument::hasAttributes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DOMDocument的用法示例。


在下文中一共展示了DOMDocument::hasAttributes方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: convert

 /**
  * Convert xml node to array or string
  *
  * @param \DOMDocument $node
  * @return array|string
  */
 public function convert($node)
 {
     $result = [];
     if ($node->nodeType == XML_TEXT_NODE) {
         $result = $node->nodeValue;
     } else {
         if ($node->hasChildNodes()) {
             $children = $node->childNodes;
             for ($i = 0; $i < $children->length; $i++) {
                 $child = $children->item($i);
                 if ($child->nodeName != '#text') {
                     $result[$child->nodeName][] = $this->convert($child);
                 } else {
                     if ($child->nodeName == '#text') {
                         $text = $this->convert($child);
                         if (trim($text) != '') {
                             $result[$child->nodeName] = $this->convert($child);
                         }
                     }
                 }
             }
         }
         if ($node->hasAttributes()) {
             $attributes = $node->attributes;
             if (!is_null($attributes)) {
                 foreach ($attributes as $attribute) {
                     $result[$attribute->name] = $attribute->value;
                 }
             }
         }
     }
     return $result;
 }
开发者ID:Mohitsahu123,项目名称:mtf,代码行数:39,代码来源:Converter.php

示例2: getArrayFromXml

 /**
  * Convert opml xml node into array for import
  * http://www.php.net/manual/en/class.domdocument.php#101014
  *
  * @param DOMDocument $node Node to convert into array
  *
  * @return array            Array corresponding to the given node
  */
 public static function getArrayFromXml($node)
 {
     $array = false;
     if ($node->hasAttributes()) {
         foreach ($node->attributes as $attr) {
             $array[$attr->nodeName] = $attr->nodeValue;
         }
     }
     if ($node->hasChildNodes()) {
         if ($node->childNodes->length == 1) {
             $array[$node->firstChild->nodeName] = $node->firstChild->nodeValue;
         } else {
             foreach ($node->childNodes as $childNode) {
                 if ($childNode->nodeType != XML_TEXT_NODE) {
                     $array[$childNode->nodeName][] = Opml::getArrayFromXml($childNode);
                 }
             }
         }
     }
     return $array;
 }
开发者ID:inscriptionweb,项目名称:kriss_feed,代码行数:29,代码来源:Opml.php

示例3: convertXMLToArray

 /**
  * @param \DOMDocument|string $xml $xml
  * @return array
  */
 public function convertXMLToArray($xml)
 {
     $result = array();
     if ($xml->hasAttributes()) {
         $attrs = $xml->attributes;
         foreach ($attrs as $attr) {
             $result['@attributes'][$attr->name] = $attr->value;
         }
     }
     if ($xml->hasChildNodes()) {
         $children = $xml->childNodes;
         if ($children->length == 1) {
             $child = $children->item(0);
             if ($child->nodeType == XML_TEXT_NODE || $child->nodeType == XML_CDATA_SECTION_NODE) {
                 $result['_value'] = $child->nodeValue;
                 return count($result) == 1 ? $result['_value'] : $result;
             }
         }
         $groups = array();
         foreach ($children as $child) {
             if (!isset($result[$child->nodeName])) {
                 $result[$child->nodeName] = $this->convertXMLToArray($child);
             } else {
                 if (!isset($groups[$child->nodeName])) {
                     $result[$child->nodeName] = array($result[$child->nodeName]);
                     $groups[$child->nodeName] = 1;
                 }
                 $result[$child->nodeName][] = $this->convertXMLToArray($child);
             }
             if (empty($result[$child->nodeName])) {
                 unset($result[$child->nodeName]);
             } elseif (is_array($result[$child->nodeName])) {
                 $values = array();
                 foreach ($result[$child->nodeName] as $key => $val) {
                     if (!empty($result[$child->nodeName][$key])) {
                         $values[] = $result[$child->nodeName][$key];
                         if (count($values) > 1) {
                             break;
                         }
                     }
                 }
                 if (empty($values)) {
                     unset($result[$child->nodeName]);
                 }
             }
         }
     }
     return $result;
 }
开发者ID:NavalKishor,项目名称:PHP-Rocker,代码行数:53,代码来源:ArrayConverter.php


注:本文中的DOMDocument::hasAttributes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。