ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
XmlFragment.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\Xml\Element;
4 
8 
23 class XmlFragment implements Element {
24 
25  protected $xml;
26 
27  function __construct($xml) {
28 
29  $this->xml = $xml;
30 
31  }
32 
33  function getXml() {
34 
35  return $this->xml;
36 
37  }
38 
58  function xmlSerialize(Writer $writer) {
59 
60  $reader = new Reader();
61 
62  // Wrapping the xml in a container, so root-less values can still be
63  // parsed.
64  $xml = <<<XML
65 <?xml version="1.0"?>
66 <xml-fragment xmlns="http://sabre.io/ns">{$this->getXml()}</xml-fragment>
67 XML;
68 
69  $reader->xml($xml);
70 
71  while ($reader->read()) {
72 
73  if ($reader->depth < 1) {
74  // Skipping the root node.
75  continue;
76  }
77 
78  switch ($reader->nodeType) {
79 
80  case Reader::ELEMENT :
81  $writer->startElement(
82  $reader->getClark()
83  );
84  $empty = $reader->isEmptyElement;
85  while ($reader->moveToNextAttribute()) {
86  switch ($reader->namespaceURI) {
87  case '' :
88  $writer->writeAttribute($reader->localName, $reader->value);
89  break;
90  case 'http://www.w3.org/2000/xmlns/' :
91  // Skip namespace declarations
92  break;
93  default :
94  $writer->writeAttribute($reader->getClark(), $reader->value);
95  break;
96  }
97  }
98  if ($empty) {
99  $writer->endElement();
100  }
101  break;
102  case Reader::CDATA :
103  case Reader::TEXT :
104  $writer->text(
105  $reader->value
106  );
107  break;
108  case Reader::END_ELEMENT :
109  $writer->endElement();
110  break;
111 
112  }
113 
114  }
115 
116  }
117 
139  static function xmlDeserialize(Reader $reader) {
140 
141  $result = new self($reader->readInnerXml());
142  $reader->next();
143  return $result;
144 
145  }
146 
147 }
The XmlFragment element allows you to extract a portion of your xml tree, and get a well-formed xml s...
Definition: XmlFragment.php:23
$result
writeAttribute($name, $value)
Writes a new attribute.
Definition: Writer.php:230
startElement($name)
Opens a new element.
Definition: Writer.php:121
The Reader class expands upon PHP&#39;s built-in XMLReader.
Definition: Reader.php:20
static xmlDeserialize(Reader $reader)
The deserialize method is called during xml parsing.
xmlSerialize(Writer $writer)
The xmlSerialize metod is called during xml writing.
Definition: XmlFragment.php:58
This is the XML element interface.
Definition: Element.php:18
The XML Writer class.
Definition: Writer.php:31