7

xml 파싱 예제

· 2012-12-28 (금) 14:26:26 · 6277 · 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,187건
+
제목 글쓴이 날짜 조회
13-02-27 조회 4,169
13-02-27 조회 3,521
13-02-27 조회 6,825
13-02-27 조회 4,219
13-02-26 조회 1.2만
13-02-26 조회 3,714
13-02-26 조회 3,988
13-02-26 조회 6,352
13-02-26 조회 3,941
13-02-26 조회 4,836
13-02-26 조회 6,609
13-02-26 조회 4,107
13-02-25 조회 4,034
13-02-24 조회 8,218
13-02-24 조회 4,321
13-02-24 조회 3,563
13-02-24 조회 5,366
13-02-24 조회 5,351
13-02-24 조회 4,420
13-02-24 조회 5,753
13-02-24 조회 9,450
13-02-24 조회 5,080
13-02-22 조회 3,603
13-02-21 조회 4,745
13-02-21 조회 4,878
13-02-20 조회 5,580
13-02-19 조회 3,449
13-02-19 조회 4,583
13-02-19 조회 3,294
13-02-19 조회 4,924
13-02-18 조회 5,595
13-02-18 조회 3,742
13-02-16 조회 7,006
13-02-15 조회 1.3만
13-02-15 조회 4,299
13-02-15 조회 5,578
13-02-14 조회 5,198
13-02-14 조회 5,674
13-02-13 조회 9,135
13-02-12 조회 5,404
13-02-12 조회 6,078
13-02-12 조회 4,074
13-02-07 조회 3,660
13-02-06 조회 4,723
13-02-05 조회 9,045
13-02-05 조회 4,310
13-02-05 조회 7,736
13-02-04 조회 3,578
13-02-04 조회 7,313
13-02-04 조회 4,254
13-02-03 조회 4,099
13-02-02 조회 4,111
13-02-02 조회 4,213
13-02-01 조회 3,426
13-02-01 조회 3,395
13-02-01 조회 2.4만
13-01-31 조회 3,333
13-01-31 조회 4,073
13-01-30 조회 4,035
13-01-30 조회 4,302
13-01-30 조회 6,448
13-01-30 조회 3,574
13-01-30 조회 5,138
13-01-29 조회 7,751
13-01-28 조회 3,571
13-01-28 조회 6,879
13-01-28 조회 4,104
13-01-28 조회 6,842
13-01-28 조회 3,882
13-01-25 조회 4,951
13-01-25 조회 3,895
13-01-25 조회 5,194
13-01-25 조회 1.1만
13-01-24 조회 4,969
13-01-24 조회 4,847
13-01-24 조회 4,500
13-01-24 조회 3,450
13-01-24 조회 5,093
13-01-24 조회 4,976
13-01-23 조회 3,446
13-01-23 조회 4,123
13-01-23 조회 3,502
13-01-23 조회 3,636
13-01-23 조회 4,253
13-01-22 조회 6,786
13-01-21 조회 5,210
13-01-20 조회 5,287
13-01-20 조회 6,909
13-01-20 조회 6,636
13-01-19 조회 6,350
13-01-19 조회 7,206
13-01-18 조회 3,950
13-01-18 조회 8,976
13-01-18 조회 4,133
13-01-18 조회 4,034
13-01-18 조회 6,253
13-01-18 조회 5,931
13-01-18 조회 8,484
13-01-18 조회 5,813
13-01-18 조회 6,307