7

xml 파싱 예제

· 13년 전 · 4350 · 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개

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

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 2,745
13년 전 조회 2,154
13년 전 조회 6,262
13년 전 조회 2,006
13년 전 조회 4,478
13년 전 조회 4,482
13년 전 조회 4,105
13년 전 조회 4,264
13년 전 조회 3,774
13년 전 조회 3,719
13년 전 조회 3,952
13년 전 조회 3,754
13년 전 조회 3,777
13년 전 조회 3,881
13년 전 조회 3,908
13년 전 조회 2,575
13년 전 조회 1,780
13년 전 조회 1,908
13년 전 조회 1,658
13년 전 조회 3,901
13년 전 조회 3,813
13년 전 조회 3,173
13년 전 조회 2,202
13년 전 조회 1,897
13년 전 조회 3,742
13년 전 조회 4,273
13년 전 조회 2,322
13년 전 조회 1.2만
13년 전 조회 2,271
13년 전 조회 1,494
13년 전 조회 2,435
13년 전 조회 4,165
13년 전 조회 8,214
13년 전 조회 1만
13년 전 조회 3,944
13년 전 조회 4,072
13년 전 조회 7,597
13년 전 조회 1.1만
13년 전 조회 5,136
13년 전 조회 1,688
13년 전 조회 1,831
13년 전 조회 3,297
13년 전 조회 1,817
13년 전 조회 4,685
13년 전 조회 1,520
13년 전 조회 4,053
13년 전 조회 4,552
13년 전 조회 3,031
13년 전 조회 3,283
13년 전 조회 4,351
13년 전 조회 1,806
13년 전 조회 3,949
13년 전 조회 6,295
13년 전 조회 2,737
13년 전 조회 2,433
13년 전 조회 2,354
13년 전 조회 2,269
13년 전 조회 2,004
13년 전 조회 4,242
13년 전 조회 1,925
13년 전 조회 2,607
13년 전 조회 6,943
13년 전 조회 4,307
13년 전 조회 3,691
13년 전 조회 4,188
13년 전 조회 2,519
13년 전 조회 1,834
13년 전 조회 2,288
13년 전 조회 2,335
13년 전 조회 1,529
13년 전 조회 4,543
13년 전 조회 5,036
13년 전 조회 3,388
13년 전 조회 9,393
13년 전 조회 1,870
13년 전 조회 9,796
13년 전 조회 1.8만
13년 전 조회 2,324
13년 전 조회 1,804
13년 전 조회 7,335
13년 전 조회 1,823
13년 전 조회 1,991
13년 전 조회 1,741
13년 전 조회 1,890
13년 전 조회 2,199
13년 전 조회 2,073
13년 전 조회 2,142
13년 전 조회 4,728
13년 전 조회 3,196
13년 전 조회 5,475
13년 전 조회 4,139
13년 전 조회 5,329
13년 전 조회 3,157
13년 전 조회 2,374
13년 전 조회 2,055
13년 전 조회 3,163
13년 전 조회 6,073
13년 전 조회 2,238
13년 전 조회 5,102
13년 전 조회 6,627