더 나은 프로그래머가 되자

xml 파싱,simplexml_load_string,attributes 본문

언어/PHP

xml 파싱,simplexml_load_string,attributes

greathuman 2015. 4. 13. 10:42

 

xml을 파싱할때 simplexml_load_string 함수를 사용해서 각 엘리먼트의 값을 가져오거나

엘리먼트의 속성(attributes) 값을 가져올 수 있다.

 

$string = <<<XML
<a>
 <FOO game="lonely" name="one">1</FOO>
 </a>
XML;

$xml = simplexml_load_string($string);

foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

 

결과 값 : game="lonely" name="one"

$name = "name";
echo $xml->foo[0]->attributes()->$name;

결과 값 : one;

echo $xml->foo[0];

결과 값 : 1

snoopy 클래스를 이용해서 외부 페이지를 파싱해서 사용 할 수 있다.

require_once './snoopy/Snoopy.class.php';
$s=new snoopy;
$url="http://www.daum.net";
$s->fetch($url);
$data = $s->results;
$xml=simplexml_load_string(trim($data));

'언어 > PHP' 카테고리의 다른 글

브라우저 언어 체크 HTTP_ACCEPT_LANGUAGE  (0) 2015.04.16
올림,반올림,내림  (0) 2015.04.13
배열 정렬 sort,rsort,asort,arsort  (0) 2015.03.03
함수 유무 체크 function_exists()  (0) 2015.02.23
로딩화면만들기  (0) 2015.02.23
Comments