简体   繁体   中英

How do I add !DOCTYPE to my html with groovy

I would like to add the following DOCTYPE to my html email with groovy.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

My groovycode starts like this:

def responseDoc = job.addDocument("ECommerce_test.html"){out ->
def xmlWriter = new OutputStreamWriter(out)
MarkupBuilder html = new MarkupBuilder(xmlWriter)

I have tried to use the MarkupBuilderHelper, but I don' whant the xml-declaration. This is the code I have used with MarkupBuilderHelper and it doesn't seems to work without the declaration.

def helper = new groovy.xml.MarkupBuilderHelper(xml)
helper.xmlDeclaration([version:'1.0', encoding:'UTF-8', standalone:'no'])
helper.yieldUnescaped """<!DOCTYPE note SYSTEM "note.dtd">"""

/Therese

Something like this with StreamingMarkupBuilder?

import groovy.xml.*

def responseDoc = job.addDocument("ECommerce_test.html"){out ->
    out << new StreamingMarkupBuilder().bind {
        mkp.yieldUnescaped '<?xml version="1.0", encoding="UTF-8", standalone="no"?>\n'
        mkp.yieldUnescaped '<!DOCTYPE note SYSTEM "note.dtd">\n'
        html {
            body {
                h1('WOW!')
            }
        }
    }
}

An alternaive:

import groovy.xml.*

job.addDocument("ECommerce_test.html"){out ->
    new StringWriter().with { sw ->
        new MarkupBuilder(sw).html {
            body {
                h1('WOW!')
            }
        }
        out << '<!DOCTYPE note SYSTEM "note.dtd">\n' << sw.toString()
    }
}

It does look like a bug. yieldUnescaped works within a closure (within a tag), or after xml declaration, but not for generating a first top level statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM