7

xml 파싱 예제

· 13년 전 · 3450 · 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년 전 조회 1,835
13년 전 조회 1,452
13년 전 조회 5,346
13년 전 조회 1,113
13년 전 조회 3,588
13년 전 조회 3,598
13년 전 조회 3,187
13년 전 조회 3,538
13년 전 조회 3,051
13년 전 조회 3,023
13년 전 조회 3,048
13년 전 조회 3,050
13년 전 조회 3,070
13년 전 조회 3,015
13년 전 조회 3,037
13년 전 조회 1,673
13년 전 조회 1,081
13년 전 조회 989
13년 전 조회 770
13년 전 조회 3,017
13년 전 조회 3,139
13년 전 조회 2,285
13년 전 조회 1,517
13년 전 조회 962
13년 전 조회 3,047
13년 전 조회 3,396
13년 전 조회 1,417
13년 전 조회 1.1만
13년 전 조회 1,610
13년 전 조회 619
13년 전 조회 1,556
13년 전 조회 3,294
13년 전 조회 7,320
13년 전 조회 9,160
13년 전 조회 3,064
13년 전 조회 3,188
13년 전 조회 6,865
13년 전 조회 1.1만
13년 전 조회 4,232
13년 전 조회 768
13년 전 조회 1,078
13년 전 조회 2,551
13년 전 조회 875
13년 전 조회 3,913
13년 전 조회 615
13년 전 조회 3,133
13년 전 조회 3,632
13년 전 조회 2,286
13년 전 조회 2,364
13년 전 조회 3,451
13년 전 조회 866
13년 전 조회 3,093
13년 전 조회 5,395
13년 전 조회 1,823
13년 전 조회 1,536
13년 전 조회 1,404
13년 전 조회 1,321
13년 전 조회 1,104
13년 전 조회 3,325
13년 전 조회 984
13년 전 조회 1,695
13년 전 조회 6,009
13년 전 조회 3,390
13년 전 조회 2,789
13년 전 조회 3,284
13년 전 조회 1,655
13년 전 조회 984
13년 전 조회 1,391
13년 전 조회 1,457
13년 전 조회 793
13년 전 조회 3,734
13년 전 조회 4,144
13년 전 조회 2,474
13년 전 조회 8,474
13년 전 조회 1,147
13년 전 조회 8,918
13년 전 조회 1.7만
13년 전 조회 1,419
13년 전 조회 879
13년 전 조회 6,416
13년 전 조회 884
13년 전 조회 1,080
13년 전 조회 831
13년 전 조회 967
13년 전 조회 1,314
13년 전 조회 1,175
13년 전 조회 1,262
13년 전 조회 3,843
13년 전 조회 2,315
13년 전 조회 4,579
13년 전 조회 3,259
13년 전 조회 4,436
13년 전 조회 2,260
13년 전 조회 1,452
13년 전 조회 1,125
13년 전 조회 2,247
13년 전 조회 5,180
13년 전 조회 1,453
13년 전 조회 4,296
13년 전 조회 5,824
🐛 버그신고