Question: How to create a xml file and save it in some place in my machine using java..there are attributes also include in the xml file? I have found org.w3c.dom.Document but having problems with creating attributes for elements and save the xml file.

Thank You.

Answer: 
Have look at [**dom4j**](http://sourceforge.net/projects/dom4j/) or [**jdom**](http://www.jdom.org/). Both libraries allow creating a Document and allow printing the document as xml. Both are widly used, pretty easy to use and you'll find a lot of examples and snippets.

[dom4j - Quick start guide](http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html)
I liked the Xembly syntax, but it is not a statically typed API. You can get this with [XMLBeam](http://xmlbeam.org):

```
// Declare a projection
public interface Projection {

    @XBWrite("/root/order/@id")
    Projection setID(int id);

    @XBWrite("/root/order")
    Projection setValue(String value);
}

public static void main(String[] args) {
    // create a projector
    XBProjector projector = new XBProjector();

    // use it to create a projection instance
    Projection projection = projector.projectEmptyDocument(Projection.class);

    // You get a fluent API, with java types in parameters 
    projection.setID(553).setValue("$140.00");

    // Use the projector again to do IO stuff or create an XML-string
    projector.toXMLString(projection);
}

```

My experience is that this works great even when the XML gets more complicated. You can just decouple the XML structure from your java code structure.