Loading XML from files and strings using SimpleXML
True to its name, SimpleXML functions provide an easy way to access data from XML documents. XML files or strings can be converted into objects, and data can be read from them.
We will see how to load an XML from a file or string using SimpleXML functions. You will also learn how to handle errors in XML documents.
Getting ready
Create a new directory named Chapter3
. This chapter will contain sub-folders for each recipe. So, create a folder named Recipe1
inside it.
How to do it...
Create a file named
index.php
inRecipe1
folder. In this file, write the PHP code that will try to load thecommon.xml
file. On loading it successfully, it will display a list of book names. We have also used the libxml functions that will detect any error and will show its detailed description on the screen.<?php libxml_use_internal_errors(true); $objXML = simplexml_load_file('../common.xml'); if (!$objXML) { $errors = libxml_get_errors(); foreach($errors...