- Post in | WebDevelop/PHP
- Post at | 2007. 8. 19. 00:32 | by 쥬리엘
XML 생성 Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | <?php class Xml_Writer { var $xml ; var $indent ; var $stack = array (); function XmlWriter( $indent = ' ' ) { $this ->indent = $indent ; $this ->xml = '<!--?xml version="1.0" encoding="utf-8"?-->' . "\n" ; } function _indent() { for ( $i = 0, $j = count ( $this ->stack); $i < $j ; $i ++) { $this ->xml .= $this ->indent; } } function push( $element , $attributes = array ()) { $this ->_indent(); $this ->xml .= '<' . $element ; foreach ( $attributes as $key => $value ) { $this ->xml .= ' ' . $key . '="' .htmlentities( $value ). '"' ; } $this ->xml .= ">\n" ; $this ->stack[] = $element ; } function element( $element , $content , $attributes = array ()) { $this ->_indent(); $this ->xml .= '<' . $element ; foreach ( $attributes as $key => $value ) { $this ->xml .= ' ' . $key . '="' .htmlentities( $value ). '"' ; } $this ->xml .= '>' .htmlentities( $content ). '<!--' . $element . '-->' . "\n" ; } function emptyelement( $element , $attributes = array ()) { $this ->_indent(); $this ->xml .= '<' . $element ; foreach ( $attributes as $key => $value ) { $this ->xml .= ' ' . $key . '="' .htmlentities( $value ). '"' ; } $this ->xml .= " />\n" ; } function pop() { $element = array_pop ( $this ->stack); $this ->_indent(); $this ->xml .= "<!--$element-->\n" ; } function getXml() { return $this ->xml; } } $xml = new Xml_Writer(); $array = array ( array ( 'monkey' , 'banana' , 'Jim' ), array ( 'hamster' , 'apples' , 'Kola' ), array ( 'turtle' , 'beans' , 'Berty' ), ); $xml ->push( 'zoo' ); foreach ( $array as $animal ) { $xml ->push( 'animal' , array ( 'species' => $animal [0])); $xml ->element( 'name' , $animal [2]); $xml ->element( 'food' , $animal [1]); $xml ->pop(); } $xml ->pop(); print $xml ->getXml(); ?> |
'WebDevelop > PHP' 카테고리의 다른 글
PHP란 무엇인가... (0) | 2007.09.17 |
---|---|
간단한 페이징 처리 (0) | 2007.08.31 |
history.back()해도 폼값 남아 있기 (0) | 2007.08.18 |