import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element child = root.addElement("child");
child.addAttribute("name", "John");
child.setText("Hello, World!");
System.out.println(document.asXML());
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
Document document = new Document();
Element root = new Element("root");
document.setRootElement(root);
Element child = new Element("child");
root.addContent(child);
child.setAttribute("name", "John");
child.setText("Hello, World!");
XMLOutputter xmlOutputter = new XMLOutputter();
System.out.println(xmlOutputter.outputString(document));
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(System.out);
writer.writeStartDocument();
writer.writeStartElement("root");
writer.writeStartElement("child");
writer.writeAttribute("name", "John");
writer.writeCharacters("Hello, World!");
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndDocument();
writer.close();