Semalam ada request (kaya di radio saja), untuk mengambil data TEXT dari XML hasil export blog Wordpress-nya. Hm, langsung buka-buka manual PHP, ketemu XML Parser.
http://localhost/doc/php-doc/html/book.xml.html
Dari sana ketemu contoh program, langsung copy-paste, sunting-2, simpan... dan jalankan!
Hanya tag2 berikut yg saya ambil: TITLE = judul posting
PUBDATE = tanggal publikasi
CONTENT:ENCODED = isi posting
WP:COMMENT_AUTHOR = komentator
WP:COMMENT_AUTHOR_EMAIL = email komentator
WP:COMMENT_DATE = tanggal komentar
WP:COMMENT_CONTENT = komentar
Untuk menjalankan:
$ php xml2txt.php > hasil.txt
Berkas XML (wordpress.2009-06-25.xml) dan xml2txt.php harus berada dlm direktori kerja.
<?php
// nama program: xml2txt.php
$file = "wordpress.2009-06-25.xml";
//$depth = array();
$found_item = false;
$echo_data = false;
$data_count = 0;
$post_count = 0;
$comment_count = 0;
$data_id = '';
function startElement($parser, $name, $attrs)
{
//global $depth;
global $echo_data;
global $found_item;
global $data_id;
//for ($i = 0; $i < $depth[$parser]; $i++) {
// echo " ";
//}
//echo "$name\n";
//$depth[$parser]++;
if (!$found_item)
{
if ($name=='ITEM')
$found_item = true;
}
else if (in_array($name, array('TITLE','PUBDATE','CONTENT:ENCODED',
'WP:COMMENT_AUTHOR','WP:COMMENT_AUTHOR_EMAIL','WP:COMMENT_DATE','WP:COMMENT_CONTENT')))
{
$echo_data = true;
$data_id = $name;
}
else
$echo_data = false;
}
function endElement($parser, $name)
{
//global $depth;
//$depth[$parser]--;
}
function characterData($parser, $data)
{
global $echo_data;
global $data_count;
global $post_count;
global $comment_count;
global $data_id;
//if ($data_count>200)
// exit;
if (!$data || $data=="\n")
return;
if ($echo_data)
{
if ($data_id=='TITLE')
{
if ($post_count!=0)
echo "\n\n<!-- AKHIR -->\n\n";
++$post_count;
$comment_count = 0;
}
else if ($data_id=='WP:COMMENT_AUTHOR')
{
if ($comment_count==0)
echo "\n<!-- KOMENTAR: -->\n";
++$comment_count;
echo "<!-- ($comment_count) -->\n";
}
//else
// echo "\n";
echo htmlspecialchars_decode(strip_tags($data));
if (in_array($data_id,array('TITLE','WP:COMMENT_AUTHOR','WP:COMMENT_AUTHOR_EMAIL')))
echo "\t";
else if ($data_id=='WP:COMMENT_DATE')
echo "\n";
else if ($data_id=='PUBDATE')
echo "\n\n";
++$data_count;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
?>
4 Komentar
wahhh ribet ya....
BalasHapusHe;;; klo ga mau ribet ambil semua XML atau jangan ambil sama sekali :P
BalasHapuspuciiiing
BalasHapusknapa?
BalasHapus