/***@authorhuangjing
* @date 2014-2-13*/
public class Dom4j implementsXmlDocument {
@Overridepublic voidcreateXml(String fileName) {
Document document=DocumentHelper.createDocument();
Element employees= document.addElement("employees");
Element employee= employees.addElement("employee");
Element name= employee.addElement("name");
name.setText("dddvip");
Element sex= employee.addElement("sex");
sex.setText("f");
Element age= employee.addElement("age");
age.setText("29");try{/**格式化输出,类型IE浏览一样*/OutputFormat format =OutputFormat.createPrettyPrint();
Writer fileWriter= newFileWriter(fileName);
XMLWriter xmlWriter= newXMLWriter(fileWriter,format);
xmlWriter.write(document);
xmlWriter.close();
}catch(IOException e) {
System.out.println(e.getMessage());
}
}
@Overridepublic voidparserXml(String fileName) {
File inputXml= newFile(fileName);
SAXReader saxReader= newSAXReader();try{
Document document=saxReader.read(inputXml);
Element employees=document.getRootElement();for (Iterator i =employees.elementIterator(); i.hasNext();) {
Element employee=(Element) i.next();for (Iterator j =employee.elementIterator(); j.hasNext();) {
Element node=(Element) j.next();
System.out.println(node.getName()+ ":" +node.getText());
}
}
}catch(DocumentException e) {
System.out.println(e.getMessage());
}
System.out.println("dom4j parserXml");
}
@OverridepublicMap parserSmsXml(String sms) {
Map map= newHashMap();//SAXReader saxReader = new SAXReader();
try{
Document document=DocumentHelper.parseText(sms); Element returnsms=document.getRootElement();for (Iterator i =returnsms.elementIterator(); i.hasNext();) {//Element employee = (Element) i.next();//for (Iterator j = employee.elementIterator(); j.hasNext();) {
Element node =(Element) i.next();//System.out.println(node.getName() + ":" + node.getText());
map.put(node.getName(), node.getText());//}
}
}catch(DocumentException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}returnmap;
}
}