本文整理汇总了PHP中XMLReader类的典型用法代码示例。如果您正苦于以下问题:PHP XMLReader类的具体用法?PHP XMLReader怎么用?PHP XMLReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了XMLReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: start_process
public function start_process()
{
if (!$this->path_to_xml_file) {
return false;
}
if (!$this->valid_xml) {
return false;
}
$this->archive_builder = new \eol_schema\ContentArchiveBuilder(array('directory_path' => DOC_ROOT . 'temp/xml_to_archive/'));
$this->taxon_ids = array();
$this->media_ids = array();
$this->vernacular_name_ids = array();
$this->reference_ids = array();
$this->agent_ids = array();
$reader = new \XMLReader();
$file = file_get_contents($this->path_to_xml_file);
$file = iconv("UTF-8", "UTF-8//IGNORE", $file);
$reader->XML($file);
$i = 0;
while (@$reader->read()) {
if ($reader->nodeType == \XMLReader::ELEMENT && $reader->name == "taxon") {
$taxon_xml = $reader->readOuterXML();
$t = simplexml_load_string($taxon_xml, null, LIBXML_NOCDATA);
if ($t) {
$this->add_taxon_to_archive($t);
}
$i++;
if ($i % 100 == 0) {
echo "Parsed taxon {$i} : " . time_elapsed() . "\n";
}
// if($i >= 5000) break;
}
}
$this->archive_builder->finalize();
}
示例2: validate
/**
* @param FeedTypeInterface $type
* @param OutputInterface $output
*
* @return int
*/
protected function validate(FeedTypeInterface $type, OutputInterface $output)
{
$file = $this->exporter->getFeedFilename($type);
if (!file_exists($file)) {
throw new FileNotFoundException(sprintf('<error>Feed "%s" has not yet been exported</error>', $type->getName()));
}
$options = LIBXML_NOENT | LIBXML_COMPACT | LIBXML_PARSEHUGE | LIBXML_NOERROR | LIBXML_NOWARNING;
$this->reader = new \XMLReader($options);
$this->reader->open($file);
$this->reader->setParserProperty(\XMLReader::SUBST_ENTITIES, true);
// foreach ($type->getNamespaces() as $name => $location) {
// $this->reader->setSchema($location);
// }
libxml_clear_errors();
libxml_use_internal_errors(true);
libxml_disable_entity_loader(true);
$progress = new ProgressBar($output);
$progress->start();
// go through the whole thing
while ($this->reader->read()) {
if ($this->reader->nodeType === \XMLReader::ELEMENT && $this->reader->name === $type->getItemNode()) {
$progress->advance();
$this->currentItem = $this->reader->readOuterXml();
}
if ($error = libxml_get_last_error()) {
throw new \RuntimeException(sprintf('[%s %s] %s (in %s - line %d, column %d)', LIBXML_ERR_WARNING === $error->level ? 'WARNING' : 'ERROR', $error->code, trim($error->message), $error->file ? $error->file : 'n/a', $error->line, $error->column));
}
}
$progress->finish();
}
示例3: getClioNum
function getClioNum($appurl, $link)
{
$clio = $current = $currentA = "";
$reader = new XMLReader();
$clioQ = $appurl . '/getSingleStuff.xq?doc=' . $link . '_ead.xml§ion=summary';
$reader->open($clioQ);
while ($reader->read()) {
if ($reader->name == "unitid" && $reader->getAttribute("type") == "clio") {
if ($reader->nodeType == XMLReader::ELEMENT) {
$currentA = "clio";
} else {
if ($reader->nodeType == XMLReader::END_ELEMENT) {
$currentA = "";
}
}
}
//echo "{$reader->name} and $currentA<br />";
if ($reader->name == "#text" && $currentA == "clio") {
$clio = $reader->value;
// there can be only one
}
}
// end WHILE
$reader->close();
return $clio;
}
示例4: generateAcronymInfo
public static function generateAcronymInfo($filename)
{
static $info;
if ($info) {
return $info;
}
$r = new \XMLReader();
if (!$r->open($filename)) {
throw new \Exception("Could not open file for accessing acronym information: {$filename}");
}
$acronyms = array();
while ($r->read()) {
if ($r->nodeType != \XMLReader::ELEMENT) {
continue;
}
if ($r->name == "term") {
$r->read();
$k = $r->value;
$acronyms[$k] = "";
} else {
if ($r->name == "simpara") {
$r->read();
$acronyms[$k] = $r->value;
}
}
}
$info = $acronyms;
return $acronyms;
}
示例5: 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();
}
示例6: writeReaderImpl
private function writeReaderImpl(XMLWriter $writer, XMLReader $reader)
{
switch ($reader->nodeType) {
case XMLReader::ELEMENT:
$writer->startElement($reader->name);
if ($reader->moveToFirstAttribute()) {
do {
$writer->writeAttribute($reader->name, $reader->value);
} while ($reader->moveToNextAttribute());
$reader->moveToElement();
}
if ($reader->isEmptyElement) {
$writer->endElement();
}
break;
case XMLReader::END_ELEMENT:
$writer->endElement();
break;
case XMLReader::COMMENT:
$writer->writeComment($reader->value);
break;
case XMLReader::SIGNIFICANT_WHITESPACE:
case XMLReader::TEXT:
$writer->text($reader->value);
break;
case XMLReader::PI:
$writer->writePi($reader->name, $reader->value);
break;
default:
XMLReaderNode::dump($reader);
}
}
示例7: parseErrorResponse
public function parseErrorResponse($statusCode, $content, MnsException $exception = NULL)
{
$this->succeed = FALSE;
$xmlReader = new \XMLReader();
try {
$xmlReader->XML($content);
$result = XMLParser::parseNormalError($xmlReader);
if ($result['Code'] == Constants::TOPIC_NOT_EXIST) {
throw new TopicNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
}
if ($result['Code'] == Constants::INVALID_ARGUMENT) {
throw new InvalidArgumentException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
}
if ($result['Code'] == Constants::MALFORMED_XML) {
throw new MalformedXMLException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
}
throw new MnsException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
} catch (\Exception $e) {
if ($exception != NULL) {
throw $exception;
} elseif ($e instanceof MnsException) {
throw $e;
} else {
throw new MnsException($statusCode, $e->getMessage());
}
} catch (\Throwable $t) {
throw new MnsException($statusCode, $t->getMessage());
}
}
示例8: readNext
protected function readNext(XMLReader $reader)
{
do {
if ($this->isStartElement('ancode')) {
$pos_id = (int) $reader->getAttribute('pos_id');
if (!isset($this->poses[$pos_id])) {
throw new Exception("Invalid pos id '{$pos_id}' found in ancode '" . $reader->getAttribute('id') . "'");
}
$pos = $this->poses[$pos_id];
$ancode = new phpMorphy_Dict_Ancode($reader->getAttribute('id'), $pos['name'], $pos['is_predict']);
while ($this->read()) {
if ($this->isStartElement('grammem')) {
$grammem_id = (int) $reader->getAttribute('id');
if (!isset($this->grammems[$grammem_id])) {
throw new Exception("Invalid grammem id '{$grammem_id}' found in ancode '" . $ancode->getId() . "'");
}
$ancode->addGrammem($this->grammems[$grammem_id]['name']);
} elseif ($this->isEndElement('ancode')) {
break;
}
}
unset($this->current);
$this->current = $ancode;
break;
}
} while ($this->read());
}
示例9: parseErrorResponse
public function parseErrorResponse($statusCode, $content, MnsException $exception = NULL)
{
$this->succeed = FALSE;
$xmlReader = new \XMLReader();
try {
$xmlReader->XML($content);
$result = XMLParser::parseNormalError($xmlReader);
if ($result['Code'] == Constants::QUEUE_NOT_EXIST) {
throw new QueueNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
}
if ($result['Code'] == Constants::MESSAGE_NOT_EXIST) {
throw new MessageNotExistException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
}
throw new MnsException($statusCode, $result['Message'], $exception, $result['Code'], $result['RequestId'], $result['HostId']);
} catch (\Exception $e) {
if ($exception != NULL) {
throw $exception;
} elseif ($e instanceof MnsException) {
throw $e;
} else {
throw new MnsException($statusCode, $e->getMessage());
}
} catch (\Throwable $t) {
throw new MnsException($statusCode, $t->getMessage());
}
}
示例10: getFeed
/**
* Method to load a URI into the feed reader for parsing.
*
* @param string $uri The URI of the feed to load.
*
* @return JFeedReader
*
* @since 3.0
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function getFeed($uri)
{
// Make sure the file exists.
try {
$this->http->get($uri);
} catch (RunTimeException $e) {
throw new InvalidArgumentException('The file ' . $uri . ' does not exist.');
}
// 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)) {
throw new RuntimeException('Unable to open the feed.');
}
try {
// Skip ahead to the root node.
while ($reader->read() && $reader->nodeType !== XMLReader::ELEMENT) {
}
} 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();
}
示例11: xmlTree
public static function &buildTree($source, $mode = XMLDocument::BUILD_MODE_FROM_FILE, $ns = NULL)
{
if ($mode == XMLDocument::BUILD_MODE_FROM_FILE) {
if (($content = Filesystem::getFileContent($source)) === NULL) {
return NULL;
}
} else {
if ($source == "" || $source == NULL) {
return new xmlTree();
}
$content = $ns == NULL ? "<ROOT>" . $source . "</ROOT>" : "<ROOT xmlns:{$ns}=\".\">" . $source . "</ROOT>";
}
/*$content = ereg_replace( "&", "&", $content );
$content = ereg_replace( "&", "&", $content );*/
//echo $content;
$content = preg_replace("/&/", "&", $content);
$content = preg_replace("/&/", "&", $content);
$content = iconv("windows-1251", "UTF-8", $content);
$reader = new XMLReader();
if ($reader->XML($content)) {
$_docTree = NULL;
$_docTree = new xmlTree();
self::_appendChilds($_docTree->getCurrent(), $reader, 0);
$reader = NULL;
return $_docTree;
} else {
self::raiseException(ERR_XML_INVALID_XML, $content);
return NULL;
}
}
示例12: testXml
private static function testXml()
{
$xml = new \XMLReader();
$xml->XML(self::$TestXml);
$xmlarray = xmlWithArray::xmlToarray($xml);
return $xmlarray;
}
示例13: parseEntrypoints
protected function parseEntrypoints(\XMLReader $xml, InfosAbstract $object)
{
$property = $xml->name;
while ($xml->read()) {
if ($xml->nodeType == \XMLReader::END_ELEMENT && 'entrypoints' == $xml->name) {
break;
}
if ($xml->nodeType == \XMLReader::ELEMENT) {
$id = $config = '';
$type = 'classic';
while ($xml->moveToNextAttribute()) {
if ($xml->name == 'file') {
$id = $xml->value;
} else {
if ($xml->name == 'config') {
$config = $xml->value;
} else {
if ($xml->name == 'type') {
$type = $xml->value;
}
}
}
}
if ($id) {
if (strpos($id, '.php') === false) {
$id .= '.php';
}
$object->entrypoints[$id] = array('config' => $config, 'file' => $id, 'type' => $type);
}
}
}
}
示例14: 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();
}
示例15: actionFias
public function actionFias()
{
$file = 'AS_ADDROBJ_20160609_c5080ba4-9f46-4b6e-aecc-72a630730b3a.XML';
$interestingNodes = array('AOGUID');
$xmlObject = new \XMLReader();
$xmlObject->open($file);
header('Content-Type: text/html; charset=utf-8');
$i = 0;
while ($xmlObject->read()) {
if ($xmlObject->name == 'Object') {
if ($xmlObject->getAttribute('IFNSFL') == '8603') {
// if (($xmlObject->getAttribute('PARENTGUID') == '0bf0f4ed-13f8-446e-82f6-325498808076' && $xmlObject->getAttribute('AOLEVEL') == '7') || $xmlObject->getAttribute('AOGUID') == '0bf0f4ed-13f8-446e-82f6-325498808076') {
$fias = new Fias();
$fias->AOGUID = $xmlObject->getAttribute('AOGUID');
$fias->OFFNAME = $xmlObject->getAttribute('OFFNAME');
$fias->SHORTNAME = $xmlObject->getAttribute('SHORTNAME');
$fias->IFNSFL = $xmlObject->getAttribute('IFNSFL');
$fias->AOLEVEL = $xmlObject->getAttribute('AOLEVEL');
$fias->PARENTGUID = $xmlObject->getAttribute('PARENTGUID');
if ($fias->validate()) {
$fias->save();
} else {
var_dump($fias->attributes);
var_dump($fias->getErrors());
}
// $i++;
}
}
}
echo 'ok';
$xmlObject->close();
}