function parse($path, $data = '') { $this->state = 0; /* reader */ if (!$this->v('reader')) { ARC2::inc('Reader'); $this->reader =& new ARC2_Reader($this->a, $this); } $this->reader->setAcceptHeader('Accept: sparql-results+xml; q=0.9, */*; q=0.1'); $this->reader->activate($path, $data); $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; /* xml parser */ $this->initXMLParser(); /* parse */ $first = true; while ($d = $this->reader->readStream()) { if (!xml_parse($this->xml_parser, $d, false)) { $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); $line = xml_get_current_line_number($this->xml_parser); $this->tmp_error = 'XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')'; return $this->addError($this->tmp_error); } } $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); xml_parser_free($this->xml_parser); $this->reader->closeStream(); return $this->done(); }
function parse($path, $data = '', $iso_fallback = false) { /* reader */ if (!$this->v('reader')) { ARC2::inc('Reader'); $this->reader = new ARC2_Reader($this->a, $this); } $this->reader->setAcceptHeader('Accept: application/rdf+xml; q=0.9, */*; q=0.1'); $this->reader->activate($path, $data); $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; /* xml parser */ $this->initXMLParser(); /* parse */ $first = true; while ($d = $this->reader->readStream()) { if (!$this->keep_time_limit) { @set_time_limit($this->v('time_limit', 60, $this->a)); } if ($iso_fallback && $first) { $d = '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . preg_replace('/^\\<\\?xml [^\\>]+\\?\\>\\s*/s', '', $d); $first = false; } preg_match_all('/<<<(.+?\\s*?)*?>>>/', $d, $res); if (isset($res)) { foreach ($res[0] as $templ) { $key = 'template_' . count($this->templates); $this->templates[$key] = str_replace('<<<', '', str_replace('>>>', '', $templ)); $d = str_replace($templ, $key, $d); } } if (!xml_parse($this->xml_parser, $d, false)) { $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); $line = xml_get_current_line_number($this->xml_parser); $this->tmp_error = 'XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')'; if (!$iso_fallback && preg_match("/Invalid character/i", $error_str)) { xml_parser_free($this->xml_parser); unset($this->xml_parser); $this->reader->closeStream(); $this->__init(); $this->encoding = 'ISO-8859-1'; unset($this->xml_parser); unset($this->reader); return $this->parse($path, $data, true); } else { return $this->addError($this->tmp_error); } } } $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); xml_parser_free($this->xml_parser); $this->reader->closeStream(); unset($this->reader); return $this->done(); }
protected function parseXML($xml) { $xml_parser = xml_parser_create(); // use case-folding so we are sure to find the tag in $map_array xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true); xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, true); $this->setEncoding(xml_parser_get_option($xml_parser, XML_OPTION_TARGET_ENCODING)); xml_set_element_handler($xml_parser, array($this, "startElement"), array($this, "endElement")); xml_set_character_data_handler($xml_parser, array($this, "characterData")); if (!xml_parse($xml_parser, $xml)) { throw new KurogoDataException(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } xml_parser_free($xml_parser); }
function parse($path, $data = '', $iso_fallback = false) { $this->nodes = array(); $this->node_count = 0; $this->level = 0; /* reader */ if (!$this->v('reader')) { ARC2::inc('Reader'); $this->reader = new ARC2_Reader($this->a, $this); } $this->reader->setAcceptHeader('Accept: application/xml; q=0.9, */*; q=0.1'); $this->reader->activate($path, $data); $this->x_base = isset($this->a['base']) && $this->a['base'] ? $this->a['base'] : $this->reader->base; $this->base = $this->x_base; $this->doc_url = $this->reader->base; /* xml parser */ $this->initXMLParser(); /* parse */ $first = true; while ($d = $this->reader->readStream(1)) { if ($iso_fallback && $first) { $d = '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . preg_replace('/^\\<\\?xml [^\\>]+\\?\\>\\s*/s', '', $d); } if (!xml_parse($this->xml_parser, $d, false)) { $error_str = xml_error_string(xml_get_error_code($this->xml_parser)); $line = xml_get_current_line_number($this->xml_parser); if (!$iso_fallback && preg_match("/Invalid character/i", $error_str)) { xml_parser_free($this->xml_parser); unset($this->xml_parser); $this->reader->closeStream(); unset($this->reader); $this->__init(); $this->encoding = 'ISO-8859-1'; $this->initXMLParser(); return $this->parse($path, $data, true); } else { return $this->addError('XML error: "' . $error_str . '" at line ' . $line . ' (parsing as ' . $this->getEncoding() . ')'); } } $first = false; } $this->target_encoding = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); xml_parser_free($this->xml_parser); $this->reader->closeStream(); unset($this->reader); return $this->done(); }
public function parseData($contents) { $this->clearInternalCache(); $xml_parser = xml_parser_create(); // use case-folding so we are sure to find the tag in $map_array xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true); xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, true); $this->setEncoding(xml_parser_get_option($xml_parser, XML_OPTION_TARGET_ENCODING)); xml_set_element_handler($xml_parser, array($this, "startElement"), array($this, "endElement")); xml_set_character_data_handler($xml_parser, array($this, "characterData")); if (!xml_parse($xml_parser, $contents)) { throw new KurogoDataException(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } xml_parser_free($xml_parser); $this->setTotalItems(count($this->items)); return $this->items; }
function xmlParser($file, $max) { $this->max_news = $max; $this->xml_parser = xml_parser_create(); xml_set_object($this->xml_parser, $this); xml_set_element_handler($this->xml_parser, "startElement", "endElement"); xml_set_character_data_handler($this->xml_parser, 'elementContent'); $this->rss_option = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); if (!($data = $this->_get_contents($file))) { $this->content[0]['title'] = "Fatal Error"; $this->content[0]['description'] = "Fatal Error: could not open XML input (" . $file . ")"; $this->content[0]['link'] = "#"; $this->content[0]['date'] = time(); } preg_replace("#encoding=\"(.+?)\"#ie", "\$this->get_charset('\\1')", $data); if (!xml_parse($this->xml_parser, $data)) { $error_code = xml_get_error_code($this->xml_parser); $error_line = xml_get_current_line_number($this->xml_parser); if ($error_code == 4) { $this->content = array(); $this->index = 0; $this->tag_open = false; $this->tagname = ""; $this->xml_parser = xml_parser_create(); xml_set_object($this->xml_parser, $this); xml_set_element_handler($this->xml_parser, "startElement", "endElement"); xml_set_character_data_handler($this->xml_parser, 'elementContent'); $this->rss_option = xml_parser_get_option($this->xml_parser, XML_OPTION_TARGET_ENCODING); $data = iconv($this->rss_charset, "utf-8", $data); if (!xml_parse($this->xml_parser, $data)) { $this->content[0]['title'] = "XML error in File: " . $file; $this->content[0]['description'] = sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->xml_parser)), xml_get_current_line_number($this->xml_parser)); $this->content[0]['link'] = "#"; $this->content[0]['date'] = time(); } } else { $this->content[0]['title'] = "XML error in File: " . $file; $this->content[0]['description'] = sprintf("XML error: %s at line %d", xml_error_string($error_code), $error_line); $this->content[0]['link'] = "#"; $this->content[0]['date'] = time(); } } xml_parser_free($this->xml_parser); }
/** * get option * * @param Charcoal_Integer|integer $option XML_OPTION_CASE_FOLDING or XML_OPTION_TARGET_ENCODING */ public function getRawOption($option) { Charcoal_ParamTrait::validateInteger(1, $option); if (!$this->parser) { _throw(new PhpXmlParserComponentException('parser object is not created')); } return xml_parser_get_option($this->parser, ui($option)); }
public function getOption($option) { return $this->parser ? xml_parser_get_option($this->parser, $option) : false; }
<?php /* Prototype : proto bool xml_set_notation_decl_handler ( resource $parser , callback $handler ) * Description: Sets the notation declaration handler function for the XML parser. * Source code: ext/xml/xml.c * Alias to functions: */ echo "Simple testcase for xml_parser_get_option() function\n"; $parser = xml_parser_create_ns(); var_dump(xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING)); var_dump(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING)); var_dump(xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 1)); var_dump(xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1")); var_dump(xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING)); var_dump(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING)); var_dump(xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0)); var_dump(xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8")); var_dump(xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING)); var_dump(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING)); var_dump(xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "US-ASCII")); var_dump(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING)); xml_parser_free($parser); echo "Done\n";
/** * Parse XML data * * @param var data either a string or an xml.parser.InputSource * @param string source default NULL optional source identifier, will show up in exception * @return bool * @throws xml.XMLFormatException in case the data could not be parsed * @throws lang.NullPointerException in case a parser could not be created */ public function parse($data, $source = null) { if ($parser = xml_parser_create('')) { xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); if (null === $this->encoding) { $this->encoding = xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING); } else { xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $this->encoding); } // Register callback if ($this->callback) { xml_set_object($parser, $this->callback); $this->callback->onBegin($this); xml_set_element_handler($parser, 'onStartElement', 'onEndElement'); xml_set_character_data_handler($parser, 'onCData'); xml_set_default_handler($parser, 'onDefault'); } // Parse streams while reading data if ($data instanceof InputSource) { $stream = $data->getStream(); $source || ($source = $data->getSource()); do { if ($stream->available()) { $r = xml_parse($parser, $stream->read(), false); } else { $r = xml_parse($parser, '', true); break; } } while ($r); } else { $r = xml_parse($parser, $data, true); } // Check for errors if (!$r) { $type = xml_get_error_code($parser); $line = xml_get_current_line_number($parser); $column = xml_get_current_column_number($parser); $message = xml_error_string($type); xml_parser_free($parser); libxml_clear_errors(); $e = new XMLFormatException($message, $type, $source, $line, $column); $this->callback && $this->callback->onError($this, $e); throw $e; } xml_parser_free($parser); $r = true; $this->callback && ($r = $this->callback->onFinish($this)); return $r; } throw new \lang\NullPointerException('Could not create parser'); }
/** * Récupérer toutes les informations dans un fichier DocBook. * @param string $filename nom du fichier à analyser * @return boolean booléen indiquant si le document XML a pu être lu * @throws PEAR_ErrorStack */ function setFromDocbook($filename) { $xmltree = new XML_Tree($filename); $xmltree->setErrorHandling(PEAR_ERROR_CALLBACK, array(&$this, '_XMLTreeErrorHandler')); $article = $xmltree->getTreeFromFile(); if ($this->errors->hasErrors('error')) { return false; } $this->charset = xml_parser_get_option($xmltree->parser, XML_OPTION_TARGET_ENCODING); $this->repertoire = $this->stringValue(@$article->getAttribute("id")); $this->type = $this->stringValue(@$article->getAttribute("role")); $this->lang = $this->stringValue(@$article->getAttribute("lang")); $artinfonode = $this->getElementsByTagName($article, "articleinfo"); if (count($artinfonode) == 0) { $this->errors->push(OW_MISSING_NODE, 'error', array('file' => $filename, 'node' => 'articleinfo')); return false; } /* Traitement particulier pour les balises <author> */ $auteurs = array(); foreach ($this->getElementsByTagName($artinfonode[0], "author") as $author) { $firstnames = $this->getElementsByTagName($author, "firstname"); $surnames = $this->getElementsByTagName($author, "surname"); $nom = $this->stringValue(ucfirst(implode(' ', $firstnames)) . ' ' . ucfirst(implode(' ', $surnames))); if (preg_match('/^\\s*$/', $nom) != 0) { $auteurs[] = $nom; } } $this->auteurs = implode(', ', $auteurs); /* Traitement des autres méta-données */ $this->classement = array(); /** * Tableau des balises que l'on récupère sans traitement particulier */ $autotags = array('title', 'titleabbrev', 'pubdate', 'date', 'abstract'); foreach ($artinfonode[0]->children as $child) { $name = $child->name; if (in_array($name, $autotags)) { $this->{$name} = $this->stringValue($child); } if ($name == 'subjectset') { foreach ($this->getElementsByTagName($child, 'subject') as $subjnode) { $attribute = $this->stringValue($subjnode->getAttribute('role')); if (empty($attribute)) { continue; } $this->classement[$attribute] = array(); foreach ($this->getElementsByTagName($subjnode, 'subjectterm') as $entry) { $this->classement[$attribute][] = $this->stringValue($entry); } } } } return true; }
/** * Fetch a parser options * * @return mixed the option value * @access public */ public function getOption($option) { if ($this->parser === null) { throw new ParserException('Parser not been registered'); } return xml_parser_get_option($this->parser, $option); }
<?php /* Prototype : proto int xml_parser_get_option(resource parser, int option) * Description: Get options from an XML parser * Source code: ext/xml/xml.c * Alias to functions: */ echo "*** Testing xml_parser_get_option() : usage variations ***\n"; error_reporting(E_ALL & ~E_NOTICE); class aClass { function __toString() { return "Some Ascii Data"; } } // Initialise function arguments not being substituted (if any) $option = 10; //get an unset variable $unset_var = 10; unset($unset_var); $fp = fopen(__FILE__, "r"); //array of values to iterate over $values = array(0, 1, 12345, -2345, 10.5, -10.5, 101234567000.0, 1.07654321E-9, 0.5, array(), array(0), array(1), array(1, 2), array('color' => 'red', 'item' => 'pen'), NULL, null, true, false, TRUE, FALSE, "", '', "string", 'string', new aClass(), $fp, $undefined_var, $unset_var); // loop through each element of the array for parser foreach ($values as $value) { echo @"\nArg value {$value} \n"; var_dump(xml_parser_get_option($value, $option)); } fclose($fp); echo "Done";
function parse_data($data){ $this->init(false); $this->encoding=($this->encoding=="auto") ? "UTF-8" : $this->encoding; $this->create_parser(); if($this->save_data){ $this->data=$data; } if(!$success=xml_parse($this->parser, $data, true)){ $error_str = xml_error_string(xml_get_error_code($this->parser)); $line = xml_get_current_line_number($this->parser); xml_parser_free($this->parser); return "XML error: '".$error_str."' at line ".$line."\n"; } $this->target_encoding=xml_parser_get_option($this->parser, XML_OPTION_TARGET_ENCODING); xml_parser_free($this->parser); return $this->done(); }
/** * Récupérer toutes les informations dans un fichier DocBook. * @param string $filename nom du fichier à analyser * @return object DocInfos objet contenant les infos de l'article, ou un tableau de chaînes contenant des messages d'erreurs * @todo Relire le code, il ne vérifie parfois pas assez les données d'entrées/les erreurs qui peuvent survenir pendant le traitement. */ function docbookGetArticleInfoFromFile($filename) { $doc = new DocInfos(); $errors =& PEAR_ErrorStack::singleton('OpenWeb_Backend_DocbookParse'); /* Extrait du XML toutes les informations pour remplir la classe Article */ /* Obtenir un arbre à partir du fichier XML */ $xmltree = new XML_Tree($filename); /* lecture du fichier qui récupère la balise <article> */ $article = $xmltree->getTreeFromFile(); if ($xmltree->isError($article)) { $erreur = $article->getUserInfo(); if ($erreur == '') { $erreur = 'probablement un document mal formé'; } return array('Impossible de parser le fichier XML (' . $erreur . ')'); } $charset = xml_parser_get_option($xmltree->parser, XML_OPTION_TARGET_ENCODING); $doc->repertoire = _toUtf8($charset, trim($article->getAttribute("id"))); $doc->type = _toUtf8($charset, array_key_exists("role", $article->attributes) ? $article->getAttribute("role") : "article"); $doc->lang = _toUtf8($charset, array_key_exists("lang", $article->attributes) ? $article->getAttribute("lang") : "fr"); $artinfonode = getElementsByTagName($article, "articleinfo"); if (count($artinfonode) == 0) { return array('Impossible de continuer à parser le fichier XML : il manque la section articleinfo du docbook'); } /* Traitement particulier pour les balises <author> */ $auteurs = array(); foreach (getElementsByTagName($artinfonode[0], "author") as $author) { $firstnames = getElementsByTagName($author, "firstname"); $surnames = getElementsByTagName($author, "surname"); $auteurs[] = (count($firstnames) ? ucfirst(trim(tree2text($firstnames[0]))) : "") . ' ' . (count($surnames) ? ucfirst(trim(tree2text($surnames[0]))) : ""); } $doc->auteurs = _toUtf8($charset, implode(', ', $auteurs)); $doc->classement = array(); foreach ($artinfonode[0]->children as $child) { /* Récupération des balises <title>, <pubdate> et <date> */ if (!strcmp($child->name, "title")) { $doc->titre = _toUtf8($charset, tree2text($child)); } if (!strcmp($child->name, "titleabbrev")) { $doc->titremini = _toUtf8($charset, tree2text($child)); } if (!strcmp($child->name, "pubdate")) { $doc->pubdate = _toUtf8($charset, tree2text($child)); } if (!strcmp($child->name, "date")) { $doc->update = _toUtf8($charset, tree2text($child)); } if (!strcmp($child->name, "subjectset")) { foreach (getElementsByTagName($child, "subject") as $subjnode) { $attribute = _toUtf8($charset, $subjnode->getAttribute("role")); $doc->classement[$attribute] = array(); foreach (getElementsByTagName($subjnode, "subjectterm") as $entry) { $doc->classement[$attribute][] = _toUtf8($charset, tree2text($entry)); } } } if (!strcmp($child->name, "abstract")) { $doc->accroche = _toUtf8($charset, tree2text($child)); } } return $doc; }
/* Prototype : proto int xml_parser_get_option(resource parser, int option) * Description: Get options from an XML parser * Source code: ext/xml/xml.c * Alias to functions: */ echo "*** Testing xml_parser_get_option() : usage variations ***\n"; error_reporting(E_ALL & ~E_NOTICE); class aClass { function __toString() { return "Some Ascii Data"; } } // Initialise function arguments not being substituted (if any) $parser = xml_parser_create(); //get an unset variable $unset_var = 10; unset($unset_var); $fp = fopen(__FILE__, "r"); //array of values to iterate over $values = array(12345, -2345, 10.5, -10.5, 101234567000.0, 1.07654321E-9, 0.5, array(), array(0), array(1), array(1, 2), array('color' => 'red', 'item' => 'pen'), NULL, null, true, false, TRUE, FALSE, "", '', "string", 'string', new aClass(), $fp, $undefined_var, $unset_var); // loop through each element of the array for option foreach ($values as $value) { echo @"\nArg value {$value} \n"; var_dump(xml_parser_get_option($parser, $value)); } fclose($fp); xml_parser_free($parser); echo "Done";
<?php /* Prototype : proto int xml_parser_get_option(resource parser, int option) * Description: Get options from an XML parser * Source code: ext/xml/xml.c * Alias to functions: */ echo "*** Testing xml_parser_get_option() : error conditions ***\n"; //Test xml_parser_get_option with one more than the expected number of arguments echo "\n-- Testing xml_parser_get_option() function with more than expected no. of arguments --\n"; $option = 10; $extra_arg = 10; var_dump(xml_parser_get_option(null, $option, $extra_arg)); // Testing xml_parser_get_option with one less than the expected number of arguments echo "\n-- Testing xml_parser_get_option() function with less than expected no. of arguments --\n"; var_dump(xml_parser_get_option(null)); echo "Done";
/** * get xml parser option * @final * @param $option */ function getOption($option) { return xml_parser_get_option($this->_p, $option); }
<?php require_once '../../includes/header.php'; ?> <pre> <?php //We firstly need to create the parser $parser = xml_parser_create(); $perserOption = xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); var_dump($perserOption); ////bool(true) $perserGetOption = xml_parser_get_option($parser, XML_OPTION_CASE_FOLDING); var_dump($perserGetOption); //int(0) //Warning: xml_parser_get_option(): Unknown option $perserGetOption = xml_parser_get_option($parser, XML_OPTION_SKIP_TAGSTART); var_dump($perserGetOption); //bool(false) $xmlString = <<<XML <?xml version='1.0' standalone='yes'?> <movies> <movie> <title>PHP: Behind the Parser</title> <characters> <character> <name>Ms. Coder</name> <actor>Onlivia Actora</actor> </character> <character> <name>Mr. Coder</name> <actor>El ActÓr</actor>