We will explore how to use the JSimpleXML
parser because it is the most commonly used XML parser in Joomla!.
The first thing we need to do is obtain an instance of the parser. We do this using the
JFactory method getXMLParser(). When we use this method we must tell it which
XML parser we want to use:
$parser =& JFactory::getXMLParser('Simple');
The next step is to load and parse some XML. There are two ways in which we
can do this; we can either load XML from a file or from a pre-existing string. This
example demonstrates how we load XML from a file:
$parser->loadFile($pathToXML_File);
Loading XML from a string is a very similar process, as this example demonstrates:
$xml = '
Moving Pictures
Rush
1981
??? ??? ??? ??? ???
Chapter 10
[ 279 ]
';
$parser->loadString($xml);
That is all we have to do in order to parse XML using the JSimpleXML parser!
We can only use a JSimpleXML parser once; if we attempt to use the
load methods more than once, we will encounter errors.
Pages:
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381