Loading a Document

The focal class of the XMLReader extension is aptly named XMLReader. It doesn’t declare a constructor, but rather offers two methods for introducing XML data into it.

<?php
// Loads a document contained within a string
$doc = XMLReader::xml($xmlString);

// Loads a document from an external file
$doc = XMLReader::open($filePath);
?>

Both of these methods have two additional parameters.

The second parameter is a string specifying the encoding scheme for the input document. It is optional and defaults to 'UTF-8’ if unspecified or specified as null. Valid values for this parameter aren’t included in the PHP manual, but can be found in the reference for the underlying libxml2 library at http://www.xmlsoft.org/encoding.html#Default.

The third parameter is an integer value that can be set in bitmask fashion using constants from the libxml extension. This is the preferred method to configure the parser over using the deprecated setParserProperty() method. The specific constants that can be used to form the bitmask (using the bitwise OR operator |) are listed below. Descriptions for them can be found at http://php.net/manual/en/libxml.constants.php.

  • LIBXML_COMPACT
  • LIBXML_DTDATTR
  • LIBXML_DTDLOAD
  • LIBXML_DTDVALID
  • LIBXML_NOBLANKS
  • LIBXML_NOCDATA
  • LIBXML_NOENT
  • LIBXML_NOERROR
  • LIBXML_NONET
  • LIBXML_NOWARNING
  • LIBXML_NSCLEAN
  • LIBXML_XINCLUDE

As an example, a call that configured the parser to suppress errors and warnings might look like this.

<?php
$doc = XMLReader::xml(
$xmlString,
null,
LIBXML_NOERROR | LIBXML_NOWARNING
);
?>

 


© XMLReader Extension — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (03.09.2014)
Views: 353 | Rating: 0.0/0
Total comments: 0
avatar