PHP Doku:: The SimpleXMLIterator class - class.simplexmliterator.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige GrunderweiterungenStandard PHP Library (SPL)IteratorenThe SimpleXMLIterator class

Ein Service von Reinhard Neidl - Webprogrammierung.

Iteratoren

<<RegexIterator::setPregFlags

SimpleXMLIterator::current>>


UnterSeiten:

The SimpleXMLIterator class

Einführung

The SimpleXMLIterator provides recursive iteration over all nodes of a SimpleXMLElement object.

Klassenbeschreibung

SimpleXMLIterator extends SimpleXMLElement implements RecursiveIterator , Traversable , Iterator , Countable {
/* Methoden */
public mixed current ( void )
object getChildren ( void )
bool hasChildren ( void )
mixed key ( void )
void next ( void )
void rewind ( void )
bool valid ( void )
/* Geerbte Methoden */
public SimpleXMLElement::__construct ( string $data [, int $options = 0 [, bool $data_is_url = false [, string $ns = "" [, bool $is_prefix = false ]]]] )
public void SimpleXMLElement::addAttribute ( string $name , string $value [, string $namespace ] )
public SimpleXMLElement SimpleXMLElement::addChild ( string $name [, string $value [, string $namespace ]] )
public mixed SimpleXMLElement::asXML ([ string $filename ] )
public SimpleXMLElement SimpleXMLElement::attributes ([ string $ns = NULL [, bool $is_prefix = false ]] )
public SimpleXMLElement SimpleXMLElement::children ([ string $ns [, bool $is_prefix = false ]] )
public integer SimpleXMLElement::count ( void )
public array SimpleXMLElement::getDocNamespaces ([ bool $recursive = false ] )
public string SimpleXMLElement::getName ( void )
public array SimpleXMLElement::getNamespaces ([ bool $recursive = false ] )
public bool SimpleXMLElement::registerXPathNamespace ( string $prefix , string $ns )
public array SimpleXMLElement::xpath ( string $path )
}

Inhaltsverzeichnis


2 BenutzerBeiträge:
- Beiträge aktualisieren...
ratfactor at gmail dot com
20.07.2009 2:08
The documentation is a bit sparse for SimpleXmlIterator.  Here is an example showing the use of its methods. xml2Array and sxiToArray work together to convert an XML document to an associative array structure.

The contents of cats.xml:
======================================
<cats>
  <cat>
      <name>Jack</name>
      <age>2</age>
      <color>grey</color>
      <color>white</color>
  </cat>
  <cat>
      <name>Maxwell</name>
      <age>12</age>
      <color>orange</color>
      <color>black</color>
  </cat>
</cats>
======================================

<?php
function xml2array($fname){
 
$sxi = new SimpleXmlIterator($fname, null, true);
  return
sxiToArray($sxi);
}

function
sxiToArray($sxi){
 
$a = array();
  for(
$sxi->rewind(); $sxi->valid(); $sxi->next() ) {
    if(!
array_key_exists($sxi->key(), $a)){
     
$a[$sxi->key()] = array();
    }
    if(
$sxi->hasChildren()){
     
$a[$sxi->key()][] = sxiToArray($sxi->current());
    }
    else{
     
$a[$sxi->key()][] = strval($sxi->current());
    }
  }
  return
$a;
}

// Read cats.xml and print the results:
$catArray = xml2array('cats.xml');
print_r($catArray);
?>

Results (reformatted a bit for compactness and clarity):
======================================
Array(
  [cat] => Array(
    [0] => Array(
      [name] => Array(  [0] => Jack    )
      [age] => Array(   [0] => 2       )
      [color] => Array( [0] => grey,
                        [1] => white   )
    )
    [1] => Array(
      [name] => Array(  [0] => Maxwell )
      [age] => Array(   [0] => 12      )
      [color] => Array( [0] => orange
                        [1] => black   )
    )
  )
)
David Lanstein
18.01.2009 3:40
Unlike the DirectoryIterator, for one, you'll need to call $it->rewind() before using the iterator in a for() or while() loop.  Calling foreach() does rewind the iterator before iteration.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)