<?xml version="1.0" encoding="utf-8"?><!-- generator="wordpress/2.0.2" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: A Fluent Interface for XML DOM APIs</title>
	<link>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/</link>
	<description>A blog about programming in .NET and Java</description>
	<pubDate>Thu, 20 Nov 2008 13:32:42 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>

	<item>
		<title>by: Stefan Tilkov</title>
		<link>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1122</link>
		<pubDate>Sun, 13 May 2007 17:54:21 +0000</pubDate>
		<guid>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1122</guid>
					<description>In Ruby:

&lt;pre&gt;
require 'builder'
x = Builder::XmlMarkup.new(:target =&amp;#62; $stdout, :indent =&amp;#62; 2)
x.contacts {
  x.contact {
    x.name('John Doe')
    x.phone '555-12345', :type =&amp;#62; 'home'
    x.phone '555-67890', :type =&amp;#62; 'work'
  }
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>In Ruby:</p>
<pre>
require 'builder'
x = Builder::XmlMarkup.new(:target =&gt; $stdout, :indent =&gt; 2)
x.contacts {
  x.contact {
    x.name('John Doe')
    x.phone '555-12345', :type =&gt; 'home'
    x.phone '555-67890', :type =&gt; 'work'
  }
}
</pre>
]]></content:encoded>
				</item>
	<item>
		<title>by: Erik-Jan Blanksma</title>
		<link>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1121</link>
		<pubDate>Fri, 11 May 2007 20:06:12 +0000</pubDate>
		<guid>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1121</guid>
					<description>I had a go at creating a utility class that mimmicks the Xlinq API. Here's what I came up with:

&lt;code&gt;
class XElement{
	List children = new ArrayList();
	Listattributes = new ArrayList();
	final String name;
	String text;
	
	public XElement(String name){
		this.name = name;		
	}
	
	public XElement(String name, String text, XAttribute... xAttributes){
		this(name);
		this.text = text;	
		attributes.addAll(Arrays.asList(xAttributes));
	}
	
	public XElement (String name, XElement... xElements){
		this(name);
		children.addAll(Arrays.asList( xElements));
	}
		
	public Document toDocument(){
		return build(this);
	}
	
	public static Document build(XElement xElement) {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			throw new RuntimeException(e);
		}
		Document doc = db.newDocument();
		doc.appendChild(buildElement(doc, xElement));
		return doc;		
	}
	
	public static Element buildElement(Document document, XElement xElement){
		Element element = document.createElement(xElement.name);
		for (Iterator iter = xElement.attributes.iterator(); iter.hasNext();) {
			XAttribute attribute = (XAttribute) iter.next();
			element.setAttribute(attribute.name, attribute.value);
		}
		if(xElement.text!=null){
			element.setTextContent(xElement.text);
		}else{
			for (XElement childElement : xElement.children) {
				element.appendChild(buildElement(document, childElement));					
			}
		}
		return element;		
	}
}

class XAttribute{
	final String name;
	final String value;

	public XAttribute(String name, String value){
		this.name = name;
		this.value = value;		
	}
}
&lt;/code&gt;

To create a document:
&lt;code&gt;
Document doc = 
			   new XElement(&quot;contacts&quot;,
			      new XElement(&quot;contact&quot;,
			         new XElement(&quot;name&quot;, &quot;John Doe&quot;),
			         new XElement(&quot;phone&quot;, &quot;555-12345&quot;,
			            new XAttribute(&quot;type&quot;, &quot;home&quot;)),
			         new XElement(&quot;phone&quot;, &quot;555-67890&quot;,
			            new XAttribute(&quot;type&quot;, &quot;work&quot;))
			        )
			      ).toDocument();
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>I had a go at creating a utility class that mimmicks the Xlinq API. Here&#8217;s what I came up with:</p>
<p><code><br />
class XElement{<br />
    List children = new ArrayList();<br />
    Listattributes = new ArrayList();<br />
    final String name;<br />
    String text;</p>
<pre><code>public XElement(String name){
    this.name = name;
}

public XElement(String name, String text, XAttribute... xAttributes){
    this(name);
    this.text = text;
    attributes.addAll(Arrays.asList(xAttributes));
}

public XElement (String name, XElement... xElements){
    this(name);
    children.addAll(Arrays.asList( xElements));
}

public Document toDocument(){
    return build(this);
}

public static Document build(XElement xElement) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db;
    try {
        db = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    Document doc = db.newDocument();
    doc.appendChild(buildElement(doc, xElement));
    return doc;
}

public static Element buildElement(Document document, XElement xElement){
    Element element = document.createElement(xElement.name);
    for (Iterator iter = xElement.attributes.iterator(); iter.hasNext();) {
        XAttribute attribute = (XAttribute) iter.next();
        element.setAttribute(attribute.name, attribute.value);
    }
    if(xElement.text!=null){
        element.setTextContent(xElement.text);
    }else{
        for (XElement childElement : xElement.children) {
            element.appendChild(buildElement(document, childElement));
        }
    }
    return element;
}
</code></pre>
<p>}</p>
<p>class XAttribute{<br />
    final String name;<br />
    final String value;</p>
<pre><code>public XAttribute(String name, String value){
    this.name = name;
    this.value = value;
}
</code></pre>
<p>}<br />
</code></p>
<p>To create a document:<br />
<code><br />
Document doc =<br />
               new XElement("contacts",<br />
                  new XElement("contact",<br />
                     new XElement("name", "John Doe"),<br />
                     new XElement("phone", "555-12345",<br />
                        new XAttribute("type", "home")),<br />
                     new XElement("phone", "555-67890",<br />
                        new XAttribute("type", "work"))<br />
                    )<br />
                  ).toDocument();<br />
