Programmatic HTMLDocument generation using Java

前端 未结 9 1884
孤独总比滥情好
孤独总比滥情好 2021-02-14 03:34

Does anyone know how to generate an HTMLDocument object programmatically in Java without resorting to generating a String externally and then using HTMLEditorKit#read to parse i

9条回答
  •  滥情空心
    2021-02-14 04:02

    When dealing with XHTML, I have had much success using Java 6's XMLStreamWriter interface.

    OutputStream destination = ...;
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    XMLStreamWriter xml = outputFactory.createXMLStreamWriter(destination);
    
    xml.writeStartDocument();
    xml.writeStartElement("html");
    xml.writeDefaultNamespace("http://www.w3.org/1999/xhtml");
    
    xml.writeStartElement("head");
    xml.writeStartElement("title");
    xml.writeCharacters("The title of the page");
    xml.writeEndElement();
    xml.writeEndElement();
    
    xml.writeEndElement();
    xml.writeEndDocument();
    

提交回复
热议问题