本文整理汇总了PHP中XMLReader::xml方法的典型用法代码示例。如果您正苦于以下问题:PHP XMLReader::xml方法的具体用法?PHP XMLReader::xml怎么用?PHP XMLReader::xml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类XMLReader
的用法示例。
在下文中一共展示了XMLReader::xml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getFeed
/**
* Method to load a URI into the feed reader for parsing.
*
* @param string $uri The URI of the feed to load. Idn uris must be passed already converted to punycode.
*
* @return JFeedReader
*
* @since 12.3
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function getFeed($uri)
{
// Create the XMLReader object.
$reader = new XMLReader();
// Open the URI within the stream reader.
if (!@$reader->open($uri, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING)) {
// If allow_url_fopen is enabled
if (ini_get('allow_url_fopen')) {
// This is an error
throw new RuntimeException('Unable to open the feed.');
} else {
// Retry with JHttpFactory that allow using CURL and Sockets as alternative method when available
$connector = JHttpFactory::getHttp();
$feed = $connector->get($uri);
// Set the value to the XMLReader parser
if (!$reader->xml($feed->body, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING)) {
throw new RuntimeException('Unable to parse the feed.');
}
}
}
try {
// Skip ahead to the root node.
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT) {
break;
}
}
} catch (Exception $e) {
throw new RuntimeException('Error reading feed.');
}
// Setup the appopriate feed parser for the feed.
$parser = $this->_fetchFeedParser($reader->name, $reader);
return $parser->parse();
}
示例2: getFeed
/**
* Method to load a URI into the feed reader for parsing.
*
* @param string $uri The URI of the feed to load. Idn uris must be passed already converted to punycode.
*
* @return JFeedReader
*
* @since 12.3
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function getFeed($uri)
{
// Create the XMLReader object.
$reader = new XMLReader();
// Open the URI within the stream reader.
if (!@$reader->open($uri, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING)) {
// Retry with JHttpFactory that allow using CURL and Sockets as alternative method when available
// Adding a valid user agent string, otherwise some feed-servers returning an error
$options = new \joomla\Registry\Registry();
$options->set('userAgent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0');
$connector = JHttpFactory::getHttp($options);
$feed = $connector->get($uri);
if ($feed->code != 200) {
throw new RuntimeException('Unable to open the feed.');
}
// Set the value to the XMLReader parser
if (!$reader->xml($feed->body, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING)) {
throw new RuntimeException('Unable to parse the feed.');
}
}
try {
// Skip ahead to the root node.
while ($reader->read()) {
if ($reader->nodeType == XMLReader::ELEMENT) {
break;
}
}
} catch (Exception $e) {
throw new RuntimeException('Error reading feed.', $e->getCode(), $e);
}
// Setup the appopriate feed parser for the feed.
$parser = $this->_fetchFeedParser($reader->name, $reader);
return $parser->parse();
}
示例3: parseSparqlResults
/**
* Parse the passed in Sparql XML string into a more easily usable format.
*
* @param string $sparql
* A string containing Sparql result XML.
*
* @return array
* Indexed (numerical) array, containing a number of associative arrays,
* with keys being the same as the variable names in the query.
* URIs beginning with 'info:fedora/' will have this beginning stripped
* off, to facilitate their use as PIDs.
*/
public static function parseSparqlResults($sparql)
{
// Load the results into a XMLReader Object.
$xmlReader = new XMLReader();
$xmlReader->xml($sparql);
// Storage.
$results = array();
// Build the results.
while ($xmlReader->read()) {
if ($xmlReader->localName === 'result') {
if ($xmlReader->nodeType == XMLReader::ELEMENT) {
// Initialize a single result.
$r = array();
} elseif ($xmlReader->nodeType == XMLReader::END_ELEMENT) {
// Add result to results
$results[] = $r;
}
} elseif ($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->depth == 3) {
$val = array();
$uri = $xmlReader->getAttribute('uri');
if ($uri !== NULL) {
$val['value'] = self::pidUriToBarePid($uri);
$val['uri'] = (string) $uri;
$val['type'] = 'pid';
} else {
//deal with any other types
$val['type'] = 'literal';
$val['value'] = (string) $xmlReader->readInnerXML();
}
$r[$xmlReader->localName] = $val;
}
}
$xmlReader->close();
return $results;
}
示例4: execute
/**
* Execute parsing
*
* @param string $input
* @return mixed
*/
public function execute($input)
{
$reader = new BasicXmlReader();
$reader->xml($input);
$data = $this->xmlToArray($reader);
return $this->arrayToResource($data);
}
示例5: read
/**
* Convert string with xml data to php array.
*
* @throws Exception
*
* @param string $string
*
* @return array
*/
public function read($string)
{
libxml_use_internal_errors(true);
libxml_disable_entity_loader(true);
$result = simplexml_load_string($string, null, LIBXML_IMPORT_FLAGS);
if (!$result) {
$errors = libxml_get_errors();
libxml_clear_errors();
foreach ($errors as $error) {
$text = '';
switch ($error->level) {
case LIBXML_ERR_WARNING:
$text .= _s('XML file contains warning %1$s:', $error->code);
break;
case LIBXML_ERR_ERROR:
$text .= _s('XML file contains error %1$s:', $error->code);
break;
case LIBXML_ERR_FATAL:
$text .= _s('XML file contains fatal error %1$s:', $error->code);
break;
}
$text .= trim($error->message) . ' [ Line: ' . $error->line . ' | Column: ' . $error->column . ' ]';
throw new Exception($text);
}
}
$xml = new XMLReader();
$xml->xml($string);
$array = $this->xmlToArray($xml);
$xml->close();
return $array;
}
示例6: readString
public function readString($string, $class)
{
$reader = new \XMLReader();
// $reader->setParserProperty(\XMLReader::VALIDATE, true);
$reader->xml($string);
$ret = $this->readXml($reader, $class);
$reader->close();
return $ret;
}
示例7: foo
function foo()
{
for ($i = 0; $i < 100; $i++) {
$reader = new XMLReader();
$reader->xml('<?xml version="1.0" encoding="utf-8"?><id>1234567890</id>');
while ($reader->read()) {
$reader->expand();
}
}
}
示例8: getParsed
/**
* Parses given xml by xmlPathConfig of this class.
*
* @param string $xml The XML as string.
*
* @return array The processed xml as array.
* @throws VersionControl_SVN_Parser_Exception If XML isn't parseable.
*/
public function getParsed($xml)
{
$reader = new XMLReader();
$reader->xml($xml);
if (false === $reader) {
throw new VersionControl_SVN_Parser_Exception('Cannot instantiate XMLReader');
}
$data = self::getParsedBody($reader, $this->xmlPathConfig);
$reader->close();
return $data;
}
示例9: testEncode
/**
* Should convert php arrays to valid xml.
*/
public function testEncode()
{
$ops = new Ops();
$data = array('protocol' => '', 'action' => '', 'object' => '');
$result = $ops->encode($data);
$xml = XMLReader::xml($result);
// The validate parser option must be enabled for
// this method to work properly
$xml->setParserProperty(XMLReader::VALIDATE, true);
// make sure this is xml
$this->assertTrue($xml->isValid());
}
示例10: map
/**
* Return a JS file in the container in links.
*
* @param string $xml
*
* @return string
* @access public
* @author Etienne de Longeaux <etienne.delongeaux@gmail.com>
*/
public function map($xml)
{
$reader = new \XMLReader();
$reading = $reader->xml($xml);
$reader->read();
foreach ($this->mappers as $mapper) {
if ($mapper->supports($reader->name)) {
return $mapper->map($xml, $this);
}
}
throw new \Exception('No registered mapper for ' . $reader->name);
}
示例11: deserialize
/**
* Deserialize the resource from the specified input stream.
*
* @param string $incoming The text to deserialize.
*
* @return mixed the resource.
*/
public function deserialize($incoming)
{
$resources = null;
$reader = new \XMLReader();
$reader->xml($incoming);
$reader->read();
do {
if ($reader->nodeType == \XMLReader::ELEMENT && XmlMapper::isKnownType($reader->name)) {
$class = XmlMapper::getClassName($reader->name);
$resources[] = new $class($reader);
}
} while ($reader->read());
return $resources;
}
示例12: testViewRss
/**
* testViewRss
*
*/
public function testViewRss()
{
// Disable debug mode to avoid DebugKit interruption
$debug = Configure::read('debug');
Configure::write('debug', 0);
$this->testAction('/categories/view/1.rss', array('method' => 'GET', 'return' => 'contents'));
// Restore debug setting
Configure::write('debug', $debug);
$expected_strings = array('Books', 'TestSlide1', 'TestSlide2');
foreach ($expected_strings as $str) {
$this->assertRegExp("/" . preg_quote($str) . "/", $this->contents);
}
$z = new XMLReader();
$z->xml($this->contents, NULL, LIBXML_DTDVALID);
$this->assertTrue($z->isValid());
}
示例13: read
/**
* Read an RSS feed, and return its items.
*
* @param unknown $uri
* @return array
* */
public function read($uri)
{
$xml = new XMLReader();
$xml->xml(file_get_contents($uri));
$items = array();
$cur_item = null;
$in_item = false;
$cur_node = null;
while ($xml->read()) {
if ($xml->nodeType === XMLReader::ELEMENT && $xml->name === "item") {
$cur_item = new stdClass();
$cur_item->headline = null;
$cur_item->body = null;
$cur_item->uri = null;
$cur_item->pub_date = null;
$cur_item->category = null;
$in_item = true;
} elseif ($xml->nodeType === XMLReader::ELEMENT) {
$cur_node = $xml->name;
} elseif (($xml->nodeType === XMLReader::TEXT || $xml->nodeType === XMLReader::CDATA) && $in_item === true) {
switch ($cur_node) {
case "title":
$cur_item->headline = $xml->value;
break;
case "description":
$cur_item->body = $xml->value;
break;
case "link":
$cur_item->uri = $xml->value;
break;
case "pubDate":
$cur_item->pub_date = $xml->value;
break;
case "category":
$cur_item->category = $xml->value;
break;
default:
break;
}
} elseif ($xml->nodeType === XMLReader::END_ELEMENT && $xml->name === "item") {
$items[] = $cur_item;
$in_item = false;
}
}
return $items;
}
示例14: _getReader
protected function _getReader($data, $request)
{
if (!is_bool($request)) {
}
/// @TODO
$this->_currentNode = NULL;
$reader = new XMLReader();
$reader->xml($data, NULL, LIBXML_NONET | LIBXML_NOENT);
if ($this->_validate) {
if ('@data_dir@' != '@' . 'data_dir' . '@') {
$schema = '@data_dir@' . DIRECTORY_SEPARATOR . 'pear.erebot.net' . DIRECTORY_SEPARATOR . 'XRL';
} else {
$schema = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'data';
}
$schema .= DIRECTORY_SEPARATOR;
$schema .= $request ? 'request.rng' : 'response.rng';
$reader->setRelaxNGSchema($schema);
}
return $reader;
}
示例15: parse_GetObs
function parse_GetObs($output)
{
$bookList = array();
$i = 0;
$xmlReader = new XMLReader();
$xmlReader->xml($output);
while ($xmlReader->read()) {
// check to ensure nodeType is an Element not attribute or #Text
if ($xmlReader->nodeType == XMLReader::ELEMENT) {
if ($xmlReader->localName == 'values') {
// move to its textnode / child
$xmlReader->read();
$bookList[$i]['values'] = $xmlReader->value;
$i = $i + 1;
}
}
}
//print_r($bookList);
return $bookList;
}