Sugar4Xml is used for writing XML. Like its buddy Sugar4j, it also makes use of
the "treat-array-elements-as-pairs' pattern.
XML is written with only the two methods element() and end() .
element() writes an element, end() ends the last written element.
Example
This code:
public class SampleXml extends sugar4xml.Sugar4Xml<SampleXml> {
public SampleXml(java.io.Writer writer) {
super(writer);
}
public void doCreate() {
element("xml");
{
element("order", "id", 1, "pending", true);
{
element("amount", "1000").end();
}
end();
}
end();
}
public static void main(String[] args) {
SampleXml sample = new SampleXml(new java.io.OutputStreamWriter(System.out));
sample.create();
}
}
|
prints out this xml:
<xml>
<order id="1" pending="true">
<amount>1000</amount>
</order>
</xml>
|
Another example using schema based XHtml superclass:
package sugar4xml.gen;
public class SampleXHtmlDocument extends sugar4xml.xhtml.XHtml<SampleXHtmlDocument> {
public SampleXHtmlDocument(java.io.Writer writer) {
super(writer);
}
public void doCreate() {
html(xmlns, XHtml);
{
head();
{
title("My XHtml Document").end();
}
end();
body();
{
table(cellspacing, 0, cellpadding, 2, border, true);
{
colgroup();
{
col().end();
col(width, "100%").end();
}
end();
tr();
{
td("data1").end();
td("data2").end();
}
end();
}
end();
}
end();
}
end();
}
}
|
Sugar4Xml is packaged together with Sugar4j and can be downloaded here. |