$code = '
<pre class="prettyprint"><?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> SimpleXML Example</title>
</head>
<body>
<h1>
Please go <a href="http://www.ucertify.com">http://www.ucertify.com</a>
<br/>
</h1>
</body>
</html> </pre>
Which of the following statements will display the HREF attribute on the anchor tag if the SimpleXML object is $sxml? | In the script given in the question, HREF is the attribute of the anchor (<a>) tag. Besides this, anchor tag is inside the <h1> tag and <body> tag. Hence, you will access the HREF tag in the following way:
<pre class="prettyprint">$sxml->body->h1->a[\'href\']</pre>
Fact What is SimpleXML?
SimpleXML is a PHP extension that allows users to easily manipulate/use XML data. Its functions let a user convert XML to an object, which can be processed with property selectors and array iterators. Consider the following XML file, which describes a small collection of books in XML format. It has a root node of store having a direct child, i.e., category, which classifies the books as fiction; category has two children() labelled book; "twilight" by Stephinie Mayer and "The Bikers" by Alex R. Stuart.
<pre class="prettyprint"><?xml version="1.0"?>
<store>
<category id="fiction">
<book>
<title>twilight</title>
<author>Stephinie Mayer</author>
</book>
<book>
<title>The Bikers</title>
<author>Alex R. Stuart</author>
</book>
</category>
</store>
</pre>
To extract the book title and author, a user will run the following PHP script:
<pre class="prettyprint"><?php
$store = simplexml_load_file(\'store.xml\');
foreach ($store->category as $category) {
printf("Category %s\n", $category[\'id\']);
foreach ($category->book as $book) {
printf("<br>Title: %s\n", $book->title);
printf("<br>Author: %s\n", $book->author);
}
}
?></pre>
';
$matches = null;
$content = preg_match('#<pre class="prettyprint">(.*?)</pre>#i', $code, $matches);
$matches = $matches[1];
var_dump(preg_replace("#$code#", htmlentities($content), $code));