public function run() { $apiDocument = $this->document->createElement("api"); try { $apiDocument = $this->execute($apiDocument); } catch (ApiException $ex) { $exception = $this->document->createElement("error"); $exception->setAttribute("message", $ex->getMessage()); $apiDocument->appendChild($exception); } $this->document->appendChild($apiDocument); return $this->document->saveXml(); }
/** * Extracts information from the file $fileName associated with the url $url. * * The document type for this document is given in $type, and the images on * disk should be in the directory named $imagePath. The urls where the * images link to should be in $imageUrlPath. * * @param string $fileName * @param string $type * @param string $url * @param string $imagePath * @param string $imageUrlPath * @return array(ezcSearchDocument) */ public static function extract($fileName, $type, $url, $imagePath = null, $imageUrlPath = null) { $published = filemtime($fileName); $converted = file_get_contents($fileName); $dom = new DomDocument(); @$dom->loadHtml($converted); $tbody = $dom->getElementsByTagName('div')->item(0); $xpath = new DOMXPath($dom); $tocElem = $xpath->evaluate("//h1[@class='title']", $tbody)->item(0); $title = $tocElem ? $tocElem->nodeValue : 'no title'; $docs = array(); $body = $urls = array(); $currentUrl = $url; $lastUrl = $url; $currentBody = ''; // child::*[self::p or self::h1] $xpath = new DOMXPath($dom); $tbody = $xpath->evaluate("//p|//h1|//ol|//ul|//dl|//img|//a", $tbody); $body = ''; foreach ($tbody as $item) { switch ($item->tagName) { case 'a': $name = $item->getAttribute('name'); if (strlen($name)) { $currentUrl = $url . '#' . $name; } break; case 'img': $alt = $item->getAttribute('alt'); $src = $item->getAttribute('src'); $location = $imagePath == null ? dirname($fileName) . '/' . $src : $imagePath . '/' . preg_replace('@(\\.\\./)+@', '', $src); $imgurl = $src[0] == '/' ? $src : ($imageUrlPath === null ? $url . '/' . $src : $imageUrlPath . '/' . preg_replace('@(\\.\\./)+@', '', $src)); echo " - {$src} => {$imgurl}\n"; $docs[] = self::extractImage($alt, $location, $imgurl); break; case 'p': case 'h1': case 'dl': if ($lastUrl !== $currentUrl) { $docs[] = new ezcSearchSimpleArticle(null, $title, $currentBody, $published, $lastUrl, $type); $currentBody = ''; $lastUrl = $currentUrl; } $currentBody .= strip_tags($dom->saveXml($item)) . "\n\n"; break; } } if ($currentBody != '') { $docs[] = new ezcSearchSimpleArticle(null, $title, $currentBody, $published, $lastUrl, $type); } return $docs; }
public function prettyPrint($code, $lang) { switch ($lang) { case 'json': return json_encode(json_decode($code), JSON_PRETTY_PRINT); case 'xml': $xml = new \DomDocument('1.0'); $xml->preserveWhiteSpace = false; $xml->formatOutput = true; $xml->loadXml($code); return $xml->saveXml(); default: return $code; } }
/** * Renders the element to string * @return string */ public function renderElement() { if (isset($this->tag) && $this->tag instanceof DOMElement) { if ($this->dom->hasChildNodes()) { $this->dom->insertBefore($this->tag, $this->dom->firstChild); } else { $this->dom->appendChild($this->tag); } } $out = ''; foreach ($this->dom->childNodes as $node) { $out .= $this->dom->saveXml($node); } return $out; }
public static function unmarshalRoot($string) { $dom = new \DomDocument(); $dom->loadXml($string); $xpath = new \DomXPath($dom); $query = "child::*"; $childs = $xpath->query($query); $binding = array(); foreach ($childs as $child) { $legko = new Bind(); $lDom = new \DomDocument(); $lDom->appendChild($lDom->importNode($child, true)); $binding = $legko->unmarshal($lDom->saveXml()); } return $binding; }
/** * Massage the SVG image data for converters which don't understand some path data syntax. * * This is necessary for rsvg and ImageMagick when compiled with rsvg support. * Upstream bug is https://bugzilla.gnome.org/show_bug.cgi?id=620923, fixed 2014-11-10, so * this will be needed for a while. (T76852) * * @param string $svg SVG image data * @return string Massaged SVG image data */ protected function massageSvgPathdata($svg) { $dom = new DomDocument(); $dom->loadXml($svg); foreach ($dom->getElementsByTagName('path') as $node) { $pathData = $node->getAttribute('d'); // Make sure there is at least one space between numbers, and that leading zero is not omitted. // rsvg has issues with syntax like "M-1-2" and "M.445.483" and especially "M-.445-.483". $pathData = preg_replace('/(-?)(\\d*\\.\\d+|\\d+)/', ' ${1}0$2 ', $pathData); // Strip unnecessary leading zeroes for prettiness, not strictly necessary $pathData = preg_replace('/([ -])0(\\d)/', '$1$2', $pathData); $node->setAttribute('d', $pathData); } return $dom->saveXml(); }
/** * Generate and return the OsmChange XML required to record the changes * made to the object in question. * * @return string * @link http://wiki.openstreetmap.org/wiki/OsmChange */ public function getOsmChangeXml() { $type = $this->getType(); if ($this->dirty) { $version = $this->getVersion(); $version++; $domd = new DomDocument(); $domd->loadXml($this->getXml()); $xpath = new DomXPath($domd); $nodelist = $xpath->query("//{$type}"); $nodelist->item(0)->setAttribute('action', $this->action); $nodelist->item(0)->setAttribute('id', $this->getId()); if (!is_null($this->changesetId)) { $nodelist->item(0)->setAttribute('changeset', $this->changesetId); } $tags = $xpath->query("//{$type}/tag"); $set = array(); for ($i = 0; $i < $tags->length; $i++) { $key = $tags->item($i)->getAttribute('k'); $val = $tags->item($i)->getAttribute('v'); $set[$key] = $val; } $diff = array_diff($this->getTags(), $set); // Remove existing tags for ($i = 0; $i < $tags->length; $i++) { $rkey = $tags->item($i)->getAttribute('k'); if (isset($diff[$rkey])) { $nodelist->item(0)->removeChild($tags->item($i)); } } foreach ($diff as $key => $value) { $new = $domd->createElement('tag'); $new->setAttribute('k', $key); $new->setAttribute('v', $value); $nodelist->item(0)->appendChild($new); } $xml = $domd->saveXml($nodelist->item(0)); $xml = "<{$this->action}>{$xml}</{$this->action}>"; return $this->osmChangeXml($xml); } elseif ($this->action == 'delete') { $xml = null; $domd = new DomDocument(); $domd->loadXml($this->getXml()); $xpath = new DomXPath($domd); $n = $xpath->query("//{$type}"); $version = $this->getVersion(); $version++; if (!is_null($this->changesetId)) { $n->item(0)->setAttribute('changeset', $this->changesetId); } $n->item(0)->setAttribute('action', 'delete'); $xml = $domd->saveXml($n->item(0)); return $this->osmChangeXml("<delete>{$xml}</delete>"); } }
/** * Highlights a keyword within a larger string. Won't insert the tags around the keyword if it's found within HTML tags. * see: http://stackoverflow.com/questions/4081372/highlight-keywords-in-a-paragraph * @param string $src String to search for the keyword text. * @param string $keyword String to search for and highlight. * @return string The original text with SPAN tags inserted around the keyword with a class attribute of 'highlight'. */ public static function highlightKeyword($src, $keyword) { $keyword = str_replace('*', '', $keyword); $src = "<div>{$src}</div>"; $dom = new DomDocument(); $dom->recover = true; @$dom->loadHtml($src); $xpath = new DomXpath($dom); $elements = $xpath->query('//*[contains(.,"' . $keyword . '")]'); foreach ($elements as $element) { foreach ($element->childNodes as $child) { if (!$child instanceof DomText) { continue; } $fragment = $dom->createDocumentFragment(); $text = $child->textContent; while (($pos = stripos($text, $keyword)) !== false) { $fragment->appendChild(new DomText(substr($text, 0, $pos))); $word = substr($text, $pos, strlen($keyword)); $highlight = $dom->createElement('span'); $highlight->appendChild(new DomText($word)); $highlight->setAttribute('class', 'searchterm'); $fragment->appendChild($highlight); $text = substr($text, $pos + strlen($keyword)); } if (!empty($text)) { $fragment->appendChild(new DomText($text)); } $element->replaceChild($fragment, $child); } } $str = $dom->saveXml($dom->getElementsByTagName('body')->item(0)->firstChild); return $str; }
if (!empty($profile_data['dob'])) { $Age = convert_birthDate2Age($profile_data['dob']); } $age->appendChild($xml_obj->createTextNode($Age)); $buddy->appendChild($age); //adding gender to the xml $gender = $xml_obj->createElement("sex"); $gender->appendChild($xml_obj->createTextNode(field_value(@$profile_data['sex'], ' '))); //$gender->appendChild($xml_obj->createTextNode('M')); $buddy->appendChild($gender); //adding effect to the xml $effect = $xml_obj->createElement("effect"); $event = $xml_obj->createElement("event"); $event->appendChild($xml_obj->createTextNode('baloon.say')); $effect->appendChild($event); $shoutout = $xml_obj->createElement("shoutout"); if (empty($profile_data['sub_caption'])) { $profile_data['sub_caption'] = 'Hi.'; } $shoutout->appendChild($xml_obj->createTextNode($profile_data['sub_caption'])); $effect->appendChild($shoutout); $buddy->appendChild($effect); $buddylist->appendChild($buddy); } $xml_obj->appendChild($buddylist); $xml = $xml_obj->saveXml(); /*$file_name = $path_prefix.'/web/blist.xml'; $handle = fopen($file_name, "w"); fwrite($handle, $xml); */ echo $xml; }
function saveXML($simplexml) { global $config; $domDoc = new DomDocument('1.0', 'utf-8'); $domDoc->formatOutput = true; $domDoc->preserveWhiteSpace = false; $domDoc->loadXml($simplexml->asXml()); return file_put_contents($config['xml_tv'], $domDoc->saveXml()); }
/** * exports an array of instances into an rdf string * * @param array $instances * @return string */ public function getRdfString($instances) { $api = core_kernel_impl_ApiModelOO::singleton(); $xmls = array(); foreach ($instances as $instance) { $xmls[] = $api->getResourceDescriptionXML($instance->getUri()); } if (count($xmls) == 1) { $rdf = $xmls[0]; } elseif (count($xmls) > 1) { //merge the xml of each instances... $baseDom = new DomDocument(); $baseDom->formatOutput = true; $baseDom->loadXML($xmls[0]); for ($i = 1; $i < count($xmls); $i++) { $xmlDoc = new SimpleXMLElement($xmls[$i]); foreach ($xmlDoc->getNamespaces() as $nsName => $nsUri) { if (!$baseDom->documentElement->hasAttribute('xmlns:' . $nsName)) { $baseDom->documentElement->setAttribute('xmlns:' . $nsName, $nsUri); } } $newDom = new DOMDocument(); $newDom->loadXml($xmls[$i]); foreach ($newDom->getElementsByTagNameNS('http://www.w3.org/1999/02/22-rdf-syntax-ns#', "Description") as $desc) { $newNode = $baseDom->importNode($desc, true); $baseDom->documentElement->appendChild($newNode); } } $rdf = $baseDom->saveXml(); } return $rdf; }
/** * Build the request XML to send to the Eway API * * @return string request xml */ protected function getRequestXml() { $xml = new \DomDocument(); $ewayGateway = $xml->createElement('ewaygateway'); foreach (self::$requestFields as $field => $length) { $value = isset($this->requestData[$field]) ? $this->requestData[$field] : null; $ewayGateway->appendChild($xml->createElement($field, $value)); } $xml->appendChild($ewayGateway); return $xml->saveXml(); }
/** * {@inheritDoc} */ public function getAccounts(Bank $bank) { if (!$this->logged) { throw new \RuntimeException('Call login() first'); } $accounts = array(); $browser = $this->getBrowser(); $response = $browser->request()->getBody(); $dom = new \DomDocument(); @$dom->loadHTML($response); $xpath = new \DomXPath($dom); foreach ($xpath->query("//div[@class='dv']") as $div) { $xmlAccount = $xpath->query("a", $div); if ($xmlAccount->length > 0) { $id = preg_replace('/(.*)<br *\\/>(\\d*)<div.*/', '$2', $dom->saveXml($div)); $balance = str_replace(',', '.', $xpath->query("div", $div)->item(0)->textContent); $balance = preg_replace("/[\\xA0\\xC2 €]/", '', $balance); $account = new Account(); $account->setBank($bank)->setId($id)->setLabel($xmlAccount->item(0)->textContent)->setLink($xmlAccount->item(0)->getAttribute('href'))->setBalance($balance); $accounts[$account->getId()] = $account; } } return $accounts; }
} catch (PAException $e) { $error = '<li>' . $e->message . '</li>'; } $html = '<ul>'; if (!empty($view_content)) { foreach ($view_content as $value) { if (!empty($value->title)) { $html .= '<li><a href= "' . $value->url . '">' . $value->title . '</a></li>'; } } } else { $html .= $error; } $html .= '</ul>'; //creating the rss $dom = new DomDocument(); $format = 'D, j M Y H:m:s O'; $rss = $dom->createElement('rss'); $attrib1 = $rss->setAttribute("xmlns:content", "http://purl.org/rss/1.0/modules/content/"); $attrib = $rss->setAttribute("version", "2.0"); $root = $dom->createElement("channel"); //this is the only item entry $node = $dom->createElement("item"); $description = $dom->createCDATASection($html); $node->appendChild($description); $root->appendChild($node); $rss->appendChild($root); $dom->appendChild($rss); header('Content-Type: text/xml'); print $dom->saveXml();
/** * Use for debug xslt process * @param DomDocument $domDocument * @param DomDocument $xslTemplate * @return unknown_type */ private function _debug(DomDocument $domDocument, DomDocument $xslTemplate) { if (!$this->_debugMode) { return false; } $domDocument->formatOutput = true; $xmlContent = $domDocument->saveXml(); $xsltContent = $xslTemplate->saveXml(); file_put_contents("/tmp/result.xsl", $xsltContent); file_put_contents("/tmp/result.xml", $xmlContent); $request = Zend_Controller_Front::getInstance()->getRequest(); if ($request instanceof Zend_Controller_Request_Abstract && null !== ($debugMode = $request->getParam('d'))) { switch ($debugMode) { case 'xml': Zend_Controller_Front::getInstance()->getResponse()->setHeader('Content-type', 'text/xml'); return $xmlContent; break; case 'xsl': Zend_Controller_Front::getInstance()->getResponse()->setHeader('Content-type', 'text/xml'); return $xsltContent; break; } } return false; }
/** * Amend osmChangeXml with specific updates pertinent to this Way object. * * @param string $xml OSM Change XML as generated by getOsmChangeXml * * @return string * @see getOsmChangeXml * @link http://wiki.openstreetmap.org/wiki/OsmChange */ public function osmChangeXml($xml) { if ($this->dirtyNodes) { $domd = new DomDocument(); $domd->loadXml($xml); $xpath = new DomXPath($domd); $nodelist = $xpath->query('//' . $this->action . '/way'); $nd = $xpath->query("//{$this->action}/way/nd"); // Remove nodes if appropriate. for ($i = 0; $i < $nd->length; $i++) { $ref = $nd->item($i)->getAttribute('ref'); if (array_search($ref, $this->nodes) === false) { $nodelist->item(0)->removeChild($nd->item($i)); } } // Add new nodes. foreach ($this->nodesNew as $new) { $el = $domd->createElement('nd'); $el->setAttribute('ref', $new); $nodelist->item(0)->appendChild($el); } // Remove blank lines in XML - minimise bandwidth usage. return preg_replace("/(^[\r\n]*|[\r\n]+)[\\s\t]*[\r\n]+/", '', $domd->saveXml($nodelist->item(0))); return $domd->saveXml($nodelist->item(0)); } else { return $xml; } }
/** * Generates a XML encoded API call readable by the AC server. * * @return string */ public function toXml() { if (!$this->getAction()) { throw new AcApi_Exception('No action specified in request.'); } $dom = new DomDocument('1.0', 'utf-8'); $params = $dom->createElement('params'); $dom->appendChild($params); // set action $param = $dom->createElement('param', $this->getAction()); $param->setAttribute('name', 'action'); $params->appendChild($param); // set params foreach ($this->getParams() as $paramName => $paramValue) { $param = $dom->createElement('param', $paramValue); $param->setAttribute('name', $paramName); $params->appendChild($param); } return $dom->saveXml(); }