</code></p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Dan Diephouse</title>
		<link>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1120</link>
		<pubDate>Thu, 10 May 2007 17:26:32 +0000</pubDate>
		<guid>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1120</guid>
					<description>I like the idea. I think it'd be great to make this a &quot;single class library&quot;. I.e. you could just create a class and various people can stick it in their util directory instead of Yet Another Jar.

Groovy isn't the solution for all things because I don't necessarily want to embed groovy in all my apps just to do this.</description>
		<content:encoded><![CDATA[<p>I like the idea. I think it&#8217;d be great to make this a &#8220;single class library&#8221;. I.e. you could just create a class and various people can stick it in their util directory instead of Yet Another Jar.</p>
<p>Groovy isn&#8217;t the solution for all things because I don&#8217;t necessarily want to embed groovy in all my apps just to do this.</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Craig Wals</title>
		<link>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1119</link>
		<pubDate>Thu, 10 May 2007 04:05:53 +0000</pubDate>
		<guid>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1119</guid>
					<description>Anticipating Guillaume's next question: Why do you need to use it in Java? Why not just use Groovy markup in a Groovy class, then have your Java code delegate to that class for XML handling? Push all of your XML access code into a DAO-like class (XAO?) and then call that whenever you need to create some XML.</description>
		<content:encoded><![CDATA[<p>Anticipating Guillaume&#8217;s next question: Why do you need to use it in Java? Why not just use Groovy markup in a Groovy class, then have your Java code delegate to that class for XML handling? Push all of your XML access code into a DAO-like class (XAO?) and then call that whenever you need to create some XML.</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Arjen Poutsma</title>
		<link>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1118</link>
		<pubDate>Thu, 10 May 2007 00:50:19 +0000</pubDate>
		<guid>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1118</guid>
					<description>@Guillaume,

Yeah, the Groovy Markup builder is definitely fluent as well; I forgot to mention it. Writing a fluent interface in a more dynamic language like Groovy is easier in a way. The only downside is that I cannot use it in Java.</description>
		<content:encoded><![CDATA[<p>@Guillaume,</p>
<p>Yeah, the Groovy Markup builder is definitely fluent as well; I forgot to mention it. Writing a fluent interface in a more dynamic language like Groovy is easier in a way. The only downside is that I cannot use it in Java.</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Guillaume Laforge</title>
		<link>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1117</link>
		<pubDate>Thu, 10 May 2007 00:26:22 +0000</pubDate>
		<guid>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1117</guid>
					<description>What about Groovy's Markup builder?

&lt;pre&gt;
def mkp = new MarkupBuilder()
mkp.contacts {
   contact {
      name(&quot;John Doe&quot;)
      phone(type: &quot;home&quot;, &quot;555-12345&quot;)
      phone(type:&quot;work&quot;, &quot;555-67890&quot;)
   }
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>What about Groovy&#8217;s Markup builder?</p>
<pre>
def mkp = new MarkupBuilder()
mkp.contacts {
   contact {
      name("John Doe")
      phone(type: "home", "555-12345")
      phone(type:"work", "555-67890")
   }
}
</pre>
]]></content:encoded>
				</item>
</channel>
</rss>
