Parsing JSKit/Echo XML using PHP
According to dpusa in the comments, you can manually insert comments into WordPress using something like:
[toggle code]
-
$data = array(
- 'comment_post_ID' => 256,
- 'comment_author' => 'Dave',
- 'comment_author_email' => 'dave@example.com',
- 'comment_author_url' => 'http://hal.example.com',
- 'comment_content' => 'Lorem ipsum dolor sit amet...',
- 'comment_author_IP' => '127.3.1.1',
- 'comment_agent' => 'manual insertion',
- 'comment_date' => date('Y-m-d H:i:s'),
- 'comment_date_gmt' => date('Y-m-d H:i:s'),
- 'comment_approved' => 1,
- );
- $comment_id = wp_insert_comment($data);
In PHP, you should be able to loop through a jskit XML file using something like:
[toggle code]
- $comments = simplexml_load_file("/path/to/comments.xml");
-
function getJSKitAttribute($item, $key) {
- $attribute = $item->xpath('./jskit:attribute[@key="' . $key . '"]/@value');
- $attribute = $attribute[0];
- return $attribute;
- }
-
foreach ($comments as $page) {
-
if ($page->item) {
- $pageURL = $page->link;
- echo $pageURL, "\n";
-
foreach ($page->item as $comment) {
- $date = $comment->pubDate;
- $text = $comment->description;
- $IP = getJSKitAttribute($comment, 'IP');
- echo "\t", substr($text, 0, 80), "\n";
- echo "\t\t", $date, "\n";
- echo "\t\t", $IP, "\n";
- }
- echo "\n";
- }
-
if ($page->item) {
- }
You could then fill out the $data array with the values of $date, $text, $IP, etc., or hard-code them to default values if they don’t exist. Do this in place of (or in addition to) the three “echo” lines.
[toggle code]
-
$data = array(
- 'comment_post_ID' => $comment->guid,
- 'comment_author' => $comment->author,
- 'comment_content' => $text,
- 'comment_author_IP' => $IP,
- 'comment_agent' => 'manual insertion',
- 'comment_date_gmt' => strtotime($date),
- 'comment_approved' => 1,
- );
- $comment_id = wp_insert_comment($data);
Since I don’t know WordPress, I’m not going to guess at the exact code. For example, I suspect that WordPress’s ID field is numeric, which means you won’t be able to use JSKit’s guid, since it is a string. You’ll need to generate your own unique numeric ID if that’s the case. But if WordPress allows you to manually insert comments through the code, then you should be able to import your JSKit XML files into it relatively easily.
In response to Parsing JSKit/Echo XML comments files: While I’m not a big fan of remote comment systems for privacy reasons, I was willing to use JSKit as a temporary solution because they provide an easy XML dump of posted comments. This weekend, I finally moved my main blog to custom comments; here’s how I parsed JSKit’s XML file.
More XML
- Catalina: iTunes Library XML
- What does Catalina mean for 42 Astounding Scripts?
- Parsing JSKit/Echo XML comments files
- While I’m not a big fan of remote comment systems for privacy reasons, I was willing to use JSKit as a temporary solution because they provide an easy XML dump of posted comments. This weekend, I finally moved my main blog to custom comments; here’s how I parsed JSKit’s XML file.
- Auto-closing HTML tags in comments
- One of the biggest problems on blogs is that comments often get stuck with unclosed italics, bold, or links. You can automatically close them by transforming the HTML snippet into an XML document.
- minidom self-closes empty SCRIPT tags
- Python’s minidom will self-close empty script tags—as it should. But it turns out that Firefox 3.6 and IE 8 don’t support empty script tags.
- A present for Palm
- Palm needs a little help understanding XML.
- Five more pages with the topic XML, and other related pages