7

xml 파싱 예제

· 2012-12-28 (금) 14:26:26 · 6208 · 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건
+
제목 글쓴이 날짜 조회
16-06-03 조회 3,029
15-07-30 조회 3,419
15-06-08 조회 5,559
15-05-27 조회 4,416
15-01-08 조회 6,717
14-12-30 조회 5,448
14-10-10 조회 5,440
14-08-13 조회 6,356
14-08-11 조회 5,208
14-08-06 조회 6,548
14-03-19 조회 7,439
13-04-26 조회 9,596
12-12-15 조회 5,118
12-12-15 조회 7,352
12-12-11 조회 5,494
12-11-21 조회 6,026
12-10-11 조회 5,105
12-10-03 조회 4,798
12-09-20 조회 6,095
12-09-02 조회 4,273
12-08-29 조회 5,284
12-08-29 조회 4,163
12-08-24 조회 4,265
12-08-18 조회 4,482
12-08-17 조회 4,528
12-08-16 조회 3,783
12-08-06 조회 5,559
12-08-05 조회 4,846
12-08-03 조회 4,563
12-08-03 조회 4,711
12-07-29 조회 3,562
12-07-24 조회 4,153
12-07-22 조회 3,616
12-07-20 조회 4,377
12-07-19 조회 4,720
12-07-19 조회 3,950
12-07-19 조회 3,897
12-07-19 조회 5,563
12-07-19 조회 3,506
12-07-18 조회 5,193
12-07-15 조회 4,175
12-07-14 조회 3,843
12-07-12 조회 4,515
12-07-11 조회 3,789
12-07-11 조회 3,511
12-07-11 조회 3,488
12-07-10 조회 4,674
12-07-09 조회 3,422
12-07-09 조회 4,239
12-07-09 조회 4,658
12-07-08 조회 4,574
12-07-07 조회 5,495
12-07-06 조회 4,751
12-07-06 조회 4,016
12-07-06 조회 3,903
12-07-06 조회 3,842
12-07-02 조회 5,519
12-07-02 조회 4,217
12-06-29 조회 3,740
12-06-28 조회 4,364
12-06-26 조회 3,859
12-06-26 조회 4,977
12-06-21 조회 3,956
12-06-20 조회 4,657
12-06-20 조회 4,295
12-06-15 조회 4,649
12-06-15 조회 4,382
12-06-13 조회 7,399
12-06-05 조회 7,374
12-06-04 조회 3,949
12-06-03 조회 3,801
12-05-31 조회 3,793
12-05-28 조회 6,642
12-05-27 조회 3,875
12-05-25 조회 4,009
12-05-23 조회 4,416
12-05-21 조회 5,129
12-05-17 조회 4,064
12-05-17 조회 3,978
12-05-09 조회 4,058
12-05-07 조회 4,316
12-05-02 조회 4,502
12-05-01 조회 4,678
12-04-30 조회 3,426
12-04-30 조회 4,372
12-04-25 조회 4,458
12-04-25 조회 4,882
12-04-24 조회 3,846
12-04-24 조회 4,618
12-04-23 조회 3,611
12-04-20 조회 3,269
12-04-17 조회 5,452
12-04-16 조회 4,388
12-04-14 조회 3,672
12-04-14 조회 3,701
12-04-14 조회 3,748
12-04-13 조회 3,375
12-04-13 조회 4,560
12-04-13 조회 6,203
12-04-12 조회 3,368