www.webdeveloper.com
Results 1 to 4 of 4

Thread: Stopping extra space in XML output

  1. #1
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96

    Stopping extra space in XML output

    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?

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,136
    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.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Jun 2003
    Location
    Yorkshire UK
    Posts
    96
    Here's the code:
    PHP 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($fp4096)) 
    {
    xml_parse($xml_parser$datafeof($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.

  4. #4
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles