<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>델리 키포스 &#187; xml</title>
	<atom:link href="http://kiipos.delimount.net/tag/xml/feed" rel="self" type="application/rss+xml" />
	<link>http://kiipos.delimount.net</link>
	<description>춤을 추는 거야. 음악이 계속되는 한.</description>
	<lastBuildDate>Fri, 07 Oct 2011 15:56:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>iPhone, XML 처리 예제 &#8211; NSXMLParser 이용하기</title>
		<link>http://kiipos.delimount.net/1084</link>
		<comments>http://kiipos.delimount.net/1084#comments</comments>
		<pubDate>Wed, 10 Mar 2010 04:14:55 +0000</pubDate>
		<dc:creator>키포스</dc:creator>
				<category><![CDATA[미분류]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[아이폰]]></category>

		<guid isPermaLink="false">http://kiipos.delimount.net/?p=1084</guid>
		<description><![CDATA[몇일 전 iPhone에서 HTTP로 XML 데이터 요청하는 예제를 올렸다가 iPhone SDK에서 NSXMLDocument 클래스를 지원하지 않는 다는 것을 확인하고 NSXMLParser 클래스를 이용해서 XML 데이터를 처리하는 예제를 다시 준비했습니다. 아마도 iPhone에서 NSXMLDocument를 지원하지 않는 건 NSXMLDocument가 메모리를 많이 먹기 때문인 아닐까 싶습니다.
NSXMLParser는 XML 문서를 순차적으로 분석(parse)하면서 이벤트별 처리를 위임(delegate)하는 방식입니다. 따라서 NSXMLDocument를 사용하는 예제보다는 훨씬 더 복잡합니다. [...]]]></description>
			<content:encoded><![CDATA[<p>몇일 전 <a href="http://kiipos.delimount.net/980">iPhone에서 HTTP로 XML 데이터 요청하는 예제</a>를 올렸다가 iPhone SDK에서 NSXMLDocument 클래스를 지원하지 않는 다는 것을 확인하고 NSXMLParser 클래스를 이용해서 XML 데이터를 처리하는 예제를 다시 준비했습니다. 아마도 iPhone에서 NSXMLDocument를 지원하지 않는 건 NSXMLDocument가 메모리를 많이 먹기 때문인 아닐까 싶습니다.</p>
<p><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html">NSXMLParser</a>는 XML 문서를 순차적으로 분석(parse)하면서 이벤트별 처리를 위임(delegate)하는 방식입니다. 따라서 NSXMLDocument를 사용하는 예제보다는 훨씬 더 복잡합니다. 참고로 iPhone에서 사용할 수 있는 <a href="http://note.marines.co.kr/124">XML 관련 라이브러리</a>들이 개발되어 있기 때문에 해당 라이브러리를 익혀서 사용하는 것이 개발 효율은 더 좋을 것 같습니다. 하지만 NSXMLParser를 이용해서 구현하면 메모리도 적게 먹고 나름 성능 향상을 기대할 수 기대할 수 있기 때문에 직접 작성하는 것도 알아두면 좋을 듯 합니다.</p>
<p>예제에 포함된 파일 목록은 아래와 같습니다.</p>
<table>
<tr>
<th>파일</th>
<th>설명</th>
</tr>
<tr>
<td>showrooms.xml</td>
<td>XML 파일</td>
</tr>
<tr>
<td>Dealer.h <br /> Dealer.m</td>
<td>Dealer 모델 클래스</td>
</tr>
<tr>
<td>Showroom.h <br /> Showroom.m</td>
<td>Showroom 모델 클래스</td>
</tr>
<tr>
<td>ShowroomsXmlParse.h <br /> ShowroomsXmlParse.m</td>
<td>showrooms.xml용 파서 클래스</td>
</tr>
<tr>
<td>showrooms_xml_parser.m</td>
<td>실행(main) 소스</td>
</tr>
</table>
<p>우선 showrooms.xml은 아래와 같습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;showrooms<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>강남전시장<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>02-111-1111<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dealer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>김태연<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dealer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>삼성전시장<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>02-222-2222<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phone<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dealer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>구하라<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dealer<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/showrooms<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>예제에서 showroom 및 dealer 엘리먼트가 각각 name 엘리먼트를 자식으로 가지도록 작성했습니다.</p>
<p>Dealer.h와 Dealer.m은 다음과 같습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> Dealer <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> _name;
<span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">@property</span><span style="color: #002200;">&#40;</span> copy <span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> name;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Dealer.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> Dealer
  <span style="color: #a61390;">@synthesize</span> name <span style="color: #002200;">=</span> _name;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>Showroom.h와 Showroom.m은 다음과 같습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Dealer.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> Showroom <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> _name;
  <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> _phone;
  Dealer<span style="color: #002200;">*</span> _dealer;
<span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">@property</span><span style="color: #002200;">&#40;</span> copy <span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> name;
  <span style="color: #a61390;">@property</span><span style="color: #002200;">&#40;</span> copy <span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> phone;
  <span style="color: #a61390;">@property</span><span style="color: #002200;">&#40;</span> retain <span style="color: #002200;">&#41;</span> Dealer<span style="color: #002200;">*</span> dealer;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Showroom.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> Showroom
  <span style="color: #a61390;">@synthesize</span> name <span style="color: #002200;">=</span> _name;
  <span style="color: #a61390;">@synthesize</span> phone <span style="color: #002200;">=</span> _phone;
  <span style="color: #a61390;">@synthesize</span> dealer <span style="color: #002200;">=</span> _dealer;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>NSXMLParser는 NSXMLDocument의 NSXMLNode처럼 모델이 없기 때문에 분석(parse) 결과를 담을 수 있는 모델이 필요합니다. 이 모델 클래스들은 MVC에도 적용될 수 있기 때문에 모델 클래스를 만들어 두면 조금 더 우아한 프로그래밍할 수 있습니다. 모델 클래스들은 간단하기 때문에 자세한 설명은 생략하도록 하겠습니다. </p>
<p>ShowroomsXmlParser.h는 아래와 같습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Showroom.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> ShowroomsXmlParser <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> _showrooms;
  <span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> _elementStack;
<span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> parser<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> parser 
      didStartElement<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> elementName 
      namespaceURI<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> namespaceURI 
      qualifiedName<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> qualifiedName 
      attributes<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> attributeDict;
  <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> parser<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> parser
      foundCharacters<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">string</span>;
  <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> parser<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> parser 
      didEndElement<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> elementName
      namespaceURI<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> namespaceURI 
      qualifiedName<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> qName;
  <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> parseContentOfUrl<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURL</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> url;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>ShowroomsXmlParser는 NSXMLParser가 발생시키는 event를 처리해서 Showroom 객체 배열을 넘겨주는 클래스입니다. NSXMLParser가 위임(delegate)하는 method들을 처리하기 위해서 <del datetime="2010-03-15T11:50:24+00:00"><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/NSXMLParserDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/NSXMLParserDelegate">NSXMLParserDelegate</a> 프로토콜을 구현해야 합니다</del>. 참고로 프로토콜은 자바에서 interface랑 동일합니다. 멤버 변수 _elementStack은 파서의 트리 탐색 경로 및 정보를 담아두는 스택입니다.</p>
<p>ShowroomsXmlParse.m은 아래와 같습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;ShowroomsXmlParser.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> <span style="color: #a61390;">const</span> SHOWROOM <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;showroom&quot;</span>;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> <span style="color: #a61390;">const</span> DEALER <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;dealer&quot;</span>;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> <span style="color: #a61390;">const</span> NAME <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name&quot;</span>;
<span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> <span style="color: #a61390;">const</span> PHONE <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;phone&quot;</span>;
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> ShowroomsXmlParser
  <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> parser<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> parser 
      didStartElement<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> elementName 
      namespaceURI<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> namespaceURI 
      qualifiedName<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> qualifiedName 
      attributes<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> attributeDict
  <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> elementName isEqualToString<span style="color: #002200;">:</span> SHOWROOM <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      Showroom<span style="color: #002200;">*</span> showroom <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> Showroom new <span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#91;</span> _elementStack addObject<span style="color: #002200;">:</span> showroom <span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> elementName isEqualToString<span style="color: #002200;">:</span> DEALER <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      Dealer<span style="color: #002200;">*</span> dealer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> Dealer new <span style="color: #002200;">&#93;</span>;
      Showroom<span style="color: #002200;">*</span> showroom <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Showroom<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span> _elementStack lastObject <span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#91;</span> showroom setDealer<span style="color: #002200;">:</span> dealer <span style="color: #002200;">&#93;</span>;
&nbsp;
      <span style="color: #002200;">&#91;</span> _elementStack addObject<span style="color: #002200;">:</span> dealer <span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> elementName isEqualToString<span style="color: #002200;">:</span> NAME <span style="color: #002200;">&#93;</span> ||
      TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> elementName isEqualToString<span style="color: #002200;">:</span> PHONE <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> element <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span> elementName <span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#91;</span> _elementStack addObject<span style="color: #002200;">:</span> element <span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> parser<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> parser
      foundCharacters<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #a61390;">string</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSCharacterSet</span><span style="color: #002200;">*</span> characterSet <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSCharacterSet</span> 
      whitespaceAndNewlineCharacterSet <span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSString</span><span style="color: #002200;">*</span> trimmedValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #a61390;">string</span> stringByTrimmingCharactersInSet<span style="color: #002200;">:</span>
      characterSet <span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> trimmedValue length <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      <span style="color: #a61390;">return</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">id</span> element <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> _elementStack lastObject <span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> element isKindOfClass<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSString</span> class <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      NSUInteger count <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> _elementStack count <span style="color: #002200;">&#93;</span>;
      NSUInteger index <span style="color: #002200;">=</span> count <span style="color: #002200;">-</span> <span style="color: #2400d9;">2</span>;
      <span style="color: #a61390;">id</span> parentElement <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> _elementStack objectAtIndex<span style="color: #002200;">:</span> index <span style="color: #002200;">&#93;</span>;
&nbsp;
      <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> element isEqualToString<span style="color: #002200;">:</span> PHONE <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
      <span style="color: #002200;">&#123;</span>
        Showroom<span style="color: #002200;">*</span> showroom <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Showroom<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parentElement;
        <span style="color: #002200;">&#91;</span> showroom setPhone<span style="color: #002200;">:</span> trimmedValue <span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#125;</span>
      <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> element isEqualToString<span style="color: #002200;">:</span> NAME <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
      <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> parentElement isMemberOfClass<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span> Showroom class <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
          Showroom<span style="color: #002200;">*</span> showroom <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Showroom<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parentElement;
          <span style="color: #002200;">&#91;</span> showroom setName<span style="color: #002200;">:</span> trimmedValue <span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> parentElement isMemberOfClass<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span> Dealer class <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
        <span style="color: #002200;">&#123;</span>
          Dealer<span style="color: #002200;">*</span> dealer <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Dealer<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>parentElement;
          <span style="color: #002200;">&#91;</span> dealer setName<span style="color: #002200;">:</span> trimmedValue <span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
      <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> parser<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSXMLParser</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> parser 
      didEndElement<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> elementName
      namespaceURI<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> namespaceURI 
      qualifiedName<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> qName
  <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> elementName isEqualToString<span style="color: #002200;">:</span> SHOWROOM <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      Showroom<span style="color: #002200;">*</span> showroom <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>Showroom<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span> _elementStack lastObject <span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#91;</span> _showrooms addObject<span style="color: #002200;">:</span> showroom <span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#91;</span> _elementStack removeLastObject <span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> elementName isEqualToString<span style="color: #002200;">:</span> DEALER <span style="color: #002200;">&#93;</span> ||
      TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> elementName isEqualToString<span style="color: #002200;">:</span> NAME <span style="color: #002200;">&#93;</span> ||
      TRUE <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> elementName isEqualToString<span style="color: #002200;">:</span> PHONE <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      <span style="color: #002200;">&#91;</span> _elementStack removeLastObject <span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> parseContentOfUrl<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURL</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> url
  <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSXMLParser</span><span style="color: #002200;">*</span> xmlParser <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSXMLParser</span> alloc <span style="color: #002200;">&#93;</span> 
      initWithContentsOfURL<span style="color: #002200;">:</span> url <span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span> xmlParser setDelegate<span style="color: #002200;">:</span> self <span style="color: #002200;">&#93;</span>;
&nbsp;
    _showrooms <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSMutableArray</span> arrayWithCapacity<span style="color: #002200;">:</span> <span style="color: #2400d9;">1024</span> <span style="color: #002200;">&#93;</span>;
    _elementStack <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSMutableArray</span> arrayWithCapacity<span style="color: #002200;">:</span> <span style="color: #2400d9;">1024</span> <span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span> xmlParser parse <span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> _showrooms;
  <span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>NSXMLParser가 동작하기 위해서는 최소한 아래의 메소드들이 구현되어야 합니다. </p>
<ul>
<li><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/NSXMLParserDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSXMLParserDelegate/parser:didStartElement:namespaceURI:qualifiedName:attributes:">parser:didStartElement:namespaceURI:qualifiedName:attributes:</a> (이하 didStart)</li>
<li><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/NSXMLParserDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSXMLParserDelegate/parser:foundCharacters:">parser:foundCharacters:</a> (이하 found)</li>
<li><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/NSXMLParserDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/NSXMLParserDelegate/parser:didEndElement:namespaceURI:qualifiedName:">parser:didEndElement:namespaceURI:qualifiedName:</a> (이하 didEnd)</li>
</ul>
<p>didStart는 파서가 엘리먼트의 시작 태그를 만났을 때 호출됩니다. 비단말(nonterminal) 노드인 경우 엘리먼트 해당하는 모델 객체를 생성한 후 스택에 넣습니다. 단말(terminal) 노드인 경우 엘리먼트 이름으로 NSString 객체를 만들 후 스택에 넣습니다.</p>
<p>found는 파서가 엘리먼트를 제외한 스트링을 만날을 때 호출됩니다. 인자로 넘어온 스트링의 앞뒤 공백을 제거합니다. 유효한 스트링(이하 속성값)인 경우 스택에서 마지막으로 추가된 NSString 객체를 제거합니다. 제거된 NSString 객체의 값에 따라 적절한 부모 모델 객체의 속성(property)을 속성값으로 설정합니다. 만약 엘리먼트 이름이 NAME인 경우 중복되기 때문에 부모 모델 객체 클래스에 확인합니다.</p>
<p>didEnd는 파서가 엘리먼트의 종료 태그를 만났을 때 호출됩니다. 스택에서 모델 객체를 제거하고 SHOWROOM인 경우 _showrooms 배열 객체에 해당 객체를 추가합니다.</p>
<p>끝으로 콘솔 화면에서 결과를 확인할 수 있는 showrooms_xml_parser.m 파일은 아래와 같습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;ShowroomsXmlParser.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">char</span><span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSAutoreleasePool</span><span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSAutoreleasePool</span> new <span style="color: #002200;">&#93;</span>;
&nbsp;
  ShowroomsXmlParser<span style="color: #002200;">*</span> xmlParser <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> ShowroomsXmlParser new <span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSURL</span><span style="color: #002200;">*</span> url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span> 
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.yourserver.com/showrooms.xml&quot;</span> <span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSMutableArray</span><span style="color: #002200;">*</span> showrooms <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> xmlParser parseContentOfUrl<span style="color: #002200;">:</span> url <span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> Showroom<span style="color: #002200;">*</span> showroom <span style="color: #a61390;">in</span> showrooms <span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    Dealer<span style="color: #002200;">*</span> dealer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> showroom dealer <span style="color: #002200;">&#93;</span>;
    NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ %@ (%@)&quot;</span>, <span style="color: #002200;">&#91;</span> showroom name <span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span> showroom phone <span style="color: #002200;">&#93;</span>, 
      <span style="color: #002200;">&#91;</span> dealer name <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #002200;">&#91;</span> xmlParser release <span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span> url release <span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span> pool drain <span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>해당 소스들을 컴파일 하기 위해서는 아래의 명령어를 실행합니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> <span style="color: #660033;">-framework</span> Foundation Dealer.m Showroom.m ShowroomsXmlParser.m showrooms_xml_parser.m
<span style="color: #660033;">-o</span> showrooms_xml_parser</pre></td></tr></table></div>

<p>실행 결과 화면은 아래와 같습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>showrooms_xml_parser
강남전시장 02-<span style="color: #000000;">111</span>-<span style="color: #000000;">1111</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>김태연<span style="color: #7a0874; font-weight: bold;">&#41;</span>
삼성전시장 02-<span style="color: #000000;">222</span>-<span style="color: #000000;">2222</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>구하라<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></td></tr></table></div>

<p>프로그래밍에 도움되길 바랍니다~</p>
<h4>수정 사항</h4>
<p>엘리먼트 값이 없는 경우 에러가 발생하는 문제를 해결하기 위해서 showrooms_xml_parser.m의 found 메소드 안에 _elementStack의 마지막 요소 제거 코드를 didEnd로 옮겼습니다.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=http://bit.ly/92EjM3+iPhone%2C+XML+%EC%B2%98%EB%A6%AC+%EC%98%88%EC%A0%9C+%E2%80%93+NSXMLParser+%EC%9D%B4%EC%9A%A9%ED%95%98%EA%B8%B0+" title="Post to Twitter"><img class="nothumb" src="http://kiipos.delimount.net/wp-content/plugins/tweet-this/icons/tt-twitter-micro4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://kiipos.delimount.net/1084&amp;title=iPhone%2C+XML+%EC%B2%98%EB%A6%AC+%EC%98%88%EC%A0%9C+%E2%80%93+NSXMLParser+%EC%9D%B4%EC%9A%A9%ED%95%98%EA%B8%B0" title="Post to Delicious"><img class="nothumb" src="http://kiipos.delimount.net/wp-content/plugins/tweet-this/icons/tt-delicious-micro4.png" alt="Post to Delicious" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://kiipos.delimount.net/1084/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OS X, HTTP로 XML 데이터 요청 예제</title>
		<link>http://kiipos.delimount.net/980</link>
		<comments>http://kiipos.delimount.net/980#comments</comments>
		<pubDate>Tue, 02 Mar 2010 07:39:21 +0000</pubDate>
		<dc:creator>키포스</dc:creator>
				<category><![CDATA[미분류]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[아이폰]]></category>

		<guid isPermaLink="false">http://kiipos.delimount.net/?p=980</guid>
		<description><![CDATA[오래 간만에 포스팅합니다. 요즘 트위터 때문에 블로그에 소홀해진 것도 있지만 아이폰과 안드로이드 애플케이션(이하, 앱) 개발에 여념이 없습니다. 스마트폰 대세라 흐름을 따라 가는 것도 있지만 주로 웹 서비스만 개발하다 보니 오래 간만에 접하는 애플리케이션 형태의 제품 개발 자체가 매우 흥미롭습니다.
그런데 요즘 왠만한 앱에는 HTTP를 통해서 XML 데이터를 가져와서 보여주는 기능이 들어가는 데 막상 개발 참고하려고 구글 [...]]]></description>
			<content:encoded><![CDATA[<p>오래 간만에 포스팅합니다. 요즘 트위터 때문에 블로그에 소홀해진 것도 있지만 아이폰과 안드로이드 애플케이션(이하, 앱) 개발에 여념이 없습니다. 스마트폰 대세라 흐름을 따라 가는 것도 있지만 주로 웹 서비스만 개발하다 보니 오래 간만에 접하는 애플리케이션 형태의 제품 개발 자체가 매우 흥미롭습니다.</p>
<p>그런데 요즘 왠만한 앱에는 HTTP를 통해서 XML 데이터를 가져와서 보여주는 기능이 들어가는 데 막상 개발 참고하려고 구글 검색해보면 마땅한 예제가 없어서 간단하게 예제를 작성해보았습니다.</p>
<p>요청하는 XML 문서(hello.xml)는 아래와 같이 작성했습니다.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;showrooms<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>강남전시장<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;phone_number<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>02-111-1111<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phone_number<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>삼성전시장<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;phone_number<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>02-222-2222<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phone_number<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>한남전시장<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;phone_number<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>02-333-3333<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phone_number<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/showroom<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/showrooms<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>해당 XML 데이터를 HTTP를 통해서 가져와서 처리하는 예제는 아래와 같습니다. 간단하게 실행 되는 것을 확인하기 위해서 컨솔 창에서 돌아가도록 작성했습니다.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Foundation/Foundation.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">int</span> main<span style="color: #002200;">&#40;</span> <span style="color: #a61390;">int</span> argc, <span style="color: #a61390;">char</span><span style="color: #002200;">*</span> argv<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSAutoreleasePool</span><span style="color: #002200;">*</span> pool <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSAutoreleasePool</span> new <span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSURL</span><span style="color: #002200;">*</span> url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www,yourserver.com/hello.xml&quot;</span> <span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSError</span><span style="color: #002200;">*</span> error;
  <span style="color: #400080;">NSXMLDocument</span><span style="color: #002200;">*</span> xmlDocument <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> <span style="color: #002200;">&#91;</span> <span style="color: #400080;">NSXMLDocument</span> alloc <span style="color: #002200;">&#93;</span> 
    initWithContentsOfURL<span style="color: #002200;">:</span> url
    options<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span> NSXMLNodePreserveWhitespace | NSXMLNodePreserveCDATA <span style="color: #002200;">&#41;</span>
    error<span style="color: #002200;">:</span> <span style="color: #002200;">&amp;</span>error <span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">!=</span> error <span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, <span style="color: #002200;">&#91;</span> error localizedDescription <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span> pool drain <span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #400080;">NSXMLNode</span><span style="color: #002200;">*</span> rootElement <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> xmlDocument rootElement <span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> showrooms <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> rootElement children <span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">int</span> i, count <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> showrooms count <span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; count; <span style="color: #002200;">++</span>i <span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSXMLNode</span><span style="color: #002200;">*</span> showroomNode <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> showrooms objectAtIndex<span style="color: #002200;">:</span> i <span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSXMLNode</span><span style="color: #002200;">*</span> nameNode <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> showroomNode childAtIndex<span style="color: #002200;">:</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span><span style="color: #002200;">*</span> nodes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> showroomNode nodesForXPath<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;phone_number&quot;</span> error<span style="color: #002200;">:</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSXMLNode</span><span style="color: #002200;">*</span> phoneNumberNode <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #2400d9;">1</span> <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span> nodes count <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      phoneNumberNode <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span> nodes objectAtIndex<span style="color: #002200;">:</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    NSLog<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ (%@)&quot;</span>, <span style="color: #002200;">&#91;</span> nameNode stringValue <span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span> phoneNumberNode stringValue <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #002200;">&#91;</span> xmlDocument release <span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span> url release <span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span> pool drain <span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">return</span> <span style="color: #2400d9;">0</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>예제에서 특별히 어려운 내용은 없습니다. <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLDocument_Class/Reference/Reference.html">NSXMLDocument</a> 클래스가 HTTP를 지원하기 때문에 <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLDocument_Class/Reference/Reference.html#//apple_ref/occ/instm/NSXMLDocument/initWithContentsOfURL:options:error:">initWithContentsOfURL</a> 메소드를 호출하기만 하면 됩니다. 그리고 아이폰 런타임 환경에서는 가비지 컬렉션이 지원되지 않기 때문에 <del datetime="2010-03-10T04:19:17+00:00"><a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html">NSAutoreleasePool</a> 클래스를 이용해서 메모리 관리를 하였습니다.</del> </p>
<p>예제에서 3가지 다른 방법으로 자식 노드에 접근하고 있습니다. 각각 <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLNode_Class/Reference/Reference.html#//apple_ref/occ/instm/NSXMLNode/children">children</a>, <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLNode_Class/Reference/Reference.html#//apple_ref/occ/instm/NSXMLNode/childAtIndex:">objectAtIndex</a>, <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLNode_Class/Reference/Reference.html#//apple_ref/occ/instm/NSXMLNode/nodesForXPath:error:">nodesForXPath</a> 메소드를 이용합니다. 가장 일반적인 방법은 nodesForXPath을 이용하는 방법입니다. 약간의 성능 문제가 있을 수도 있지만 대부분의 경우 처리하는 XML 데이터의 량이 많이 않기 때문에 신경 쓰지 않아도 될 듯 합니다. 반면 objectAtIndex 메소드를 사용하는 경우 성능은 향상될 수 있겠지만 XML 문법에 종속적이기 때문에 그리 권장할만한 방법은 아닙니다.</p>
<p>해당 파일을 컴파일하는 쉘 구문은 아래와 같습니다.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> <span style="color: #660033;">-framework</span> Foundation hello.m <span style="color: #660033;">-o</span> hello</pre></div></div>

<p>실행 결과 화면은 아래와 같습니다.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>hello
강남전시장 <span style="color: #7a0874; font-weight: bold;">&#40;</span>02-<span style="color: #000000;">111</span>-<span style="color: #000000;">1111</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
삼성전시장 <span style="color: #7a0874; font-weight: bold;">&#40;</span>02-<span style="color: #000000;">222</span>-<span style="color: #000000;">2222</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
한남전시장 <span style="color: #7a0874; font-weight: bold;">&#40;</span>02-<span style="color: #000000;">333</span>-<span style="color: #000000;">3333</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>프로그래밍에 도움되길 바랍니다~</p>
<p>[ 수정 사항 ]<br />
조사해 본 결과 NSXMLDocument 클래스는 iPhone SDK에 제외되어 있습니다. 따라서 위 예제는 OS X 애플리케이션에서만 동작합니다. <del datetime="2010-03-10T04:19:17+00:00">빠른 시일 내에 iPhone에서 돌아가는 예제를 작성해서 올리도록 하겠습니다.</del> iPhone은 <a href="http://kiipos.delimount.net/1084">iPhone, HTTP로 XML 데이터 요청 및 처리 예제</a>를 참고하세요~</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=http://bit.ly/8Zygle+OS+X%2C+HTTP%EB%A1%9C+XML+%EB%8D%B0%EC%9D%B4%ED%84%B0+%EC%9A%94%EC%B2%AD+%EC%98%88%EC%A0%9C+" title="Post to Twitter"><img class="nothumb" src="http://kiipos.delimount.net/wp-content/plugins/tweet-this/icons/tt-twitter-micro4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://kiipos.delimount.net/980&amp;title=OS+X%2C+HTTP%EB%A1%9C+XML+%EB%8D%B0%EC%9D%B4%ED%84%B0+%EC%9A%94%EC%B2%AD+%EC%98%88%EC%A0%9C" title="Post to Delicious"><img class="nothumb" src="http://kiipos.delimount.net/wp-content/plugins/tweet-this/icons/tt-delicious-micro4.png" alt="Post to Delicious" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://kiipos.delimount.net/980/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

