Jdom
package org.zbq;
import java.io.File;
import java.io.FileOutputStream;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class JDomOutput {
public static void main(String[] args) throws Exception {
Document document = new Document();
Element students = new Element("students");
Element student = new Element("student");
Element name = new Element("name");
Element age = new Element("age");
Attribute id = new Attribute("id", "091150101");
document.addContent(students);
students.addContent(student);
student.addContent(name);
name.setAttribute(id);
student.addContent(age);
name.setText("Tom");
age.setText("21");
Element studnet2 = (Element) student.clone();
students.addContent(studnet2);
FileOutputStream outFile = new FileOutputStream(new File("./student.xml")) ;
XMLOutputter output = new XMLOutputter();
output.setFormat(Format.getPrettyFormat());
output.output(document, outFile);
}
}
package org.zbq;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class JDomSAX {
public static void main(String[] args) throws Exception, IOException {
SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new File("./student.xml"));
Element students = document.getRootElement();
@SuppressWarnings("unchecked")
List<Element> list = students.getChildren("student");
System.out.println(list);
for(Element student : list){
Element name = student.getChild("name");
Element age = student.getChild("age");
Attribute id = name.getAttribute("id");
System.out.println("name:" + name.getText() + " age:" + age.getText()
+ " id:" + id.getValue());
}
}
}
Dom4j
package org.zbq;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Dom4jOutput {
public static void main(String[] args) throws IOException {
Document document = DocumentHelper.createDocument();
Element books = document.addElement("books");
Element book = books.addElement("book");
book.addAttribute("ISNB", "0120112");
Element title = book.addElement("title");
title.setText("Herry Python");
Element author = book.addElement("author");
author.setText("JK roling");
Element price = book.addElement("price");
price.setText("16.3");
XMLWriter writer = new XMLWriter(new FileWriter(new File("./books.xml")), new OutputFormat(" ", true));
writer.write(document);
writer.close();
}
}
package org.zbq;
import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jSAX {
public static void main(String[] args) throws Exception {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("./books.xml"));
Element books = document.getRootElement();
@SuppressWarnings("unchecked")
Iterator<Element> iter = books.elementIterator("book");
for(; iter.hasNext();){
// iter.next();
Element book = iter.next(); //books.element("book");
Element title = book.element("title");
Element author = book.element("author");
Element price = book.element("price");
System.out.println("title:" + title.getText() + " author:" + author.getText()
+ " price:" + price.getText());
}
System.out.println(document.asXML());
}
}