7

xml 파싱 예제

· 13년 전 · 4604 · 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년 전 조회 3,006
13년 전 조회 2,496
13년 전 조회 6,503
13년 전 조회 2,236
13년 전 조회 4,703
13년 전 조회 4,729
13년 전 조회 4,335
13년 전 조회 4,588
13년 전 조회 4,090
13년 전 조회 4,033
13년 전 조회 4,175
13년 전 조회 4,070
13년 전 조회 4,083
13년 전 조회 4,107
13년 전 조회 4,134
13년 전 조회 2,804
13년 전 조회 2,083
13년 전 조회 2,151
13년 전 조회 1,900
13년 전 조회 4,135
13년 전 조회 4,143
13년 전 조회 3,400
13년 전 조회 2,506
13년 전 조회 2,130
13년 전 조회 4,075
13년 전 조회 4,509
13년 전 조회 2,548
13년 전 조회 1.2만
13년 전 조회 2,638
13년 전 조회 1,752
13년 전 조회 2,658
13년 전 조회 4,405
13년 전 조회 8,458
13년 전 조회 1만
13년 전 조회 4,164
13년 전 조회 4,301
13년 전 조회 7,890
13년 전 조회 1.2만
13년 전 조회 5,339
13년 전 조회 1,899
13년 전 조회 2,163
13년 전 조회 3,643
13년 전 조회 2,033
13년 전 조회 4,968
13년 전 조회 1,762
13년 전 조회 4,280
13년 전 조회 4,765
13년 전 조회 3,344
13년 전 조회 3,531
13년 전 조회 4,605
13년 전 조회 2,031
13년 전 조회 4,205
13년 전 조회 6,566
13년 전 조회 3,015
13년 전 조회 2,679
13년 전 조회 2,597
13년 전 조회 2,516
13년 전 조회 2,260
13년 전 조회 4,518
13년 전 조회 2,161
13년 전 조회 2,833
13년 전 조회 7,187
13년 전 조회 4,563
13년 전 조회 3,947
13년 전 조회 4,431
13년 전 조회 2,799
13년 전 조회 2,089
13년 전 조회 2,532
13년 전 조회 2,585
13년 전 조회 1,860
13년 전 조회 4,822
13년 전 조회 5,270
13년 전 조회 3,613
13년 전 조회 9,602
13년 전 조회 2,180
13년 전 조회 1만
13년 전 조회 1.8만
13년 전 조회 2,576
13년 전 조회 2,048
13년 전 조회 7,573
13년 전 조회 2,056
13년 전 조회 2,245
13년 전 조회 2,014
13년 전 조회 2,131
13년 전 조회 2,473
13년 전 조회 2,287
13년 전 조회 2,393
13년 전 조회 4,968
13년 전 조회 3,420
13년 전 조회 5,687
13년 전 조회 4,376
13년 전 조회 5,564
13년 전 조회 3,366
13년 전 조회 2,605
13년 전 조회 2,273
13년 전 조회 3,393
13년 전 조회 6,320
13년 전 조회 2,530
13년 전 조회 5,390
13년 전 조회 6,936