How to create an XML Document with JDOM
package org.siamnet;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class RssExampleDocument {
public void main() {
Element rootElement = new Element("rss");
rootElement.setAttribute("version", "1.0");
rootElement.setAttribute("encoding", "utf-8");
Document xml = new Document();
xml.setRootElement(rootElement);
Element channel = new Element("channel");
xml.getRootElement().addContent(channel);
Element title = new Element("title");
title.setText("This is a title.");
channel.addContent(title);
Element description = new Element("description");
description.setText("This is a description.");
channel.addContent(description);
Element language = new Element("language");
language.setText("en-us");
channel.addContent(language);
Element copyright = new Element("copyright");
copyright.setText("2011");
channel.addContent(copyright);
Element lastBuildDate = new Element("lastBuildDate");
lastBuildDate.setText("2011");
channel.addContent(lastBuildDate);
Element ttl = new Element("ttl");
ttl.setText("30");
channel.addContent(ttl);
Element item = new Element("item");
item.addContent(new Element("title").setText("Title"));
item.addContent(new Element("description").setText("Description"));
item.addContent(new Element("pubDate").setText("2011-11-11 11:11:11"));
item.addContent(new Element("link").setText("http://www.example.com/"));
channel.addContent(item);
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
System.out.println(String.format("xmlOutput = %s", xmlOutput.outputString(xml)));
}
}
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class RssExampleDocument {
public void main() {
Element rootElement = new Element("rss");
rootElement.setAttribute("version", "1.0");
rootElement.setAttribute("encoding", "utf-8");
Document xml = new Document();
xml.setRootElement(rootElement);
Element channel = new Element("channel");
xml.getRootElement().addContent(channel);
Element title = new Element("title");
title.setText("This is a title.");
channel.addContent(title);
Element description = new Element("description");
description.setText("This is a description.");
channel.addContent(description);
Element language = new Element("language");
language.setText("en-us");
channel.addContent(language);
Element copyright = new Element("copyright");
copyright.setText("2011");
channel.addContent(copyright);
Element lastBuildDate = new Element("lastBuildDate");
lastBuildDate.setText("2011");
channel.addContent(lastBuildDate);
Element ttl = new Element("ttl");
ttl.setText("30");
channel.addContent(ttl);
Element item = new Element("item");
item.addContent(new Element("title").setText("Title"));
item.addContent(new Element("description").setText("Description"));
item.addContent(new Element("pubDate").setText("2011-11-11 11:11:11"));
item.addContent(new Element("link").setText("http://www.example.com/"));
channel.addContent(item);
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
System.out.println(String.format("xmlOutput = %s", xmlOutput.outputString(xml)));
}
}
This will output,
<?xml version="1.0" encoding="UTF-8"?>
<rss version="1.0" encoding="utf-8">
<channel>
<title>This is a title.</title>
<description>This is a description.</description>
<language>en-us</language>
<copyright>2011</copyright>
<lastBuildDate>2011</lastBuildDate>
<ttl>30</ttl>
<item>
<title>Title</title>
<description>Description</description>
<pubDate>2011-11-11 11:11:11</pubDate>
<link>http://www.example.com/</link>
</item>
</channel>
</rss>
<rss version="1.0" encoding="utf-8">
<channel>
<title>This is a title.</title>
<description>This is a description.</description>
<language>en-us</language>
<copyright>2011</copyright>
<lastBuildDate>2011</lastBuildDate>
<ttl>30</ttl>
<item>
<title>Title</title>
<description>Description</description>
<pubDate>2011-11-11 11:11:11</pubDate>
<link>http://www.example.com/</link>
</item>
</channel>
</rss>
References
[Click to add or edit comments])
Please prepend comments below including a date