20 $retval = get_class($obj).
"\n";
22 case ($obj instanceof DOMDocument):
23 $retval .=
"XPath: {$obj->getNodePath()}\n".$obj->saveXML($obj);
25 case ($obj instanceof DOMElement):
26 $retval .=
"XPath: {$obj->getNodePath()}\n".$obj->ownerDocument->saveXML($obj);
28 case ($obj instanceof DOMAttr):
29 $retval .=
"XPath: {$obj->getNodePath()}\n".$obj->ownerDocument->saveXML($obj);
31 case ($obj instanceof DOMNodeList):
32 $retval .=
"NodeList of length {$obj->length}\n";
34 for ($i = 0; $i < $obj->length; $i++) {
35 $retval .=
"Item #$i, " .
xml_debug($obj->item($i));
39 return "Instance of unknown class";
49 if( is_string(
$xml ) ) {
50 $this->xml =
new DOMDocument();
51 $this->xml->loadXML(
$xml );
52 } elseif(
$xml instanceof DOMDocument ) {
55 throw new Exception(
"<xml> must be of type DOMDocument in new XMLAsArrayOf...(xml,context)");
57 $this->xpath =
new DOMXPath($this->xml);
61 if( $nodeList->length === 1 ) {
62 $this->context_node = $nodeList->item(0);
64 $this->context_node = $this->xml->documentElement;
69 $this->context_node = $this->xml->documentElement;
72 protected function query(
string $offset ) {
73 if( isset( $this->context_node ) ) {
74 return $this->xpath->query( $offset, $this->context_node );
76 return $this->xpath->query( $offset );
80 return $this->xml->saveXML( $this->context_node );
82 public function __get(
string $name ) {
83 $class = get_class($this);
84 return new $class( $this->xml, $this->
query($name) );
86 public function __set(
string $name, $value ) {
87 $this[$name] = $value;
90 return isset( $this[$name] );
93 unset( $this[$name] );
99 return $this->
query( $offset )->length === 1;
102 $nodeList = $this->
query( $offset );
103 if( $nodeList->length === 1 )
104 return $nodeList->item(0);
107 $nodeList = $this->
query( $offset );
108 if( $nodeList->length === 1 ) {
109 $node = $nodeList->item(0);
110 if($node->parentNode !== NULL) {
111 $node->parentNode->replaceChild( $value, $node );
113 } elseif( $nodeList->length === 0 ) {
114 $nodeList->context_node->appendChild( $value );
118 $nodeList = $this->
query( $offset );
119 if( $nodeList->length === 1 ) {
120 $node = $nodeList->item(0);
121 if($node->parentNode !== NULL) {
122 $node->parentNode->removeChild( $node );
130 return $this->
query( $offset )->length > 0;
133 return $this->
query( $offset );
136 $nodeList = $this->
query( $offset );
137 $cur_length = $nodeList->length;
138 $new_length = $value->length;
139 for( $i = 0 ; $i < min( $cur_length, $new_length ) ; $i++ ) {
140 $cur_node = $nodeList->item($i);
141 $new_node = $value->item($i);
142 $cur_node->parentNode->replaceChild( $new_node, $cur_node );
144 for( $i = min( $cur_length, $new_length ) ; $i < $cur_length ; $i++ ) {
145 $cur_node = $nodeList->item($i);
146 $cur_node->parentNode->removeChild( $cur_node );
148 for( $i = min( $cur_length, $new_length ) ; $i < $new_length ; $i++ ) {
149 $new_node = $value->item($i);
150 $this->context_node->appendChild( $new_node );
154 $nodeList = $this->
query( $offset );
155 $cur_length = $nodeList->length;
156 for( $i = 0 ; $i < $cur_length ; $i++ ) {
157 $cur_node = $nodeList->item($i);
158 $cur_node->parentNode->removeChild( $cur_node );
165 return $this->
query( $offset )->length === 1;
168 $nodeList = $this->
query( $offset );
169 if( $nodeList->length === 1 ) {
170 return $nodeList->item(0)->nodeValue;
174 $nodeList = $this->
query( $offset );
175 if( $nodeList->length === 1 ) {
177 $nodeList->item(0)->nodeValue = $value;
178 }
else if( $nodeList->length === 0 ) {
179 $node = $this->xml->createElement( basename($offset) );
180 $node->nodeValue = $value;
181 $this->contextNode->appendChild( $node );
185 if( $nodeList->length === 1 ) {
186 $node = $nodeList->item(0);
187 $node->parentNode->removeChild( $node );
193 $xml = file_get_contents(
"item-sample.xml");
195 print
"EnumerationA: " . $item_item_data[
'enumeration_a'] .
"\n\n";
196 $item_item_data[
'enumeration_a'] =
"NOOOOOOOOO";
197 print $item_item_data->xml->saveXML();
offsetSet( $offset, $value)
offsetSet( $offset, $value)
offsetSet( $offset, $value)