Java面试题目之备忘二十(xml之Jdom)

package com.jdom;

import java.io.FileWriter;

import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class JDOMTest
{

	public static void main(String[] args) throws Exception
	{
		Element root = new Element("hello");

		root.setAttribute("name", "zhangsan").setAttribute("age", "20")
				.setAttribute("sex", "male");

		root.addContent(new Comment("This is a comment"));

		root.addContent("helloworld");

		Element world = new Element("world").setAttribute("address", "China");
		
		world.addContent("This is another element");
		
		root.addContent(world);

		Document document = new Document(root);

		XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());

		outputter.output(document, new FileWriter("jdom.xml"));
	}

}
 

你可能感兴趣的:(java,xml,面试)