Home » PHP » Fungsi getChildren() SimpleXML PHP

Fungsi getChildren() SimpleXML PHP

by Hanifah Nurbaeti
by Hanifah Nurbaeti

Section Artikel

Definisi dan Penggunaan

Fungsi getChildren() dapat digunakan untuk mengembalikan elemen anak dari elemen saat ini.

Syntax

SimpleXMLIterator::getChildren()

Detail Teknis

Return Value:Mengembalikan objek SimpleXMLIterator yang berisi turunan dari elemen saat ini
PHP Version:5.0+

Contoh
Dapatkan child elemen dari elemen saat ini dan keluarkan nama dan data:

<?php
$bookxml = <<<XML
<bookstore>
  <book>
    <title>Everyday Italian</title>
    <author>Giada De Laurentiis</author>
  </book>
  <book>
    <title>Harry Potter</title>
    <author>J K. Rowling</author>
  </book>
  <book>
    <title>Learning XML</title>
    <author>Erik T. Ray</author>
  </book>
</bookstore>
XML;

$xml = new SimpleXMLIterator($bookxml);

for( $xml->rewind(); $xml->valid(); $xml->next() ) {
  foreach($xml->getChildren() as $name => $data) {
    echo "The $name is '$data'";
    echo "<br>";
  }
}
?>
  /* Output : 
The title is 'Everyday Italian'
The author is 'Giada De Laurentiis'
The title is 'Harry Potter'
The author is 'J K. Rowling'
The title is 'Learning XML'
The author is 'Erik T. Ray'

You may also like