<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>
<channel>
	<title>Comments on: A Fluent Interface for XML DOM APIs</title>
	<atom:link href="http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/feed/" rel="self" type="application/rss+xml" />
	<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>Sun, 05 Jul 2009 03:22:01 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Stefan Tilkov</title>
		<link>http://blog.springsource.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/comment-page-1/#comment-1122</link>
		<dc:creator>Stefan Tilkov</dc:creator>
		<pubDate>Sun, 13 May 2007 17:54:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.springframework.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1122</guid>
		<description>&lt;p&gt;In Ruby:&lt;/p&gt;

&lt;pre&gt;
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'
  }
}
&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-page-1/#comment-1121</link>
		<dc:creator>Erik-Jan Blanksma</dc:creator>
		<pubDate>Fri, 11 May 2007 20:06:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.springframework.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1121</guid>
		<description>&lt;p&gt;I had a go at creating a utility class that mimmicks the Xlinq API. Here's what I came up with:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
class XElement{
    List children = new ArrayList();
    Listattributes = new ArrayList();
    final String name;
    String text;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;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;     
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;class XAttribute{
    final String name;
    final String value;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public XAttribute(String name, String value){
    this.name = name;
    this.value = value;     
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To create a document:
&lt;code&gt;
Document doc = 
               new XElement("contacts",
                  new XElement("contact",
                     new XElement("name", "John Doe"),
                     new XElement("phone", "555-12345",
                        new XAttribute("type", "home")),
                     new XElement("phone", "555-67890",
                        new XAttribute("type", "work"))
                    )
                  ).toDocument();
&lt;/code&gt;&lt;/p&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>
class XElement{
    List children = new ArrayList();
    Listattributes = new ArrayList();
    final String name;
    String text;</code></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{
    final String name;
    final String value;</p>

<pre><code>public XAttribute(String name, String value){
    this.name = name;
    this.value = value;     
}
</code></pre>

<p>}
</p>

<p>To create a document:
<code>
Document doc = 
               new XElement("contacts",
                  new XElement("contact",
                     new XElement("name", "John Doe"),
                     new XElement("phone", "555-12345",
                        new XAttribute("type", "home")),
                     new XElement("phone", "555-67890",
                        new XAttribute("type", "work"))
                    )
                  ).toDocument();
</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-page-1/#comment-1120</link>
		<dc:creator>Dan Diephouse</dc:creator>
		<pubDate>Thu, 10 May 2007 17:26:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.springframework.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1120</guid>
		<description>&lt;p&gt;I like the idea. I think it'd be great to make this a "single class library". I.e. you could just create a class and various people can stick it in their util directory instead of Yet Another Jar.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;
</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-page-1/#comment-1119</link>
		<dc:creator>Craig Wals</dc:creator>
		<pubDate>Thu, 10 May 2007 04:05:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.springframework.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1119</guid>
		<description>&lt;p&gt;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.&lt;/p&gt;
</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-page-1/#comment-1118</link>
		<dc:creator>Arjen Poutsma</dc:creator>
		<pubDate>Thu, 10 May 2007 00:50:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.springframework.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1118</guid>
		<description>&lt;p&gt;@Guillaume,&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;
</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-page-1/#comment-1117</link>
		<dc:creator>Guillaume Laforge</dc:creator>
		<pubDate>Thu, 10 May 2007 00:26:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.springframework.com/arjen/archives/2007/05/10/a-fluent-interface-for-xml-dom-apis/#comment-1117</guid>
		<description>&lt;p&gt;What about Groovy's Markup builder?&lt;/p&gt;

&lt;pre&gt;
def mkp = new MarkupBuilder()
mkp.contacts {
   contact {
      name("John Doe")
      phone(type: "home", "555-12345")
      phone(type:"work", "555-67890")
   }
}
&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>
