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);