이것저것 연구소
BLOG
TAG
MEDIA
LOCATION
GUESTBOOK
php로 XML 생성 예제
[부설]코딩도움방
2009/01/05 16:45
php로 XML을 생성 하는 예제 입니다.
http://www.joon.pe.kr
에서 업어 왔습니다. 원 링크는 아래에 있습니다.
http://simonwillison.net/2003/Apr/29/xmlWriter/
언어 : php
<?php
// Simon Willison, 16th April 2003
// Based on Lars Marius Garshol's Python XMLWriter class
// See http://www.xml.com/pub/a/2003/04/09/py-xml.html
class
XmlWriter
{
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
XmlWriter
(
)
;
$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
(
'food'
,
$animal
[
1
]
)
;
$xml
->
element
(
'name'
,
$animal
[
2
]
)
;
$xml
->
pop
(
)
;
}
$xml
->
pop
(
)
;
print
$xml
->
getXml
(
)
;
?>
NO TRACKBACK
AND
NO COMMENT
http://mylab.tistory.com/trackback/31
by
guernica
ARTICLE CATEGORY
분류 전체보기
(32)
연구일지
(11)
리눅스랩
(7)
하드웨어랩
(1)
DIY분과
(1)
영상분석실
(8)
[부설]코딩도움방
(4)
ARCHIVE & SEARCH
--- Select Archive ---
2009/06 (1)
2009/05 (1)
2009/01 (2)
2008/12 (1)
2008/07 (1)
CALENDAR
«
2009/07
»
일
월
화
수
목
금
토
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
RECENT ARTICLE
iPod touch OS 3.0 설치
16대 대통령 선거때, 이회..
(4)
나도 이제 iPod TOUCH 유져!!
(2)
php로 XML 생성 예제
구글 새해로고. 올해도 시..
TAG CLOUD
ipod
ubuntu
fedora
Touch
이명박
터치
mysql
아이팟터치
아이폰
지름신
영화
iPhone
삼가고인의명복을빕니다
Navigator
잡스
넷스케이프
개발툴
트랜스포머 달팽이
무방비도시
맥클레인
쉘스크립트
하정우
민주노동당
임원희
바르게살자
권영길
Feisty Fawn
히치콕
동기화
ODD
RECENT COMMENT
조중동 나빠요...ㅜ.ㅜ 초..
[비밀댓글]
네... 앞으로 노무현같은 대통..
저도 가끔 그런 생각을 해봅니..
에고 죄송하네요... 댓글을 넘..
RECENT TRACKBACK
반도체맨의 느낌
[Team_WAF] No Country for Ol..
Aptana Studio 1.0 정식판 출시!
넷스케이프가 살아있었구나...
새아이팟, 터치팟의 의문점
MY LINK
COUNTER
TODAY
1
| YESTERDAY
8
Tistory 회원가입!