7

xml 파싱 예제

· 13년 전 · 4372 · 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,762
13년 전 조회 2,234
13년 전 조회 6,277
13년 전 조회 2,021
13년 전 조회 4,494
13년 전 조회 4,502
13년 전 조회 4,114
13년 전 조회 4,324
13년 전 조회 3,842
13년 전 조회 3,777
13년 전 조회 3,969
13년 전 조회 3,833
13년 전 조회 3,845
13년 전 조회 3,898
13년 전 조회 3,921
13년 전 조회 2,591
13년 전 조회 1,835
13년 전 조회 1,921
13년 전 조회 1,673
13년 전 조회 3,915
13년 전 조회 3,874
13년 전 조회 3,193
13년 전 조회 2,252
13년 전 조회 1,919
13년 전 조회 3,826
13년 전 조회 4,293
13년 전 조회 2,341
13년 전 조회 1.2만
13년 전 조회 2,342
13년 전 조회 1,517
13년 전 조회 2,453
13년 전 조회 4,190
13년 전 조회 8,243
13년 전 조회 1만
13년 전 조회 3,963
13년 전 조회 4,087
13년 전 조회 7,651
13년 전 조회 1.1만
13년 전 조회 5,161
13년 전 조회 1,702
13년 전 조회 1,900
13년 전 조회 3,373
13년 전 조회 1,834
13년 전 조회 4,747
13년 전 조회 1,542
13년 전 조회 4,070
13년 전 조회 4,569
13년 전 조회 3,100
13년 전 조회 3,309
13년 전 조회 4,373
13년 전 조회 1,823
13년 전 조회 3,975
13년 전 조회 6,309
13년 전 조회 2,758
13년 전 조회 2,447
13년 전 조회 2,372
13년 전 조회 2,290
13년 전 조회 2,029
13년 전 조회 4,269
13년 전 조회 1,951
13년 전 조회 2,634
13년 전 조회 6,961
13년 전 조회 4,329
13년 전 조회 3,709
13년 전 조회 4,213
13년 전 조회 2,552
13년 전 조회 1,863
13년 전 조회 2,315
13년 전 조회 2,357
13년 전 조회 1,593
13년 전 조회 4,603
13년 전 조회 5,055
13년 전 조회 3,415
13년 전 조회 9,417
13년 전 조회 1,944
13년 전 조회 9,824
13년 전 조회 1.8만
13년 전 조회 2,346
13년 전 조회 1,821
13년 전 조회 7,359
13년 전 조회 1,843
13년 전 조회 2,007
13년 전 조회 1,769
13년 전 조회 1,918
13년 전 조회 2,229
13년 전 조회 2,090
13년 전 조회 2,165
13년 전 조회 4,747
13년 전 조회 3,210
13년 전 조회 5,490
13년 전 조회 4,163
13년 전 조회 5,352
13년 전 조회 3,168
13년 전 조회 2,392
13년 전 조회 2,072
13년 전 조회 3,175
13년 전 조회 6,096
13년 전 조회 2,294
13년 전 조회 5,156
13년 전 조회 6,678