1: <?php
2:
3: class RESTian_Application_Xml_Parser extends RESTian_Parser_Base {
4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: function parse( $body ) {
17: if ( empty( $body ) )
18: return array();
19:
20: $is_array = false;
21: $xml = is_string( $body ) ? new SimpleXMLElement( $body ) : $body;
22:
23: $data = (array)$xml->attributes();
24: if ( 0 == count( $data ) )
25: $data['@attributes'] = array();
26:
27: 28: 29:
30: foreach ($xml as $element) {
31: $tag = $element->getName();
32: $e = get_object_vars( $element );
33: if ( ! empty( $e ) ) {
34: $subset = $element instanceof SimpleXMLElement ? $this->parse( $element ) : $e;
35: } else {
36: $subset = trim( $element );
37: }
38: if ( ! isset( $data[$tag] ) ) {
39: $data[$tag] = $subset;
40: } else {
41: if ( is_array( $data[$tag] ) ) {
42: $data[$tag][] = $subset;
43: } else {
44: 45: 46:
47: $data[$tag] = array( $data[$tag], $subset );
48: $is_array = true;
49: }
50: }
51: }
52: return $is_array ? $data : (object)$data;
53: }
54: }
55: