Which function is used to load XML data from a file in SimpleXML?
A
simplexml_load_string() B
simplexml_get_file() C
simplexml_load_file() D
xml_get_contents()
Analysis & Theory
`simplexml_load_file()` loads XML data from a file into a SimpleXML object.
What does this code output?
```
$xml = simplexml_load_string('<note><to>John</to></note>');
echo $xml->to;
```
A
John B
note C
to D
Error
Analysis & Theory
It outputs `John` by accessing the `<to>` element.
Which method is used to access child elements in SimpleXML?
A
dot notation (xml.to) B
arrow notation (xml->to) C
square brackets (xml['to']) D
get_child('to')
Analysis & Theory
SimpleXML uses arrow notation, like `$xml->to`, to access child elements.
How do you access an attribute in SimpleXML?
A
$xml->attribute['name'] B
$xml->getAttribute('name') C
$xml['name'] D
$xml->name
Analysis & Theory
Attributes are accessed using array syntax, like `$xml['name']`.
What does this code do?
```
$xml = simplexml_load_string('<user id="101"><name>Mike</name></user>');
echo $xml['id'];
```
A
Prints 'Mike' B
Prints 'id' C
Prints '101' D
Error
Analysis & Theory
It prints the attribute `id` value, which is '101'.
Which loop can be used to iterate over multiple child elements in SimpleXML?
A
for loop B
while loop C
foreach loop D
do-while loop
Analysis & Theory
SimpleXML supports the `foreach` loop to iterate over child elements easily.
What is the output of this code?
```
$xml = simplexml_load_string('<fruits><fruit>Apple</fruit><fruit>Banana</fruit></fruits>');
foreach($xml->fruit as $f) {
echo $f . ' ';
}
```
A
Apple Banana B
Apple C
Banana D
Error
Analysis & Theory
It loops through both `<fruit>` elements and prints 'Apple Banana'.
How can you check if a node exists in SimpleXML?
A
isset($xml->node) B
exists($xml->node) C
check_node($xml, 'node') D
node_exists($xml->node)
Analysis & Theory
You can use `isset($xml->node)` to check if an XML node exists.
How do you convert a SimpleXML object back to an XML string?
A
xml_convert() B
toXML() C
asXML() D
getXML()
Analysis & Theory
`asXML()` converts a SimpleXML object into an XML string.
What does this code do?
```
$xml = simplexml_load_file('data.xml');
echo $xml->book[0]->title;
```
A
Prints the first book's title from the XML file B
Prints all book titles C
Deletes the XML file D
Saves data to the XML file
Analysis & Theory
It accesses the first `<book>` node and prints its `<title>`.