ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Sabre\Xml\Element\XmlFragment Class Reference

The XmlFragment element allows you to extract a portion of your xml tree, and get a well-formed xml string. More...

+ Inheritance diagram for Sabre\Xml\Element\XmlFragment:
+ Collaboration diagram for Sabre\Xml\Element\XmlFragment:

Public Member Functions

 __construct ($xml)
 
 getXml ()
 
 xmlSerialize (Writer $writer)
 The xmlSerialize metod is called during xml writing. More...
 
 xmlSerialize (Writer $writer)
 The xmlSerialize method is called during xml writing. More...
 

Static Public Member Functions

static xmlDeserialize (Reader $reader)
 The deserialize method is called during xml parsing. More...
 
static xmlDeserialize (Reader $reader)
 The deserialize method is called during xml parsing. More...
 

Protected Attributes

 $xml
 

Detailed Description

The XmlFragment element allows you to extract a portion of your xml tree, and get a well-formed xml string.

This goes a bit beyond innerXml and friends, as we'll also match all the correct namespaces.

Please note that the XML fragment:

  1. Will not have an <?xml declaration.
  2. Or a DTD
  3. It will have all the relevant xmlns attributes.
  4. It may not have a root element.

Definition at line 23 of file XmlFragment.php.

Constructor & Destructor Documentation

◆ __construct()

Sabre\Xml\Element\XmlFragment::__construct (   $xml)

Definition at line 27 of file XmlFragment.php.

27 {
28
29 $this->xml = $xml;
30
31 }

References Sabre\Xml\Element\XmlFragment\$xml.

Member Function Documentation

◆ getXml()

Sabre\Xml\Element\XmlFragment::getXml ( )

Definition at line 33 of file XmlFragment.php.

33 {
34
35 return $this->xml;
36
37 }

References Sabre\Xml\Element\XmlFragment\$xml.

Referenced by Sabre\Xml\Element\XmlFragment\xmlSerialize().

+ Here is the caller graph for this function:

◆ xmlDeserialize()

static Sabre\Xml\Element\XmlFragment::xmlDeserialize ( Reader  $reader)
static

The deserialize method is called during xml parsing.

This method is called statictly, this is because in theory this method may be used as a type of constructor, or factory method.

Often you want to return an instance of the current class, but you are free to return other data as well.

You are responsible for advancing the reader to the next element. Not doing anything will result in a never-ending loop.

If you just want to skip parsing for this element altogether, you can just call $reader->next();

$reader->parseInnerTree() will parse the entire sub-tree, and advance to the next element.

Parameters
Reader$reader
Returns
mixed

Implements Sabre\Xml\XmlDeserializable.

Reimplemented in Sabre\DAV\Xml\Property\Complex.

Definition at line 139 of file XmlFragment.php.

139 {
140
141 $result = new self($reader->readInnerXml());
142 $reader->next();
143 return $result;
144
145 }
$result

References $reader, and $result.

◆ xmlSerialize()

Sabre\Xml\Element\XmlFragment::xmlSerialize ( Writer  $writer)

The xmlSerialize metod is called during xml writing.

Use the $writer argument to write its own xml serialization.

An important note: do not create a parent element. Any element implementing XmlSerializble should only ever write what's considered its 'inner xml'.

The parent of the current element is responsible for writing a containing element.

This allows serializers to be re-used for different element names.

If you are opening new elements, you must also close them again.

Parameters
Writer$writer
Returns
void

Implements Sabre\Xml\XmlSerializable.

Definition at line 58 of file XmlFragment.php.

58 {
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>
67XML;
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 }
The Reader class expands upon PHP's built-in XMLReader.
Definition: Reader.php:20
writeAttribute($name, $value)
Writes a new attribute.
Definition: Writer.php:230
startElement($name)
Opens a new element.
Definition: Writer.php:121

References $reader, Sabre\Xml\Element\XmlFragment\$xml, Sabre\Xml\Element\XmlFragment\getXml(), Sabre\Xml\Writer\startElement(), and Sabre\Xml\Writer\writeAttribute().

+ Here is the call graph for this function:

Field Documentation

◆ $xml


The documentation for this class was generated from the following file: