PHP simplexml_load_file() Functie
Voorbeeld
Converteer een XML-bestand naar een object en voer vervolgens sleutels en elementen van het object uit:
<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
Definitie en gebruik
De functie simplexml_load_file() converteert een XML-document naar een object.
Syntaxis
simplexml_load_file(file, class, options, ns, is_prefix)
Parameterwaarden
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 |
Technische details
Winstwaarde: | Een SimpleXMLElement-object op succes. ONWAAR bij fout |
---|---|
PHP-versie: | 5+ |
Meer voorbeelden
Stel dat we het volgende XML-bestand hebben, " 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>
Voorbeeld
Voer de gegevens uit van elk element in het XML-bestand:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
Voorbeeld
Voer de naam en gegevens van het element uit voor elk kindknooppunt in het XML-bestand:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
❮ PHP SimpleXML-referentie