/** * Read an XML snippet from an element * * @param string $metafield Field that we will fill with the result * @throws MWException */ private function readXml($metafield = null) { $this->debug("Read top level metadata"); if (!$metafield || $this->reader->nodeType != XMLReader::ELEMENT) { return; } // @todo Find and store type of xml snippet. metadata['metadataType'] = "rdf" if (method_exists($this->reader, 'readInnerXML')) { $this->metadata[$metafield] = trim($this->reader->readInnerXml()); } else { throw new MWException("The PHP XMLReader extension does not come " . "with readInnerXML() method. Your libxml is probably out of " . "date (need 2.6.20 or later)."); } $this->reader->next(); }
private function getCurrencyExchangeRates(\XMLReader $xml) { switch ($xml->localName) { case 'Cube': $this->currencyDetails['CXD'] = strtotime($xml->getAttribute('date')); break; case 'Rate': $multiplier = 1; if (!is_null($xml->getAttribute('multiplier'))) { $multiplier = $xml->getAttribute('multiplier'); } $this->currencyDetails['CXV'][$xml->getAttribute('currency')] = $xml->readInnerXml() / $multiplier; break; } }
private function getWeatherData($unit) { if ($this->city != "") { $additionalParameter = ""; if ($unit == "c") { $additionalParameter .= "&units=metric"; } $url = $this->basicUrl . $this->city . $this->fixUrlParameter . $additionalParameter; //OCP\Util::writeLog('ocDashboard',"openweather xml url: ".$url, \OCP\Util::DEBUG); $reader = new XMLReader(); $reader->open($url); $data = array(); while ($reader->read()) { if ($reader->nodeType == XMLReader::ELEMENT) { if (isset($this->xmlNodeAttributes[$reader->name])) { $n = 0; while (isset($data[$n][$reader->name])) { $n++; } foreach ($this->xmlNodeAttributes[$reader->name] as $key) { $data[$n][$reader->name][$key] = $reader->getAttribute($key); } if (in_array($reader->name, $this->xmlAddUnit)) { $data[$n][$reader->name]['unit'] = $this->getUnit($reader->name, $unit); } } else { if (isset($this->xmlNodeValueKeys[$reader->name])) { $data[$reader->name] = $reader->readInnerXml(); } } } } $reader->close(); if (count($data) > 0) { $this->weatherData = $data; } else { OCP\Util::writeLog('ocDashboard', "openweather - could not fetch data for " . $this->city, \OCP\Util::ERROR); $this->errorMsg = $this->l->t("Could not fetch data for \"%s\".<br>Please try another value.<br><a href='%s'>» settings</a>", array($this->city, \OCP\Util::linkToRoute('settings_personal'))); } } }
} if ($x->localName === 'lastUpdated' && $x->nodeType == XMLREADER::ELEMENT) { $event[eventModified] = $x->readString(); } if ($x->localName === 'jobType' && $x->nodeType == XMLREADER::ELEMENT) { $event[eventType] = $x->readString(); } if ($x->localName === 'significance' && $x->nodeType == XMLREADER::ELEMENT) { $event[description] .= ". " . $x->readString(); } if ($x->localName === 'timeOfDay' && $x->nodeType == XMLREADER::ELEMENT) { $event[description] .= ". Time(s) of day affected: " . $x->readString(); } if ($x->localName === 'trafficImpacts' && $x->nodeType == XMLREADER::ELEMENT) { $event[trafficImpactsXML] = "<root>"; $event[trafficImpactsXML] .= $x->readInnerXml(); $event[trafficImpactsXML] .= "</root>"; } if ($x->localName === 'locations' && $x->nodeType == XMLREADER::ELEMENT) { $event[geoXML] = "<root>"; $event[geoXML] .= $x->readInnerXML(); $event[geoXML] .= "</root>"; $events[] = $event; } } $x->close(); ######## CREATE GEOJSON ######## CREATE GEOJSON ######## CREATE GEOJSON ###### $eventsCount = sizeof($events); $x2GeoStruct = array(); $x2ImpactStruct = array(); $toBeEncoded1 = array("type" => "FeatureCollection", "features" => []);
<?php /* $Id$ */ $xmlstring = '<?xml version="1.0" encoding="UTF-8"?> <books><book>test</book></books>'; $reader = new XMLReader(); $reader->XML($xmlstring); $reader->read(); echo $reader->readInnerXml(); echo "\n"; $reader->close(); $reader = new XMLReader(); $reader->XML($xmlstring); $reader->read(); echo $reader->readOuterXml(); echo "\n"; $reader->close(); ?> ===DONE===
/** * @param \XMLReader $xml * @param string $type * @param bool $root * @throws \Exception * @return mixed */ protected function _readElementAsType($xml, $type, $root = false) { if (isset(self::$_knownSimpleTypes[$type])) { $ret = $this->_decodeSimpleValue($xml->readInnerXml(), $type); if (!$xml->isEmptyElement) { $open = 1; while ($open > 0) { if (!$xml->read()) { throw new \Exception("XML Read failure parsing simple value"); } if ($xml->nodeType == \XMLReader::ELEMENT && !$xml->isEmptyElement) { $open++; } elseif ($xml->nodeType == \XMLReader::END_ELEMENT) { $open--; } } } return $ret; } // Assume type strings are well formed: look for the last [ to see if it's an array or map. // Note that this might be an array of arrays, and we're after the outermost type, so we're after the last [! $pos = strrpos($type, '['); if ($pos === false) { // If there wasn't a [ then this must be an object. return $this->_readObject($xml, $type, $root); } // Extract the base type, and whatever's between the [...] as the index type. // Potentially the type string is actually badly formed: // e.g. this code will accept string[int! as being an array of string with index int. // Bah. I'll ignore that case for now. This bit of code gets called a lot, I'd rather not add another substr. $elementType = substr($type, 0, $pos); $indexType = substr($type, $pos + 1, -1); // We return an array of the element(s) we've read. // This will be merged if necessary by whoever we return it to. if ($indexType === "") { // It's an array element, not a map. return array($this->_readElementAsType($xml, $elementType)); } else { return $this->_readMap($xml, $indexType, $elementType); } }
public static function Decode($SOAPResponse, &$isSOAPFault) { $responseXML = ""; try { if (empty($SOAPResponse)) { throw new Exception("Given Response is not a valid SOAP response."); } $xmlDoc = new XMLReader(); $res = $xmlDoc->XML($SOAPResponse); if ($res) { while (trim(strtoupper($xmlDoc->localName)) != self::$SOAPBody) { $isNotEnd = $xmlDoc->read(); if (!$isNotEnd) { break; } } if (!$isNotEnd) { $isSOAPFault = true; $soapFault = new FaultMessage(); $errorData = new ErrorData(); $errorData->errorId = 'Given Response is not a valid SOAP response.'; $errorData->message = 'Given Response is not a valid SOAP response.'; $soapFault->error = $errorData; return $soapFault; } $responseXML = $xmlDoc->readInnerXml(); $xmlDOM = new DOMDocument(); $xmlDOM->loadXML($responseXML); $count = 0; $xmlDoc->read(); $isSOAPFault = trim(strtoupper($xmlDoc->localName)) == self::$SOAPFault; if ($isSOAPFault) { while (trim(strtoupper($xmlDoc->localName)) != self::$SOAPFaultMessage) { $isNotEnd = $xmlDoc->read(); if (!$isNotEnd) { break; } } $xmlDOM->loadXML($xmlDoc->readOuterXml()); } switch ($xmlDoc->nodeType) { case XMLReader::ELEMENT: $nodeName = $xmlDoc->localName; $prefix = $xmlDoc->prefix; if (class_exists($nodeName)) { $xmlNodes = $xmlDOM->getElementsByTagName($nodeName); foreach ($xmlNodes as $xmlNode) { //$xmlNode->prefix = ""; $xmlNode->setAttribute("_class", $nodeName); $xmlNode->setAttribute("_type", "object"); } } break; } $responseXML = $xmlDOM->saveXML(); $unserializer = new XML_Unserializer(); $unserializer->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object'); $res = $unserializer->unserialize($responseXML, false); if ($res) { $responseXML = $unserializer->getUnserializedData(); } $xmlDoc->close(); } else { throw new Exception("Given Response is not a valid SOAP response."); } } catch (Exception $ex) { throw $ex; throw new Exception("Error occurred while Soap decoding: " . $ex->getMessage()); } return $responseXML; }
/** * {@inheritdoc} */ public function parse($xmlString) { if ($this->validateResponse) { XmlChecker::isValid($xmlString); } $useErrors = libxml_use_internal_errors(true); $xml = new \XMLReader(); $xml->xml($xmlString, 'UTF-8', LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS | LIBXML_PARSEHUGE); $xml->setParserProperty(\XMLReader::VALIDATE, false); $xml->setParserProperty(\XMLReader::LOADDTD, false); // This following assignments are auto-generated using Fxmlrpc\Serialization\CodeGenerator\XmlReaderParserBitmaskGenerator // Don’t edit manually static $flagmethodResponse = 0b1; static $flagparams = 0b10; static $flagfault = 0b100; static $flagparam = 0b1000; static $flagvalue = 0b10000; static $flagarray = 0b100000; static $flagmember = 0b1000000; static $flagname = 0b10000000; ${'flag#text'} = 0b100000000; static $flagstring = 0b1000000000; static $flagstruct = 0b10000000000; static $flagint = 0b100000000000; static $flagbiginteger = 0b1000000000000; static $flagi8 = 0b10000000000000; static $flagi4 = 0b100000000000000; static $flagi2 = 0b1000000000000000; static $flagi1 = 0b10000000000000000; static $flagboolean = 0b100000000000000000; static $flagdouble = 0b1000000000000000000; static $flagfloat = 0b10000000000000000000; static $flagbigdecimal = 0b100000000000000000000; ${'flagdateTime.iso8601'} = 0b1000000000000000000000; static $flagdateTime = 0b10000000000000000000000; static $flagbase64 = 0b100000000000000000000000; static $flagnil = 0b1000000000000000000000000; static $flagdom = 0b10000000000000000000000000; static $flagdata = 0b100000000000000000000000000; // End of auto-generated code $aggregates = []; $depth = 0; $nextExpectedElements = 0b1; $i = 0; $isFault = false; while ($xml->read()) { ++$i; $nodeType = $xml->nodeType; if ($nodeType === \XMLReader::COMMENT || $nodeType === \XMLReader::DOC_TYPE || $nodeType === \XMLReader::SIGNIFICANT_WHITESPACE && ($nextExpectedElements & 0b100000000) !== 0b100000000) { continue; } if ($nodeType === \XMLReader::ENTITY_REF) { return ''; } $tagName = $xml->localName; if ($nextExpectedElements !== null && ($flag = isset(${'flag' . $tagName}) ? ${'flag' . $tagName} : -1) && ($nextExpectedElements & $flag) !== $flag) { throw new UnexpectedTagException($tagName, $nextExpectedElements, get_defined_vars(), $xml->depth, $xml->readOuterXml()); } processing: switch ($nodeType) { case \XMLReader::ELEMENT: switch ($tagName) { case 'methodResponse': // Next: params, fault $nextExpectedElements = 0b110; break; case 'params': // Next: param $nextExpectedElements = 0b1000; $aggregates[$depth] = []; break; case 'fault': $isFault = true; // Break intentionally omitted // Break intentionally omitted case 'param': // Next: value $nextExpectedElements = 0b10000; break; case 'array': $aggregates[++$depth] = []; // Break intentionally omitted // Break intentionally omitted case 'data': // Next: array, data, value $nextExpectedElements = 0b100000000000000000000110000; break; case 'struct': // Next: struct, member, value $nextExpectedElements = 0b10001010000; $aggregates[++$depth] = []; break; case 'member': // Next: name, value $nextExpectedElements = 0b10010000; $aggregates[++$depth] = []; break; case 'name': // Next: #text $nextExpectedElements = 0b100000000; $type = 'name'; break; case 'value': $nextExpectedElements = 0b11111111111111111100110000; $type = 'value'; $aggregates[$depth + 1] = ''; break; case 'base64': case 'string': case 'biginteger': case 'i8': case 'dateTime.iso8601': case 'dateTime': // Next: value, $tagName, #text $nextExpectedElements = 0b100010000 | ${'flag' . $tagName}; $type = $tagName; $aggregates[$depth + 1] = ''; break; case 'nil': // Next: value, $tagName $nextExpectedElements = 0b1000000000000000000010000 | ${'flag' . $tagName}; $type = $tagName; $aggregates[$depth + 1] = null; break; case 'int': case 'i4': case 'i2': case 'i1': // Next: value, #text, $tagName $nextExpectedElements = 0b100010000 | ${'flag' . $tagName}; $type = $tagName; $aggregates[$depth + 1] = 0; break; case 'boolean': // Next: value, #text, $tagName $nextExpectedElements = 0b100010000 | ${'flag' . $tagName}; $type = 'boolean'; $aggregates[$depth + 1] = false; break; case 'double': case 'float': case 'bigdecimal': // Next: value, #text, $tagName $nextExpectedElements = 0b100010000 | ${'flag' . $tagName}; $type = $tagName; $aggregates[$depth + 1] = 0.0; break; case 'dom': $type = 'dom'; // Disable type checking $nextExpectedElements = null; $aggregates[$depth + 1] = $xml->readInnerXml(); break; } break; case \XMLReader::END_ELEMENT: switch ($tagName) { case 'params': case 'fault': break 3; case 'param': // Next: params, param $nextExpectedElements = 0b1010; break; case 'value': $nextExpectedElements = 0b100100000011100100011011100; $aggregates[$depth][] = $aggregates[$depth + 1]; break; case 'array': case 'struct': --$depth; // Break intentionally omitted // Break intentionally omitted case 'string': case 'int': case 'biginteger': case 'i8': case 'i4': case 'i2': case 'i1': case 'boolean': case 'double': case 'float': case 'bigdecimal': case 'dateTime.iso8601': case 'dateTime': case 'base64': case 'nil': // Next: value $nextExpectedElements = 0b10000; break; case 'data': // Next: array $nextExpectedElements = 0b100000; break; case 'name': // Next: value, member $nextExpectedElements = 0b1010000; $aggregates[$depth]['name'] = $aggregates[$depth + 1]; break; case 'member': // Next: struct, member $nextExpectedElements = 0b10001000000; $aggregates[$depth - 1][$aggregates[$depth]['name']] = $aggregates[$depth][0]; unset($aggregates[$depth], $aggregates[$depth + 1]); --$depth; break; } break; case \XMLReader::TEXT: case \XMLReader::SIGNIFICANT_WHITESPACE: switch ($type) { case 'int': case 'i4': case 'i2': case 'i1': $value = (int) $xml->value; break; case 'boolean': $value = $xml->value === '1'; break; case 'double': case 'float': case 'bigdecimal': $value = (double) $xml->value; break; case 'dateTime.iso8601': $value = \DateTime::createFromFormat('Ymd\\TH:i:s', $xml->value, isset($timezone) ? $timezone : ($timezone = new \DateTimeZone('UTC'))); break; case 'dateTime': $value = \DateTime::createFromFormat('Y-m-d\\TH:i:s.uP', $xml->value, isset($timezone) ? $timezone : ($timezone = new \DateTimeZone('UTC'))); break; case 'base64': $value = Base64Value::deserialize($xml->value); break; case 'dom': $doc = new \DOMDocument('1.0', 'UTF-8'); $doc->loadXML($aggregates[$depth + 1]); $value = $doc; break; default: $value =& $xml->value; break; } $aggregates[$depth + 1] = $value; if ($nextExpectedElements === null) { break; } // Next: any $nextExpectedElements = 0b111111111111111111111111111; break; } if ($xml->isEmptyElement && $nodeType !== \XMLReader::END_ELEMENT) { $nodeType = \XMLReader::END_ELEMENT; goto processing; } } libxml_use_internal_errors($useErrors); $result = $aggregates ? array_pop($aggregates[0]) : null; if ($isFault) { throw FaultException::createFromResponse($result); } return $result; }
/** * Extract the contents of the first div tag in the XHTML help document * * @return string * @throws \Nethgui\Exception\HttpException */ protected function readHelpDocument($filePath) { $document = new \XMLReader(); set_error_handler(function ($errno, $errstr) { }, E_WARNING | E_NOTICE); if ($document->open('file://' . $filePath, 'utf-8', LIBXML_NOENT) === TRUE) { // Advance to BODY tag: while ($document->name != 'body' && $document->read()) { } while ($document->name != 'div' && $document->read()) { } $content = $document->readInnerXml(); } else { $content = 'Not found'; throw new \Nethgui\Exception\HttpException(sprintf("%s: resource not found", __CLASS__), 404, 1333119424); } restore_error_handler(); return $this->expandIncludes($content); }
public function readInnerXml() { return parent::readInnerXml(); }
/** * This function should determine the correct way to wrap a given html snipped to produce valid html. * - for a transparent content model element T the function is called recursively to determine the content model of T. * - phrasing content is a subset of flow content. * - div may contain any flow content and is allowed wherever flow content is allowed. * - span may contain phrasing content only and is only allowed where phrasing content is expected. * * Thus as soon as we find any flow content element that is not phrasing content, we use div; * else we use span. * * @param $content string: the html code we want to wrap in a semantically meaningless element * @return string the tag name to wrap the content in a valid way. */ public function getSurroundingElement($content) { $xml = new XMLReader(); $wrappedContent = "<root>{$content}</root>"; $xml->XML($wrappedContent); $debugTrace = ''; // $phrasingContentElements = [ // 'a', 'abbr', 'area', 'audio', // 'b', 'bdi', 'bdo', 'br', 'button', // 'canvas', 'cite', 'code', // 'data', 'datalist', 'del', 'dfn', // 'em', 'embed', // 'i', 'iframe', 'img', 'input', 'ins', // 'kbd', 'keygen', // 'label', 'link', //(if it is allowed in the body) // 'map', 'mark', 'math', 'meta' /* (if the itemprop attribute is present) */, 'meter', // 'noscript', // 'object', 'output', // 'picture', 'progress', // 'q', // 'ruby', // 's', 'samp', 'script', 'select', 'small', 'span', 'strong', 'sub', 'sup', 'svg', // 'template', 'textarea', 'time', // 'u', // 'var', 'video', // 'wbr' // // text elements // ]; $notPhrasingFlowContentElements = ['address', 'article', 'aside', 'blockquote', 'details', 'dialog', 'div', 'dl', 'fieldset', 'figure', 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup', 'hr', 'main', 'mark', 'menu', 'meter', 'nav', 'p', 'pre', 'section', 'style', 'table', 'ul']; $transparentElements = ['a', 'ins', 'del', 'object', 'map', 'noscript', 'canvas']; ob_start(); //"warningCallback"); $xml->read(); // go to the root node $xml->read(); // skip the root node do { $debugTrace .= $xml->name . '('; //.$read.'#'; if ($xml->nodeType == XMLReader::ELEMENT) { // if element name is a notPhrasingFlowContentElement, we can return div: $normalizedName = strtolower($xml->name); if (in_array($normalizedName, $transparentElements)) { $contentModelFromRecursion = $this->getSurroundingElement($xml->readInnerXml()); if ($contentModelFromRecursion['errorOccurred']) { return array('nodeName' => 'span', 'trace' => $debugTrace, 'errorOccurred' => true); } elseif ($contentModelFromRecursion['nodeName'] == 'div') { $debugTrace = $debugTrace . $contentModelFromRecursion['trace'] . ')'; return array('nodeName' => 'div', 'trace' => $debugTrace); } } elseif (in_array($normalizedName, $notPhrasingFlowContentElements)) { $debugTrace = $debugTrace . ')'; return array('nodeName' => 'div', 'trace' => $debugTrace); } } } while ($read = $xml->next()); $buffer = ob_get_clean(); $anyErrorOccurred = !empty($buffer); $debugTrace = $debugTrace . ')'; return array('nodeName' => 'span', 'trace' => $debugTrace, 'errorOccurred' => $anyErrorOccurred); }