PHP simplexml_load_string 에서 dc:creator 처리
페이지 정보
본문
PHP 에서 xml 을 파싱하는 기본도구로 simplexml_load_string 를 많이 사용한다.
그런데, 별도의 네임스페이스를 사용하는 경우가 있다.
이 경우 사용하는 방법이다.
## xml 데이타
[code]
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title></title>
<link></link>
<description></description>
<language>ko</language>
<pubDate></pubDate>
<dc:date></dc:date>
<dc:language>ko</dc:language>
<item>
<title></title>
<link></link>
<description></description>
<pubDate></pubDate>
<guid></guid>
<dc:creator></dc:creator>
<dc:date></dc:date>
</item>
</channel>
</rss>
[/code]
[code]
## 기본적으로 아래와 같이 불러올 수 있다.
$creator = (String)$xml->channel->item[0]->children(‘http://purl.org/dc/elements/1.1/’)->creator;
## 두번째 방법으로는, NS 를 정의한 후에 사용할 수 있다.
## 네임스페이스 정의
$ns = array
(
"content" => "http://purl.org/rss/1.0/modules/content/",
"wfw" => "http://wellformedweb.org/CommentAPI/",
"dc" => "http://purl.org/dc/elements/1.1/"
);
## 값 가져오기
$creator = (String)$xml->channel->item[0]->children($ns["dc"])->creator;
[/code]
* 참고
http://www.shop-wiz.com/board/view/root/php3/tid/204/category/0
http://blog.stuartherbert.com/php/2007/01/07/using-simplexml-to-parse-rss-feeds/
그런데, 별도의 네임스페이스를 사용하는 경우가 있다.
이 경우 사용하는 방법이다.
## xml 데이타
[code]
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
<channel>
<title></title>
<link></link>
<description></description>
<language>ko</language>
<pubDate></pubDate>
<dc:date></dc:date>
<dc:language>ko</dc:language>
<item>
<title></title>
<link></link>
<description></description>
<pubDate></pubDate>
<guid></guid>
<dc:creator></dc:creator>
<dc:date></dc:date>
</item>
</channel>
</rss>
[/code]
[code]
## 기본적으로 아래와 같이 불러올 수 있다.
$creator = (String)$xml->channel->item[0]->children(‘http://purl.org/dc/elements/1.1/’)->creator;
## 두번째 방법으로는, NS 를 정의한 후에 사용할 수 있다.
## 네임스페이스 정의
$ns = array
(
"content" => "http://purl.org/rss/1.0/modules/content/",
"wfw" => "http://wellformedweb.org/CommentAPI/",
"dc" => "http://purl.org/dc/elements/1.1/"
);
## 값 가져오기
$creator = (String)$xml->channel->item[0]->children($ns["dc"])->creator;
[/code]
* 참고
http://www.shop-wiz.com/board/view/root/php3/tid/204/category/0
http://blog.stuartherbert.com/php/2007/01/07/using-simplexml-to-parse-rss-feeds/
추천0
댓글목록
등록된 댓글이 없습니다.