PHP simplexml_load_file () function
PHP SimpleXML Reference Manual
Examples
Convert XML file SimpleXMLElement object, and then output the object and key elements:
<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
Running instance »
Definition and Usage
simplexml_load_file () function converts the specified XML file as SimpleXMLElement object.
grammar
simplexml_load_file( file,classname,options,ns,is_prefix );
参数 | 描述 |
---|---|
file | 必需。规定 XML 文件路径。 |
classname | 可选。规定新对象的 class。 |
options | 可选。规定附加的 Libxml 参数。通过指定选项为 1 或 0(TRUE 或 FALSE,例如 LIBXML_NOBLANKS(1))进行设置。 可能的值:
|
ns | 可选。规定命名空间前缀或 URI。 |
is_prefix | 可选。规定一个布尔值。如果 ns 是前缀则为 TRUE,如果 ns 是 URI 则为 FALSE。默认是 FALSE。 |
technical details
return value: | If successful SimpleXMLElement object, if fails returns FALSE. |
---|---|
PHP version: | 5+ |
More examples
Suppose we have the following XML document, " note.xml ":
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Example 1
Each element in the output XML file data:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
Running instance »
Example 2
XML output file for each child node of the element names and data:
<?php
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
Running instance »
PHP SimpleXML Reference Manual