Forum Moderators: coopster
function parseme($file) {
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
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);
}
if(is_page('89')) {
$coursetitle = 'PageTitle';
$courses = parseme("http://webaddresstothefeed");
}
function startElement($parser, $name, $attrs)
{
global $map_array;
if (isset($map_array[$name])) {
echo "<$map_array[$name]>";
}
}
function endElement($parser, $name)
{
global $map_array;
if (isset($map_array[$name])) {
echo "</$map_array[$name]>";
}
}
function characterData($parser, $data)
{
echo $data;
}
function parseme($subject) {
$file = "XMLFEEDHERE".$subject;
function startElement($parser, $name, $attrs)
{
global $map_array;
if (isset($map_array[$name])) {
echo "<$map_array[$name]>";
}
}
function endElement($parser, $name)
{
global $map_array;
if (isset($map_array[$name])) {
echo "</$map_array[$name]>";
}
}
function characterData($parser, $data)
{
echo $data;
}
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
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. Please <a href=\"mailto:webmaster@\">contact us</a> to let us know there was a problem.");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d || Please <a href=\"mailto:webmaster@\">contact us</a> to let us know there was a problem.",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
}
<?php parseme('subject'); ?>
I found a work around but I'd still like to know what actually outputs the data.
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
Any thoughts on the second issue?
"echo" by default goes to STDOUT, which for a server process is the response that gets sent to the browser.
function startElement($parser, $name, $attrs)
{
global $map_array;
if (isset($map_array[$name])) {
$startme = "<$map_array[$name]>";
}
}
function endElement($parser, $name)
{
global $map_array;
if (isset($map_array[$name])) {
$endme = "</$map_array[$name]>";
}
}
// CourseLeaf XML Parser
function parseme($subject) {
$file = "XMLFEEDHERE".$subject;
$output='';
function startElement($parser, $name, $attrs)
{
global $map_array;
if (isset($map_array[$name])) {
$startme = "<$map_array[$name]>";
}
}
function endElement($parser, $name)
{
global $map_array;
if (isset($map_array[$name])) {
$endme = "</$map_array[$name]>";
}
}
function characterData($parser, $data)
{
global $output;
$output.=$data;
}
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, $startme, "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);
}
<div class="format_text">
<?php echo parseme('CJ');?>
<?php echo parseme('SO');?>
</div>
xml_set_element_handler($xml_parser, $startme, "endElement");
xml_set_element_handler($xml_parser, "startElement", "endElement");
<courseinfo>
<course code="CJ 101">
<![CDATA[
<div class="courseblock"> <p class="courseblocktitle"><strong>CJ 101. Introduction to Criminal Justice. 3 Credits.</strong></p> <p class="courseblockdesc"> A general survey of the principles, system, and process of criminal justice. Introduction to conceptions and definitions of crime, criminal law, and due process. Examination of the organization and operation of the three basic components of the criminal justice system -- the police, the courts, and corrections -- individually and in relationship to one another. Offered in fall semesters.<br /> </p> </div>
]]>
</course>
<course code="CJ 102">
<![CDATA[
<div class="courseblock"> <p class="courseblocktitle"><strong>CJ 102. Substantive Criminal Law. 3 Credits.</strong></p> <p class="courseblockdesc"> This course presents the development of criminal law in the United States and discusses its principles, sources, distinctions, and limitations. The following topics are covered in detail: criminal liability; offenses against persons, property, public peace and public justice; preparatory activity crimes; and defenses available to those charged with criminal activity. Offered spring semester.<br /> </p> </div>
]]>
</course>
...
}
function characterData($parser, $data)
{
global $output;
$output .= str_replace("<br />\n","",str_replace(" "," ",$data));
}
// Testing block
if(isset($GLOBALS['output'])) $answer = "it's global"; elseif (isset($output)) $answer = "it's local";
return $answer." -> ".gettype($output);
function parseme($subject) {
$data = '';
$output = '';
if (!($fp = fopen("http://catalog.example.com/ribbit/?page=getcourse.rjs&subject=".$subject, "r"))) {
//die("Could not open XML input. Please <a href=\"mailto:webmaster@example.com\">contact us</a> to let us know there was a problem.");
}
while ($chunk = fread($fp, 4096)) {
{
$data .= str_replace(']]>','',str_replace('<![CDATA[','',$chunk));
}
}
// Testing block
return $data;
fclose($fp);
unset($data);
}
[edited by: phranque at 10:35 am (utc) on Nov 17, 2013]
[edit reason] exemplified domain [/edit]
while ($chunk = fread($fp, 4096)) {
{
$data .= str_replace(']]>','',str_replace('<![CDATA[','',$chunk));
}
}
while ($chunk = fread($fp, 4096)) {
$data .= $chunk;
}
$data = str_replace(']]>','',str_replace('<![CDATA[','',$data));