/** * Serialize * * @param var $payload * @param io.streams.OutputStream $out * @return void */ public function serialize($payload, $out) { $t = new Tree(); $t->setEncoding('UTF-8'); if ($payload instanceof Payload) { $root = isset($payload->properties['name']) ? $payload->properties['name'] : 'root'; $val = $payload->value; } else { $root = 'root'; $val = $payload; } $out->write($t->getDeclaration() . "\n"); if ($val instanceof \Traversable) { $i = 0; $map = null; foreach ($val as $key => $element) { if (0 === $i++) { $out->write('<' . $root . '>'); $map = 0 !== $key; } $out->write($this->node($map ? $key : $root, $element)->getSource(INDENT_NONE, 'utf-8')); } if (null === $map) { $out->write('<' . $root . '/>'); } else { $out->write('</' . $root . '>'); } } else { $out->write($t->withRoot($this->node($root, $val))->getSource(INDENT_NONE)); } }
/** * @param Bucket $config * @param array $request */ public function __construct(Bucket $config, $request) { $this->addExpect('formId'); $this->request = $request; $this->config = $config; $this->response = new Tree('document'); $this->formData = $this->response->getRootNode()->addChild(new Node('form')); }
/** * Calculate payload * * @param com.google.gsa.feed.XmlFeed feed * @return string */ public function payload($feed) { $tree = new Tree('gsafeed'); $tree->addChild((new Node('header'))->withChild(new Node('datasource', $feed->dataSource()))->withChild(new Node('feedtype', $feed->feedType()->name()))); $group = $tree->addChild(new Node('group')); foreach ($feed->getRecords() as $record) { $record->visit($group->addChild(new Node('record'))); } return $tree->getDeclaration() . "\n" . self::FEED_DOC_TYPE . "\n" . $tree->getSource(INDENT_DEFAULT); }
/** * Constructor * */ public function __construct() { parent::__construct(); $this->formresult = new Node('formresult', null, ['serial' => time(), 'tz' => date('Z'), 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema', 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance']); $this->withRoot($this->formresult); $this->formvalues = $this->formresult->addChild(new Node('formvalues')); $this->formerrors = $this->formresult->addChild(new Node('formerrors')); }
/** * Construct a XML-RPC message from a string * * <code> * $msg= XmlRpcRequestMessage::fromString('<methodCall>...</methodCall>'); * </code> * * @param string string * @return webservices.xmlrpc.XmlRpcMessage */ public static function fromString($string) { $msg = new XmlRpcRequestMessage(); $msg->tree = \xml\Tree::fromString($string); // Set class and method members from XML structure $target = $msg->tree->root()->nodeAt(0)->getContent(); list($msg->class, $msg->method) = explode('.', $target); return $msg; }
/** * Run tests and return XML generated * * @param unittest.TestCase... tests * @return xml.Tree the output * @throws unittest.AssertionFailedError in case the generated XML is not well-formed */ protected function runTests() { foreach (func_get_args() as $test) { $this->suite->addTest($test); } $this->suite->run(); try { return Tree::fromString($this->out->getBytes()); } catch (\xml\XMLFormatException $e) { $this->fail('XML generated is not well-formed: ' . $e->getMessage(), $this->out->getBytes(), null); } }
/** * Construct a XML-RPC response message from a string * * <code> * $msg= XmlRpcResponseMessage::fromString('<methodCall>...</methodCall>'); * </code> * * @param string string * @return webservices.xmlrpc.XmlRpcResponse Message */ public static function fromString($string) { $msg = new self(); $msg->tree = \xml\Tree::fromString($string); if (!$msg->tree->root()->hasChildren()) { throw new \lang\FormatException('Response is not well formed'); } // Set class and method members from XML structure $target = $msg->tree->root()->nodeAt(0)->getContent(); @(list($msg->class, $msg->method) = explode('.', $target)); return $msg; }
/** * Decode xml * * @param string xml * @return mixed */ protected function decode($xml) { return create(new XmlRpcDecoder())->decode(Tree::fromString('<?xml version="1.0" encoding="utf-8"?><value>' . $xml . '</value>')->root()); }
public function performance() { $s = microtime(true); $t = new Tree(); for ($i = 0; $i < 100; $i++) { $c = $t->addChild(new \xml\Node('child', null, array('id' => $i))); for ($j = 0; $j < 100; $j++) { $c->addChild(new \xml\Node('elements', str_repeat('x', $j))); } } $l = strlen($t->getSource(INDENT_NONE)); printf('%d bytes, %.3f seconds', $l, microtime(true) - $s); }
/** * Set XML from a tree * * @param xml.Tree xml */ public function setXMLTree(Tree $xml) { libxml_get_last_error() && libxml_clear_errors(); $this->document = new \DOMDocument(); $this->document->loadXML($xml->getDeclaration() . $xml->getSource(INDENT_NONE)); $this->_checkErrors($xml); }
/** * Construct a SOAP message from a file * * <code> * $msg= SOAPMessage::fromFile(new File('foo.soap.xml'); * </code> * * @param io.File file * @return xml.Tree */ public static function fromFile($file, $c = __CLASS__) { return parent::fromFile($file, $c); }
/** * Create a WddxMessage object from an XML document. * * @param string string * @return webservices.wddx.WddxMessage */ public static function fromString($string) { return parent::fromString($string, 'WddxMessage'); }
public function queryTreeWithEncoding() { $value = new String('value öäü', 'iso-8859-1'); $xpath = new XPath(Tree::fromString(sprintf('<?xml version="1.0" encoding="iso-8859-1"?>' . '<document><node>%s</node></document>', $value->getBytes('iso-8859-1')))); $this->assertEquals($value, new String($xpath->query('string(/document/node)'), 'utf-8')); }
/** * Marshal an object to xml * * @param lang.Object instance * @param xml.QName qname default NULL * @return string xml * @deprecated Use marshalTo() instead */ public static function marshal($instance, $qname = null) { $class = $instance->getClass(); // Create XML tree and root node. Use the information provided by the // qname argument if existant, use the class` non-qualified (and // lowercased) name otherwise. $tree = new Tree(); if ($qname) { $prefix = $qname->prefix ? $qname->prefix : $qname->localpart[0]; $tree->root()->setName($prefix . ':' . $qname->localpart); $tree->root()->setAttribute('xmlns:' . $prefix, $qname->namespace); } else { if ($class->hasAnnotation('xmlns')) { $tree->root()->setName($class->getSimpleName()); } else { $tree->root()->setName(strtolower($class->getSimpleName())); } } self::recurse($instance, $class, $tree->root(), []); return $tree->getSource(INDENT_DEFAULT); }
public function queryTreeWithEncoding() { $xpath = new XPath(Tree::fromString(sprintf('<?xml version="1.0" encoding="iso-8859-1"?>' . '<document><node>%s</node></document>', utf8_decode('öäü')))); $this->assertEquals('öäü', $xpath->query('string(/document/node)')); }
/** * Callback for XML parser * * @param resource parser * @param string name * @see xp://xml.parser.XMLParser */ public function onEndElement($parser, $name) { static $trans; $path = $this->_pathname(); parent::onEndElement($parser, $name); if ($this->_cnt <= 0) { return; } // Replace < &, &#XX; etc. if (!isset($trans)) { $trans = array_flip(get_html_translation_table(HTML_SPECIALCHARS)); } $cdata = trim($this->_objs[$this->_cnt + 1]->content); $name = strtr(substr($path, 0, -1), ['/rdf:rdf/' => '', '/rss/' => '']); switch ($name) { case 'channel/title': $this->channel->title = $cdata; break; case 'channel/link': $this->channel->link = $cdata; break; case 'channel/description': $this->channel->description = $cdata; break; case 'channel/language': case 'channel/dc:language': $this->channel->language = $cdata; break; case 'channel/copyright': case 'channel/dc:rights': $this->channel->copyright = $cdata; break; case 'channel/pubdate': // 14 May 2002 $this->channel->date = new Date($cdata); break; case 'channel/dc:date': // 2002-07-12T15:59 or 2003-12-19T12:26:00+01:00 $this->channel->date = new Date($cdata); break; case 'channel/dc:publisher': $this->channel->publisher = $cdata; break; case 'channel/dc:creator': $this->channel->creator = $cdata; break; case 'channel/image/url': case 'image/url': $this->image->url = $cdata; break; case 'channel/image/title': case 'image/title': $this->image->title = $cdata; break; case 'channel/image/link': case 'image/link': $this->image->link = $cdata; break; case 'channel/item/title': case 'item/title': $this->items[sizeof($this->items) - 1]->title = $cdata; break; case 'channel/item/description': case 'item/description': $this->items[sizeof($this->items) - 1]->description = $cdata; break; case 'channel/item/link': case 'item/link': $this->items[sizeof($this->items) - 1]->link = $cdata; break; case 'channel/item/date': // 14 May 2002 // 14 May 2002 case 'channel/item/pubdate': $this->items[sizeof($this->items) - 1]->date = new Date($cdata); break; case 'channel/item/dc:date': $this->items[sizeof($this->items) - 1]->date = new Date($cdata); break; case 'channel/item/category': $this->items[sizeof($this->items) - 1]->category = $cdata; break; case 'channel/item/author': $this->items[sizeof($this->items) - 1]->author = $cdata; break; case 'channel/item/content:encoded': $this->items[sizeof($this->items) - 1]->content = $cdata; break; case 'channel/item/guid': $this->items[sizeof($this->items) - 1]->guid = $cdata; break; case 'item/dc:date': // 2002-07-12T15:59 or 2003-12-19T12:26:00+01:00 $this->items[sizeof($this->items) - 1]->date = new Date($cdata); break; default: // Ignore } }