views:

33

answers:

2

I have a c program which outputs a number of lines to another c program which stuffs them in a PHP page which is loaded with AJAX. The problem is that the output is a number of lines, which is fine to look at, but which, when viewed as HTML, lack line breaks.

The initial thought I had was obviously to put line breaks in with the output. -- That worked fine, especially since I was using responseText to handle the AJAX output. Now I have discovered that along with the raw text, a bit of metadata also needs to be part of the AJAX response. I jumped over to using responseXML, only to find that the
tags no longer worked correctly. At this point I could slog through any number of tutorials to figure out how to work some more complicated mechanism, but I really just want a hack. Could I embed the metadata in an html comment and use the DOM to dig it out (I looked and don't see a good method to get to comments using the dom...)? Could I use the xml directly as html somehow? Could I use CDATA in the xml document(this doesn't seem hopeful)? Could I just use newlines until the code reaches the webpage and then have JS insert the br tags?

I don't need any other formatting, just line breaks, and all this needs to do is work, the less complex the better.

A: 

How about using a XSLT stylesheet to format your incoming XML. Save the following as an .html file for an example. Sources : http://www.w3schools.com/xsl/xsl_client.asp & http://www.w3schools.com/dom/dom_parser.asp

<html>
<head>
    <script>
        //YOUR XML FROM AJAX
        var XML = "<top><meta><itemone>test meta</itemone></meta><rows><row>line one</row><row>line two</row><row>line three</row></rows></top>";
        //A stylesheet to format the lines that come back.
        var XSLT = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;&lt;xsl:template match="/"><h2>Lines</h2><xsl:for-each select="descendant::row"><xsl:value-of select="self::*"/><br /></xsl:for-each></xsl:template></xsl:stylesheet>'

        function loadXMLDoc(xml)
        {
            var tempXML;
            //IE
            if (window.ActiveXObject)
            {
                tempXML=new ActiveXObject("Microsoft.XMLDOM");
                tempXML.loadXML(xml);
            }
            else if(window.DOMParser)
            {
                parser=new DOMParser();
                tempXML=parser.parseFromString(xml,"text/xml");
            }
            return tempXML;
        }

        function displayResult()
        {
            var xmlDoc = loadXMLDoc(XML);
            var xsltDoc = loadXMLDoc(XSLT);
            // code for IE
            if (window.ActiveXObject)
            {
                var ex=xmlDoc.transformNode(xsltDoc);
                document.getElementById("example").innerHTML=ex;
            }
            // code for Mozilla, Firefox, Opera, etc.
            else if (document.implementation && document.implementation.createDocument)
            {
                var xsltProcessor=new XSLTProcessor();
                xsltProcessor.importStylesheet(xsltDoc);
                var resultDocument = xsltProcessor.transformToFragment(xmlDoc,document);
                document.getElementById("example").appendChild(resultDocument);
            }
        }
    </script>
</head>
<body onload="displayResult()">
<div id="example" />
</body>
</html>
Brandon Boone
Thanks for this, I may end up needing to use this or something like it in the near future, but for now it isn't quite simplest possible...
conartist6
A: 

Thanks for all the good suggestions, but I eventually decided to just prepend a fixed number of descriptor bytes to each text response and then use the substring command to get either the descriptor bytes or the main text response. This allows me to keep using the simpler response-text mechanism, and is otherwise uncomplicated. I would have used custom headers but I realized that that would have required buffering the whole output in yet ANOTHER place since the php script actually only contains a single system() call, and has no idea what the C program behind it is doing.

conartist6