dom4j是1.6.1版本,jdom是1.1版,代碼沒什么技術含量,就是API的使用而已,所以沒加注釋。
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Dom4JHandle {
private static final String xmlString = "<books><book price=\"108\"><name>Java編程思想</name><author>Bruce Eckel</author></book><book price=\"52\"><name>Effective Java</name><author>Joshua Bloch</author></book><book price=\"118\"><name>Java 7入門經典</name><author>Ivor Horton</author></book></books>";
public static void main(String[] args) {
parseXml(xmlString);
assembleXml();
}
public static void parseXml(String xmlString) {
try {
Document doc = DocumentHelper.parseText(xmlString);
Element root = doc.getRootElement();
@SuppressWarnings("unchecked")
List<Element> elements = root.elements();
for(Element element : elements) {
System.out.println("name=" + element.elementText("name") + "\tauthor=" + element.elementText("author")
+ "\tprice=" + element.attributeValue("price"));
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static String assembleXml() {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("books");
Element book = root.addElement("book");
book.addAttribute("price", "108");
book.addElement("name").setText("Java編程思想");
book.addElement("author").setText("Bruce Eckel");
book = root.addElement("book");
book.addAttribute("price", "52");
book.addElement("name").setText("Effective Java");
book.addElement("author").setText("Joshua Bloch");
book = root.addElement("book");
book.addAttribute("price", "118");
book.addElement("name").setText("Java 7入門經典");
book.addElement("author").setText("Ivor Horton");
OutputFormat format = OutputFormat.createCompactFormat(); //createPrettyPrint() 層次格式化
StringWriter writer = new StringWriter();
XMLWriter output = new XMLWriter(writer, format);
try {
output.write(doc);
writer.close();
output.close();
System.out.println(writer.toString());
} catch (IOException e) {
e.printStackTrace();
return null;
}
return writer.toString();
}
}
dom4j還可以配合xpath一起使用,一般來說用不上,不過xml比較復雜,而需求又比較特殊的時候就需要它來配合了,不過不用完全記住那些規(guī)則,可以去w3cschool網站在線查詢:
import java.io.StringWriter;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Dom4JHandle {
private static final String xmlString = "<books><book price=\"108\"><name>Java編程思想</name><author>Bruce Eckel</author></book><book price=\"52\"><name>Effective Java</name><author>Joshua Bloch</author></book><book price=\"118\"><name>Java 7入門經典</name><author>Ivor Horton</author></book></books>";
public static void main(String[] args) {
parseXml(xmlString);
assembleXml();
}
public static void parseXml(String xmlString) {
try {
Document doc = DocumentHelper.parseText(xmlString);
Element root = doc.getRootElement();
@SuppressWarnings("unchecked")
List<Element> elements = root.elements();
for(Element element : elements) {
System.out.println("name=" + element.elementText("name") + "\tauthor=" + element.elementText("author")
+ "\tprice=" + element.attributeValue("price"));
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static String assembleXml() {
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("books");
Element book = root.addElement("book");
book.addAttribute("price", "108");
book.addElement("name").setText("Java編程思想");
book.addElement("author").setText("Bruce Eckel");
book = root.addElement("book");
book.addAttribute("price", "52");
book.addElement("name").setText("Effective Java");
book.addElement("author").setText("Joshua Bloch");
book = root.addElement("book");
book.addAttribute("price", "118");
book.addElement("name").setText("Java 7入門經典");
book.addElement("author").setText("Ivor Horton");
OutputFormat format = OutputFormat.createCompactFormat(); //createPrettyPrint() 層次格式化
StringWriter writer = new StringWriter();
XMLWriter output = new XMLWriter(writer, format);
try {
output.write(doc);
writer.close();
output.close();
System.out.println(writer.toString());
} catch (IOException e) {
e.printStackTrace();
return null;
}
return writer.toString();
}
}
import java.io.StringReader;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class JdomHandle {
private static final String xmlString = "<books><book price=\"108\"><name>Java編程思想</name><author>Bruce Eckel</author></book><book price=\"52\"><name>Effective Java</name><author>Joshua Bloch</author></book><book price=\"118\"><name>Java 7入門經典</name><author>Ivor Horton</author></book></books>";
public static void main(String[] args) {
parseXml(xmlString);
assembleXml();
}
public static void parseXml(String xmlString) {
SAXBuilder builder = new SAXBuilder();
Document doc = null;
try {
doc = builder.build(new StringReader(xmlString));
Element root = doc.getRootElement(); // 根元素
@SuppressWarnings("unchecked")
List<Element> list = root.getChildren();
if (list != null) {
for (Element element : list) {
System.out.println("name=" + element.getChildText("name") + "\tauthor="
+ element.getChildText("author") + "\tprice=" + element.getAttributeValue("price"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String assembleXml() {
Document doc = new Document();
Element root = new Element("books");
Element book = new Element("book");
Element name = new Element("name");
Element author = new Element("author");
book.setAttribute("price", "108");
name.addContent("Java編程思想");
author.addContent("Bruce Eckel");
book.addContent(name);
book.addContent(author);
root.addContent(book);
book = new Element("book");
name = new Element("name");
author = new Element("author");
book.setAttribute("price", "52");
name.addContent("Effective Java");
author.addContent("Joshua Bloch");
book.addContent(name);
book.addContent(author);
root.addContent(book);
book = new Element("book");
name = new Element("name");
author = new Element("author");
name.addContent("Java 7入門經典");
author.addContent("Ivor Horton");
book.setAttribute("price", "118");
book.addContent(name);
book.addContent(author);
root.addContent(book);
doc.addContent(root);
XMLOutputter xo = new XMLOutputter(Format.getCompactFormat()); // getPrettyFormat() 層次格式化
System.out.println(xo.outputString(doc));
return xo.outputString(doc);
}
}
本文為菠蘿大象原創(chuàng),如要轉載請注明出處。http://www.aygfsteel.com/bolo
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class JdomHandle {
private static final String xmlString = "<books><book price=\"108\"><name>Java編程思想</name><author>Bruce Eckel</author></book><book price=\"52\"><name>Effective Java</name><author>Joshua Bloch</author></book><book price=\"118\"><name>Java 7入門經典</name><author>Ivor Horton</author></book></books>";
public static void main(String[] args) {
parseXml(xmlString);
assembleXml();
}
public static void parseXml(String xmlString) {
SAXBuilder builder = new SAXBuilder();
Document doc = null;
try {
doc = builder.build(new StringReader(xmlString));
Element root = doc.getRootElement(); // 根元素
@SuppressWarnings("unchecked")
List<Element> list = root.getChildren();
if (list != null) {
for (Element element : list) {
System.out.println("name=" + element.getChildText("name") + "\tauthor="
+ element.getChildText("author") + "\tprice=" + element.getAttributeValue("price"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String assembleXml() {
Document doc = new Document();
Element root = new Element("books");
Element book = new Element("book");
Element name = new Element("name");
Element author = new Element("author");
book.setAttribute("price", "108");
name.addContent("Java編程思想");
author.addContent("Bruce Eckel");
book.addContent(name);
book.addContent(author);
root.addContent(book);
book = new Element("book");
name = new Element("name");
author = new Element("author");
book.setAttribute("price", "52");
name.addContent("Effective Java");
author.addContent("Joshua Bloch");
book.addContent(name);
book.addContent(author);
root.addContent(book);
book = new Element("book");
name = new Element("name");
author = new Element("author");
name.addContent("Java 7入門經典");
author.addContent("Ivor Horton");
book.setAttribute("price", "118");
book.addContent(name);
book.addContent(author);
root.addContent(book);
doc.addContent(root);
XMLOutputter xo = new XMLOutputter(Format.getCompactFormat()); // getPrettyFormat() 層次格式化
System.out.println(xo.outputString(doc));
return xo.outputString(doc);
}
}