7

xml 파싱 예제

· 2012-12-28 (금) 14:26:26 · 6189 · 1

<?xml version="1.0" encoding="utf-8"?>
<CATEGORIES type="day">
<CATEGORY label="22" data="20101022"/>
<CATEGORY label="23" data="20101023"/>
</CATEGORIES>

// XML 파일 열고 루트 테그 가져오기
$dom = DOMDocument::load($filepath);
$temp = $dom->getElementsByTagName('CATEGORIES');
$root = $temp->item(0);

// 노드 추가 하기 위해 추가할 위치 검색
// 여기서는 날짜를 기준으로 정렬하는 부분인데, 현재 입력된 날짜가 크면 다음으로, 작으면 그 노드의 앞을 지정
$childs = $root->getElementsByTagName('CATEGORY');
for ($L1 = 0, $C1 = $childs->length; $L1 < $C1; $L1++)
{
$child = $childs->item($L1);
$label = $child->getAttribute('label');

if ($data == $label) { $isFound = -1; break; }
elseif ($data < $label) { $isFound = 1; break; }
}

// 신규 XML 파일 생성
// <?xml version="1.0" encoding="utf-8"?><CATEGORIES type="day" />
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('CATEGORIES');
$root->setAttribute('type', 'day');
$dom->appendChild($root);

// 신규 노드 생성
//<CATEGORY label="22" data="20101022"/>
$node = $dom->createElement('CATEGORY');
$node->setAttribute('label', '22');
$node->setAttribute('data', '20101022');

// 추가할 위치가 있는 경우 그 앞에 추가하고, 없다면 맨 뒤의 노드에 추가
if ($isFound == 1) { $root->insertBefore($node, $child); }
elseif ($isFound == 0) { $root->appendChild($node); }

// 저장
//echo htmlspecialchars($dom->saveXML());
createDirectory($filepath);
$dom->save($filepath);
chmod($filepath, 0646);

|

댓글 1개

좋은 자료네요
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,188건
+
제목 글쓴이 날짜 조회
15-12-13 조회 2,531
15-12-13 조회 2,946
15-12-11 조회 2,607
15-12-11 조회 2,568
15-12-11 조회 2,571
15-12-10 조회 2,529
15-12-10 조회 2,588
15-12-10 조회 2,562
15-12-03 조회 3,204
15-12-03 조회 2,718
15-11-25 조회 2,801
15-11-09 조회 3,211
15-11-04 조회 2,971
15-06-09 조회 4,097
15-05-06 조회 5,094
15-03-28 조회 4,577
14-12-05 조회 5,003
14-10-25 조회 5,099
14-10-13 조회 4,275
14-10-03 조회 6,263
14-09-19 조회 4,968
14-09-18 조회 4,053
14-09-13 조회 4,616
14-09-03 조회 5,606
14-08-26 조회 4,211
14-08-26 조회 4,016
14-08-26 조회 3,980
14-08-11 조회 6,510
14-08-06 조회 4,127
14-08-06 조회 3,726
14-08-06 조회 3,941
14-08-01 조회 6,307
14-08-01 조회 3,847
14-07-30 조회 4,447
14-06-28 조회 5,167
14-06-28 조회 4,440
14-05-11 조회 4,592
14-05-09 조회 4,951
14-05-09 조회 7,713
14-04-30 조회 4,338
14-04-27 조회 4,197
14-04-25 조회 4,900
14-04-21 조회 3,878
14-04-21 조회 5,006
14-04-10 조회 4,607
14-04-01 조회 6,161
14-03-03 조회 3,533
14-02-25 조회 5,923
14-02-14 조회 5,196
14-02-13 조회 4,955
14-01-03 조회 4,448
13-12-06 조회 3,768
13-11-22 조회 5,523
13-11-18 조회 4,030
13-10-02 조회 4,448
13-10-02 조회 3,699
13-10-02 조회 3,962
13-10-02 조회 3,818
13-10-02 조회 4,185
13-09-16 조회 8,033
13-09-16 조회 5,398
13-08-18 조회 5,431
13-07-11 조회 4,112
13-07-10 조회 4,696
13-07-09 조회 4,733
13-07-09 조회 4,988
13-07-04 조회 4,489
13-07-04 조회 4,127
13-07-04 조회 4,240
13-07-01 조회 4,727
13-07-01 조회 4,560
13-07-01 조회 6,836
13-06-23 조회 4,386
13-06-05 조회 3,632
13-05-29 조회 4,874
13-05-10 조회 4,281
13-04-05 조회 4,900
13-04-05 조회 6,586
13-04-05 조회 1.1만
13-04-05 조회 7,182
13-04-05 조회 6,570
13-03-31 조회 5,847
13-03-31 조회 4,170
13-03-28 조회 4,531
13-03-28 조회 6,728
13-03-28 조회 9,212
13-03-28 조회 4,343
13-03-24 조회 3,958
13-03-21 조회 4,637
13-03-21 조회 1.8만
13-03-14 조회 3,942
13-03-10 조회 4,753
13-03-10 조회 4,512
13-03-10 조회 4,213
13-03-08 조회 5,270
13-03-08 조회 4,374
13-03-03 조회 5,030
13-03-03 조회 7,485
13-03-03 조회 4,227
13-03-03 조회 1.1만