Click to See Complete Forum and Search --> : Stopping extra space in XML output


Hester
06-18-2003, 08:09 AM
I'm using PHP arrays to translate XML into XHTML. But when I view the source of the outputted page from the browser, I find extra space. It has put a carriage return followed by nine single spaces after each piece of data. This is because the spaces are there in the XML file (generated from XML Notepad).

My code comes out like this:

---------< a href='Ag040603.doc
---------' title='Word'>(Agenda)< /a>< /td>

(Where "-" represents a space.) Luckily it still works.

I looked at controlling whitespace but that is only between tags. I thought my routine merely took the data between each tag and output the right HTML when a certain tag is found. Now it seems to be outputting spaces from the original file as well.

I don't want to remove all carriage returns as they can be useful in outputting clear HTML code flow. But I want to remove them from occuring after each piece of data is output (because as you can see above it is breaking up lines of HTML).

Is the only way to scan the XML first and remove spaces and returns?

Khalid Ali
06-18-2003, 10:24 AM
You will need to look into how the PHP XML parser works..it seems like the parser is putting in extra spaces..or you may want to post some XML code here.

Hester
06-18-2003, 10:41 AM
Here's the code:

// xml source
$xml_file = "xml/meetings.xml";

// hash of opening HTML tags
$startTags = array(
"MEETINGS" => "",
//...snip...
"AGENDA" => " <a href='agendas/",
"AGENDA_TEXT" => "' title='Word'>"
);

// hash of closing HTML tags
$endTags = array(
"MEETINGS" => "",
//...snip...
"AGENDA" => "",
"AGENDA_TEXT" => "</a></td>"
);

function startElement($parser, $NAME, $attrs) {
global $startTags;
// if tag exists as key, print value
if ($startTags[$NAME]) print $startTags[$NAME];
}

function endElement($parser, $NAME) {
global $endTags;
if ($endTags[$NAME]) print $endTags[$NAME];
}

function characterData($parser, $data) {
print $data;
}

// initialize the parser
$xml_parser = xml_parser_create();

// set callback functions
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

// open xml file
$fp = fopen($xml_file, "r");

// read and parse data
while ($data = fread($fp, 4096))
{
xml_parse($xml_parser, $data, feof($fp));
}

// close the parser
xml_parser_free($xml_parser);


The XML takes the form:

<meetings>
<agenda>Ag040603.doc</agenda>
<agenda_text>(Agenda)</agenda_text>
</meetings>

It's pretty basic because I am a beginner. There are many more fields in there I have left out for this example. Note that if you open the file in Notepad, there are returns and 9 spaces before the key elements.

Jona
06-18-2003, 11:32 AM
You could try using RegExps to strip all of the whitespaces in the variable after it's combined together before it's written to the page.

Jona