當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。