XML基础(二)

public static void main(String[] args) {

////////////////////////////////写文件//////////////////////////////////

Document document = DocumentHelper.createDocument(); //创建文档

Element addresslist = document.addElement("addresslist"); //定义节点
Element linkman = addresslist.addElement("linkman"); //定义子节点
Element name = linkman.addElement("name");
Element email = linkman.addElement("email");

name.setText("紫馨");
email.setText("h**i@mail.dlut.edu.cn");

OutputFormat format = OutputFormat.createPrettyPrint(); //设置输出格式
format.setEncoding("GBK"); //指定输出编码
try {
XMLWriter writer = new XMLWriter(
new FileOutputStream("E:" + File.separator + "hello.xml"), format); //向文件输出XML文档
writer.write(document); //输出内容
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

//////////////////////////读文件/////////////////////////

File file = new File("E:" + File.separator + "hello.xml");
SAXReader reader = new SAXReader();
Document doc = null;

try {
doc = reader.read(file);
} catch (DocumentException e) {
e.printStackTrace();
}

Element root = doc.getRootElement();
Iterator iterator = root.elementIterator();

while (iterator.hasNext()) {
Element linkmanE = (Element)iterator.next();
System.out.println("姓名:" + linkman.elementText("name") +
",email:" + linkman.elementText("email"));
}
}


JDOM简介

JDOM = DOM 修改文件的优点 + SAX 读取快速的优点

public static void main(String[] args) {

/////////////////////////写文件///////////////////////////

Element addresslist = new Element("addresslist");
Element linkman = new Element("linkman");
Element name = new Element("name");
Element email = new Element("email");

Attribute id = new Attribute("id", "001");

Document document = new Document(addresslist);

name.setText("紫馨");
email.setText("h**i@mail.dlut.edu.cn");
name.setAttribute(id);
linkman.addContent(name);
linkman.addContent(email);
addresslist.addContent(linkman);

XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(outputter.getFormat().setEncoding("GBK"));

try {
outputter.output(document, new FileOutputStream("E:" + File.separator + "address.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

////////////////////////////读文件/////////////////////////

SAXBuilder builder = new SAXBuilder();
Document readDocument = null;
try {
readDocument = builder.build("E:" + File.separator + "address.xml");
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Element stu = readDocument.getRootElement();
List list = stu.getChildren("linkman");//获取全部linkman子元素
for (int i = 0; i < list.size(); i++) {
Element element = (Element)list.get(i);
String nameStr = element.getChildText("name");
String idStr = element.getChild("name").getAttribute("id").getValue();
String emailStr = element.getChildText("email");

System.out.println("姓名:" + nameStr + ", 编号:" + idStr);
System.out.println("Email:" + emailStr);
}
}

 DOM4J 简介

 

posted @ 2011-10-08 20:40  lihui_yy  阅读(166)  评论(0编辑  收藏  举报