首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Dom4J创造xml文件的步骤

2012-11-03 
Dom4J创建xml文件的步骤public static voidcreateDoc(String filename){/** 建立document对象 */Document

Dom4J创建xml文件的步骤

public static void  createDoc(String filename){/** 建立document对象 */  Document document = DocumentHelper.createDocument();  document.addElement("ADI");//创建根元素  Element rootElement = document.getRootElement();  rootElement.addAttribute(rootAttribute, rootAttributeValue);  rootElement.addElement("Objects");  rootElement.addElement("Mappings");  try {   /** 将document中的内容写入文件中--装饰者设计模式*/   File file = new File(filename);   FileWriter fw = new FileWriter(file);   XMLWriter writer = new XMLWriter(fw);//dom4j的writer   writer.write(document);   writer.close();  } catch (Exception ex) {  ex.printStackTrace();  }}




 /**  *   *   @Description 获取XML文件中的信息,如果文件存在,返回document,如果不存在,返回null  *   @param filepath  *   @return  *   Document  *   @throws  抛出异常说明  */ public static Document getDoc(String filepath) {  Document document = null;  SAXReader reader = new SAXReader();  try {   File file = new File(filepath);   if(file.exists()){    document = reader.read(file);    }else{    return null;   }     } catch (Exception e) {      }  return document; }


public static Element getObjects(Document doc){Element rootElement = doc.getRootElement();Element objects = rootElement.element("Objects");return objects;}
Element object = objects.addElement("Object");object.addAttribute("ElementType", pro.getElementType());object.addAttribute("ID", pro.getCode());object.addAttribute("Action", Actions.getAction(actionType));


Element nameProperty = obj.addElement("Property");nameProperty.addAttribute("Name", name);nameProperty.addText(value);

热点排行