7

xml 파싱 예제

· 2012-12-28 (금) 14:26:26 · 6296 · 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-01-18 조회 3,972
13-01-18 조회 3,855
13-01-18 조회 5,049
13-01-17 조회 3,704
13-01-17 조회 5,344
13-01-17 조회 4,508
13-01-17 조회 4,136
13-01-17 조회 5,261
13-01-17 조회 4,992
13-01-17 조회 4,441
13-01-17 조회 3,818
13-01-17 조회 4,394
13-01-17 조회 3,861
13-01-17 조회 4,200
13-01-17 조회 7,156
13-01-16 조회 7,255
13-01-16 조회 4,437
13-01-15 조회 1만
13-01-15 조회 1.1만
13-01-15 조회 5,247
13-01-15 조회 6,918
13-01-15 조회 3,642
13-01-14 조회 6,591
13-01-14 조회 6,107
13-01-14 조회 5,858
13-01-14 조회 5,828
13-01-14 조회 5,479
13-01-14 조회 5,040
13-01-14 조회 8,345
13-01-14 조회 5,877
13-01-14 조회 5,919
13-01-14 조회 5,362
13-01-14 조회 1만
13-01-14 조회 5,414
13-01-14 조회 6,563
13-01-14 조회 5,024
13-01-14 조회 7,662
13-01-14 조회 6,816
13-01-14 조회 5,550
13-01-14 조회 5,316
13-01-11 조회 4,355
13-01-11 조회 6,433
13-01-11 조회 7,134
13-01-11 조회 6,376
13-01-11 조회 8,566
13-01-10 조회 4,564
13-01-09 조회 3,811
13-01-09 조회 8,137
13-01-09 조회 3,807
13-01-09 조회 6,397
13-01-09 조회 6,413
13-01-09 조회 6,029
13-01-09 조회 6,063
13-01-09 조회 5,437
13-01-09 조회 5,424
13-01-09 조회 5,904
13-01-09 조회 5,453
13-01-09 조회 5,445
13-01-09 조회 5,778
13-01-09 조회 5,837
13-01-08 조회 4,448
13-01-08 조회 3,305
13-01-08 조회 3,743
13-01-08 조회 3,444
13-01-08 조회 5,801
13-01-08 조회 5,567
13-01-07 조회 5,039
13-01-07 조회 3,616
13-01-07 조회 3,786
13-01-07 조회 5,503
13-01-07 조회 6,299
13-01-04 조회 4,259
13-01-04 조회 1.3만
13-01-04 조회 3,780
13-01-04 조회 3,352
13-01-04 조회 4,159
13-01-04 조회 6,137
13-01-03 조회 1만
13-01-03 조회 1.2만
13-01-03 조회 5,923
13-01-02 조회 6,108
13-01-02 조회 9,340
13-01-01 조회 1.3만
13-01-01 조회 7,268
13-01-01 조회 3,563
12-12-31 조회 3,478
12-12-31 조회 5,162
12-12-31 조회 3,835
12-12-31 조회 6,523
12-12-30 조회 3,362
12-12-30 조회 6,185
12-12-30 조회 6,694
12-12-29 조회 4,710
12-12-28 조회 5,125
12-12-28 조회 6,297
12-12-28 조회 3,615
12-12-28 조회 6,085
12-12-28 조회 8,433
12-12-27 조회 4,658
12-12-27 조회 4,362