PDA

View Full Version : Help with parsing xml via php



3twentysix
05 Apr 2010, 07:13 PM
First post and I'm in big need of some help. I'm a web designer who handles very little web development outside of Wordpress. I'm trying to parse xml (generated by an api) via php, BUT I keep getting errors because of character encoding (I think). I have no clue what to do to fix it, and would love it if someone could help me out.

Here's what I'm working with...
XML


<?xml version="1.0" encoding="UTF-8"?>
<OpenVoter>
<submission>
<title>SICK OF IT ALL Post &acirc;��Lowest Common Denominator&acirc;�� Online &Acirc;&laquo; The NewReview</title>
<summary>SICK OF IT ALL have posted a new song ...</summary>
</submission>
</OpenVoter>


parser.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<?php

$file = "name of my xml file here";

function contents($parser, $data){
echo $data;
}

function startTag($parser, $data){
echo "<b>";
}

function endTag($parser, $data){
echo "</b><br/>";
}

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($file, "r");

$data = fread($fp, 80000);

if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);

?>
</body>
</html>

When I access parser.php in my browser, I get

SICK OF IT ALL Post Error on line 4

How can I strip out or replace these characters that are causing this issue?