PHP simplexml_load_file() Fungsi
Contoh
Ubah file XML menjadi objek, lalu keluarkan kunci dan elemen objek:
<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
Definisi dan Penggunaan
Fungsi simplexml_load_file() mengonversi dokumen XML menjadi objek.
Sintaksis
simplexml_load_file(file, class, options, ns, is_prefix)
Nilai Parameter
Parameter | Description |
---|---|
file | Required. Specifies the path to the XML file |
class | Optional. Specifies the class of the new object |
options | Optional. Specifies additional Libxml parameters. Is set by specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1)) Possible values:
|
ns | Optional. Specifies a namespace prefix or URI |
is_prefix | Optional. Specifies a Boolean value. TRUE if ns is a prefix. FALSE if ns is a URI. Default is FALSE |
Detail Teknis
Nilai Kembali: | Objek SimpleXMLElement pada kesuksesan. SALAH pada kegagalan |
---|---|
Versi PHP: | 5+ |
Lebih Banyak Contoh
Asumsikan kita memiliki file XML berikut, " note.xml ":
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Contoh
Keluarkan data dari setiap elemen dalam file XML:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
Contoh
Keluarkan nama elemen dan data untuk setiap simpul anak dalam file XML:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
Referensi PHP SimpleXML