java - write html using StAX API -


i new java, using stax api write html, (i know xml) in method writer.writecharacters() if added unicode string
& #x25ca;, html content contains & amp;#x25ca; (space not there between & , amp). how add special characters html supports. mean no escaping should happen.

using filewriter enough write large html files or there specific api's there. searched web, found many solutions if lot of string content involved how efficiently can write using filewriter without consuming more memory.

 public class buildhtml    {     static final logger logger = logger.getlogger(buildhtml.class);     public static void main(string[] args)         {             final string infilepath = system.getproperty("user.dir") + "/test_output/output.html";     xmloutputfactory factory = xmloutputfactory.newinstance();     fileoutputstream fout = null;     xmlstreamwriter writer = null;     try      {         fout = new fileoutputstream(infilepath);         writer = factory.createxmlstreamwriter(fout);         writer.writestartelement("html");         writer.writestartelement("head");         writer.writestartelement("body");         writer.writestartelement("table");         writer.writeattribute("align", "center");         writer.writeattribute("border", "1");         writer.writestartelement("tbody");          (int = 0; < 10000; i++)          {             writer.writestartelement("tr");             (int j = 0; j < 10000; j++)              {                 writer.writestartelement("td");                 writer.writecharacters(("" + j));                 writer.writecharacters("&#x25ca;")                 writer.writeendelement();             }             writer.writeendelement();         }         writer.writeenddocument();         system.out.println("xml created.");     } catch (exception e) {         logger.info("exception", e);     }     {         try {             if(writer != null)             {                 writer.flush();                 writer.close();             }             if(fout != null)             {                 fout.flush();                 fout.close();             }         } catch (ioexception | xmlstreamexception e) {             e.printstacktrace();         }                } } } 

as suggested in comments, use template engine.

advantages are: smaller code size, easier write, no overhead of building in-memory representation dom, since template engine stream generated output.

here example written in csp:

template(int rows, int cols) {{      <html>      <head></head>      <body>          <table align="center" border="1">          <tbody>          @for (int i=0; i<rows; i++)             <tr>              @for (int j=0; j<cols; j++)                    <td><%j%></td>             </tr>          </tbody>          </table>      </body>      </html>  }} 

Comments