Posted By

donfips on 06/18/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

koyotebr


DOM HTML output


 / Published in: Java
 

  1. import java.io.File;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4.  
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import javax.xml.parsers.ParserConfigurationException;
  8. import javax.xml.transform.OutputKeys;
  9. import javax.xml.transform.Transformer;
  10. import javax.xml.transform.TransformerConfigurationException;
  11. import javax.xml.transform.TransformerException;
  12. import javax.xml.transform.TransformerFactory;
  13. import javax.xml.transform.TransformerFactoryConfigurationError;
  14. import javax.xml.transform.dom.DOMSource;
  15. import javax.xml.transform.stream.StreamResult;
  16.  
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.Element;
  19. import org.w3c.dom.DocumentType;
  20.  
  21. public class Output {
  22.  
  23. public static void htmlOutput(ArrayList<Question> fiveQuestions){
  24.  
  25. DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
  26. DocumentBuilder builder = null;
  27. try {
  28. builder = dbfactory.newDocumentBuilder();
  29. } catch (ParserConfigurationException e) {
  30. // TODO Auto-generated catch block
  31. e.printStackTrace();
  32. }
  33.  
  34. Document doc = builder.newDocument();
  35. DOMSource source = new DOMSource(doc);
  36.  
  37. Element html = doc.createElement("html");
  38. doc.appendChild(html);
  39.  
  40. Element head = doc.createElement("head");
  41. html.appendChild(head);
  42.  
  43. Element title = doc.createElement("title");
  44. head.appendChild(title);
  45. title.appendChild(doc.createTextNode("Quit HTML Output"));
  46.  
  47. Element body = doc.createElement("body");
  48. html.appendChild(body);
  49.  
  50. Element h1 = doc.createElement("h1");
  51. body.appendChild(h1);
  52. h1.appendChild(doc.createTextNode("Fünf Fragen aus dem Gebiet " + fiveQuestions.get(0).getArea()));
  53.  
  54. Element br = doc.createElement("br");
  55. body.appendChild(br);
  56.  
  57.  
  58. for (Iterator<Question> fiveIterator = fiveQuestions.iterator() ; fiveIterator.hasNext();) {
  59.  
  60. Element p = doc.createElement("p");
  61. body.appendChild(p);
  62. p.appendChild(doc.createTextNode((fiveIterator.next().getQuestion())));
  63.  
  64. Element ul = doc.createElement("ul");
  65. body.appendChild(ul);
  66.  
  67.  
  68.  
  69. for (Iterator<String> answerIterator = fiveIterator.next().getAnswers().iterator(); answerIterator.hasNext();) {
  70. Element li = doc.createElement("li");
  71. ul.appendChild(li);
  72. li.appendChild(doc.createTextNode(answerIterator.next()));
  73.  
  74.  
  75. }
  76.  
  77. }
  78.  
  79. StreamResult result = new StreamResult(new File("./data.html"));
  80. System.out.println("tata");
  81.  
  82. Transformer transformer;
  83. try {
  84. transformer = TransformerFactory.newInstance().newTransformer();
  85. // transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC);
  86. transformer.transform(source, result);
  87. } catch (TransformerConfigurationException e) {
  88. // TODO Auto-generated catch block
  89. e.printStackTrace();
  90. } catch (TransformerFactoryConfigurationError e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. } catch (TransformerException e) {
  94. // TODO Auto-generated catch block
  95. e.printStackTrace();
  96. }
  97.  
  98.  
  99. }
  100. }

Report this snippet  

You need to login to post a comment.