XML processing utilities for Apache Groovy including markup builders, parsers, and navigation tools
—
Groovy XML provides predefined XML and HTML entity constants through the Entity class, making it easy to include special characters in XML documents without manual escaping.
Core class providing predefined XML and HTML entity constants that implement the Buildable interface for use in builders.
class Entity implements Buildable {
// Constructors
Entity(String name)
Entity(int name)
// Core method
void build(GroovyObject builder)
}Essential XML entities for special characters:
// Core XML entities
public static final Entity lt // less-than sign (<), U+003C
public static final Entity gt // greater-than sign (>), U+003E
public static final Entity amp // ampersand (&), U+0026
public static final Entity apos // apostrophe ('), U+0027
public static final Entity quot // quotation mark ("), U+0022Complete set of ISO Latin 1 entities for extended characters:
// Common punctuation and symbols
public static final Entity nbsp // no-break space, U+00A0
public static final Entity iexcl // inverted exclamation mark (¡), U+00A1
public static final Entity cent // cent sign (¢), U+00A2
public static final Entity pound // pound sign (£), U+00A3
public static final Entity curren // currency sign (¤), U+00A4
public static final Entity yen // yen sign (¥), U+00A5
public static final Entity copy // copyright sign (©), U+00A9
public static final Entity reg // registered sign (®), U+00AE
// Mathematical symbols
public static final Entity deg // degree sign (°), U+00B0
public static final Entity plusmn // plus-minus sign (±), U+00B1
public static final Entity sup2 // superscript two (²), U+00B2
public static final Entity sup3 // superscript three (³), U+00B3
public static final Entity frac14 // vulgar fraction one quarter (¼), U+00BC
public static final Entity frac12 // vulgar fraction one half (½), U+00BD
public static final Entity frac34 // vulgar fraction three quarters (¾), U+00BE
public static final Entity times // multiplication sign (×), U+00D7
public static final Entity divide // division sign (÷), U+00F7
// Accented characters (uppercase)
public static final Entity Agrave // latin capital A with grave (À), U+00C0
public static final Entity Aacute // latin capital A with acute (Á), U+00C1
public static final Entity Acirc // latin capital A with circumflex (Â), U+00C2
public static final Entity Atilde // latin capital A with tilde (Ã), U+00C3
public static final Entity Auml // latin capital A with diaeresis (Ä), U+00C4
public static final Entity Aring // latin capital A with ring above (Å), U+00C5
// ... (all uppercase accented characters)
// Accented characters (lowercase)
public static final Entity agrave // latin small a with grave (à), U+00E0
public static final Entity aacute // latin small a with acute (á), U+00E1
public static final Entity acirc // latin small a with circumflex (â), U+00E2
public static final Entity atilde // latin small a with tilde (ã), U+00E3
public static final Entity auml // latin small a with diaeresis (ä), U+00E4
public static final Entity aring // latin small a with ring above (å), U+00E5
// ... (all lowercase accented characters)Extended character set for typography and special symbols:
// Extended Latin
public static final Entity OElig // latin capital ligature OE (Œ), U+0152
public static final Entity oelig // latin small ligature oe (œ), U+0153
public static final Entity Scaron // latin capital letter S with caron (Š), U+0160
public static final Entity scaron // latin small letter s with caron (š), U+0161
public static final Entity Yuml // latin capital letter Y with diaeresis (Ÿ), U+0178
// Spacing modifiers
public static final Entity circ // modifier letter circumflex accent (ˆ), U+02C6
public static final Entity tilde // small tilde (˜), U+02DC
// Punctuation and typography
public static final Entity ensp // en space, U+2002
public static final Entity emsp // em space, U+2003
public static final Entity thinsp // thin space, U+2009
public static final Entity ndash // en dash (–), U+2013
public static final Entity mdash // em dash (—), U+2014
public static final Entity lsquo // left single quotation mark ('), U+2018
public static final Entity rsquo // right single quotation mark ('), U+2019
public static final Entity ldquo // left double quotation mark ("), U+201C
public static final Entity rdquo // right double quotation mark ("), U+201D
public static final Entity dagger // dagger (†), U+2020
public static final Entity Dagger // double dagger (‡), U+2021
public static final Entity permil // per mille sign (‰), U+2030
public static final Entity lsaquo // single left-pointing angle quotation mark (‹), U+2039
public static final Entity rsaquo // single right-pointing angle quotation mark (›), U+203A
public static final Entity euro // euro sign (€), U+20AC
// Control characters
public static final Entity zwnj // zero width non-joiner, U+200C
public static final Entity zwj // zero width joiner, U+200D
public static final Entity lrm // left-to-right mark, U+200E
public static final Entity rlm // right-to-left mark, U+200Fimport static groovy.xml.Entity.*
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.document {
title("Caf${eacute} Menu") // Café Menu
prices {
item(name: "Coffee") {
price("${dollar}3.50") // Using entity in text
note("Includes ${copy} trademark beans") // ©
}
item(name: "Caf${eacute} au Lait") {
price("${dollar}4.25")
discount("10${percent} off") // 10% off
}
}
footer {
copyright("${copy} 2023 Caf${eacute} Corp. All rights reserved.")
legal("Prices subject to change ${plusmn} local taxes")
}
}
println writer.toString()import static groovy.xml.Entity.*
def smb = new StreamingMarkupBuilder()
smb.encoding = 'UTF-8'
def content = smb.bind {
document {
header {
title("Mathematics ${amp} Science")
subtitle("Formulas ${amp} Equations")
}
formulas {
formula(type: "area") {
description("Circle area: ${pi}r${sup2}")
example("For r=5: A ${approx} 78.54")
}
formula(type: "temperature") {
description("Water freezes at 32${deg}F (0${deg}C)")
conversion("${deg}F = (${deg}C ${times} 9${divide}5) + 32")
}
formula(type: "fraction") {
description("Common fractions:")
examples {
mkp.yield("${frac14} = 0.25")
br()
mkp.yield("${frac12} = 0.5")
br()
mkp.yield("${frac34} = 0.75")
}
}
}
}
}
content.writeTo(new FileWriter('math-formulas.xml'))// Create custom entities
def trademark = new Entity("trade") // ™
def registered = new Entity("reg") // ®
def checkmark = new Entity(10003) // ✓ using Unicode code point
def xml = new MarkupBuilder(writer)
xml.products {
product(name: "Widget Pro") {
branding {
mkp.yield("Widget Pro")
trademark.build(xml) // Manually build entity
}
certification {
mkp.yield("ISO 9001 Certified")
checkmark.build(xml)
}
legal {
mkp.yield("Widget")
registered.build(xml)
mkp.yield(" is a registered trademark")
}
}
}import static groovy.xml.Entity.*
def createInternationalMenu = {
def xml = new MarkupBuilder(writer)
xml.menu {
section(cuisine: "French") {
item("Caf${eacute} au lait") // Café au lait
item("Cr${egrave}me br${ucirc}l${eacute}e") // Crème brûlée
item("Ratatouille ni${ccedil}oise") // Ratatouille niçoise
}
section(cuisine: "German") {
item("Sch${auml}ferhund") // Schäferhund
item("M${uuml}nchener Wei${szlig}bier") // Münchener Weißbier
item("${Auml}pfel mit Sahne") // Äpfel mit Sahne
}
section(cuisine: "Spanish") {
item("Ni${ntilde}o especial") // Niño especial
item("Jalape${ntilde}o") // Jalapeño
item("A${ntilde}ejo tequila") // Añejo tequila
}
section(cuisine: "Scandinavian") {
item("K${oslash}benhavn") // København
item("Sm${oslash}rrebr${oslash}d") // Smørrebrød
}
}
}import static groovy.xml.Entity.*
def createTypographicDocument = {
def xml = new MarkupBuilder(writer)
xml.article {
title("Typography ${amp} Design")
paragraph {
mkp.yield("The ${ldquo}quick brown fox${rdquo} jumps over the lazy dog.")
mkp.yield("This sentence contains ${mdash} em dash, ${ndash} en dash, and ${hellip} ellipsis.")
}
quote {
mkp.yield("${ldquo}Design is not just what it looks like ${mdash} ")
mkp.yield("design is how it works.${rdquo}")
attribution("${mdash} Steve Jobs")
}
technical {
formula("E = mc${sup2}")
temperature("Water boils at 100${deg}C (212${deg}F)")
fraction("${frac12} + ${frac14} = ${frac34}")
percentage("Sales increased by 15${permil}")
}
legal {
copyright("${copy} 2023 Design Corp.")
trademark("DesignTool${trade}")
registered("StyleGuide${reg}")
}
}
}For quick reference, here are the most commonly used entities:
| Entity | Symbol | Description | Unicode |
|---|---|---|---|
lt | < | Less than | U+003C |
gt | > | Greater than | U+003E |
amp | & | Ampersand | U+0026 |
quot | " | Quotation mark | U+0022 |
apos | ' | Apostrophe | U+0027 |
nbsp | Non-breaking space | U+00A0 | |
copy | © | Copyright | U+00A9 |
reg | ® | Registered | U+00AE |
trade | ™ | Trademark | U+2122 |
deg | ° | Degree | U+00B0 |
plusmn | ± | Plus-minus | U+00B1 |
times | × | Multiplication | U+00D7 |
divide | ÷ | Division | U+00F7 |
frac12 | ½ | One half | U+00BD |
euro | € | Euro | U+20AC |
mdash | — | Em dash | U+2014 |
ndash | – | En dash | U+2013 |
ldquo | " | Left double quote | U+201C |
rdquo | " | Right double quote | U+201D |
// Entities are static final constants - very efficient
import static groovy.xml.Entity.*
// Good: Direct entity usage
xml.text("Price: ${pound}10.99")
// Avoid: Creating new entities repeatedly
xml.text("Price: ${new Entity('pound')}10.99") // Less efficient
// For high-performance scenarios, consider pre-building entity strings
def currencySymbols = [
USD: dollar.toString(),
GBP: pound.toString(),
EUR: euro.toString(),
YEN: yen.toString()
]
xml.prices {
currencySymbols.each { code, symbol ->
currency(code: code, symbol: symbol)
}
}Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-xml