/** * Create a new AbstractWriter with the indicated Writer and Element. The full range of the * Element will be used. The range will be limited to the indicated range of the Document. */ protected AbstractWriter(Writer writer, Element elt, int pos, int len) { this.writer = writer; this.iter = new ElementIterator(elt); this.document = elt.getDocument(); this.startOffset = pos; this.endOffset = pos + len; }
/** * Create a new AbstractWriter with the indicated Writer and Element. The full range of the * Element will be used. */ protected AbstractWriter(Writer writer, Element elt) { this.writer = writer; this.iter = new ElementIterator(elt); this.document = elt.getDocument(); this.startOffset = elt.getStartOffset(); this.endOffset = elt.getEndOffset(); }
public static void saveCourseCreerToXml(Evenement event) { Element elementEvent = JDomOperations.getElementActuel_(); Element listCourses = elementEvent.getChild("listcourses"); Iterator itListCoursesInEvent = event.getIteratorCollectionEssaiCourse_(); while (itListCoursesInEvent.hasNext()) { EssaiOrCourse course = (EssaiOrCourse) itListCoursesInEvent.next(); Element elementCourse = new Element("course"); elementCourse.setAttribute(new Attribute("nomEssaiOrCourse", course.getNomEssaiOrCourse_())); elementCourse.setAttribute( new Attribute("typeEssaiOrCourse", course.getTypeEssaiOrCourse_().toString())); listCourses.addContent(elementCourse); } JDomOperations.enregistre(elementEvent.getDocument(), xmlDefaultPath); }
/** * * @param 普通版list 参数列表 */ public void writeptParaList(ArrayList list) { try { String sCaseName=""; String sItem=""; String sCycName=""; HashMap map=null; Document doc = builder.build(xmlFileName); Element root = doc.getRootElement(); Element element = root; Element cycNode=null; map=(HashMap)list.get(0); //参数文件map sItem="Description"; sCaseName=(String)map.get(sItem); mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "Description=" + sCaseName ); sItem="CysName"; sCycName=(String)map.get(sItem); map=(HashMap)list.get(1); //public 参数map cycNode=writeptPublicNode(element,map,sCycName); if( writeCycNode(cycNode,list)==false) //循环 参数 System.err.println(" write cyc node fail"); XMLOutputter outputter = new XMLOutputter("", false, "GB2312"); PrintWriter out = new PrintWriter(new BufferedWriter( new FileWriter(xmlFileName))); Document myDocument = root.getDocument(); outputter.output(myDocument, out); out.close(); } catch (JDOMException jdome) { jdome.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
//改写XML文件的某一元素的值 //strRoute为XML文件从根元素开始到该元素的父元素的路径 //strElement为需要读取的元素 //flag为当XML含有多个同名元素时,需要改写元素所处的序号 public void write(String strRoute, String strElement, String strSet, int flag) { //SAXBuilder builder=new SAXBuilder(); try { String str = null; Document doc = builder.build(xmlFileName); Element root = doc.getRootElement(); Element element = root; StringTokenizer st = new StringTokenizer(strRoute, ":"); str = st.nextToken(); while (st.hasMoreTokens()) { str = st.nextToken(); //mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,str); element = element.getChild(str); } // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"test :"+str); element = (Element) element.getParent(); //若需要改写的不是XML文件的第一个同名元素,则获取该元素 //方法为:将之前的元素依次移到后面,需要改写的元素前移至第一个 /* * if(flag>1) { int j=flag; while(j!=1) { * * Element tmp=element.getChild(str); * element.addContent(tmp.detach()); j--; } } */ element = element.getChild(str).getChild(strElement); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"gettxt // "+element.getText()); element.setText(strSet); //若需要改写的不是XML文件的第一个同名元素 //上面改变了次序,需要在成功改写后恢复原有次序 /* * if(flag!=1) { * * java.util.List children=element.getChildren(); Iterator * iterator=children.iterator(); int count=0; * while(iterator.hasNext()) { Element * child=(Element)iterator.next(); count++; } * * System.out.println("count"+count); * * k=(count+1-flag)%count; * * while(k!=0) { Element tmp=element.getChild(str); * element.addContent(tmp.detach()); k--; } } * */ XMLOutputter outputter = new XMLOutputter("", false, "GB2312"); PrintWriter out = new PrintWriter(new BufferedWriter( new FileWriter(xmlFileName))); Document myDocument = root.getDocument(); outputter.output(myDocument, out); out.close(); } catch (JDOMException jdome) { jdome.printStackTrace(); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, xmlFileName // + " is not well-formed\n"); } catch (IOException ioe) { ioe.printStackTrace(); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, ioe); } catch (Exception e) { e.printStackTrace(); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, // "write not succeed\n"); } }
/** * Writes out all empty elements (all tags that have no corresponding end tag). * * @param elem an Element * @exception IOException on any I/O error * @exception BadLocationException if pos represents an invalid location within the document. */ protected void emptyTag(Element elem) throws BadLocationException, IOException { if (!inContent && !inPre) { indent(); } AttributeSet attr = elem.getAttributes(); closeOutUnwantedEmbeddedTags(attr); writeEmbeddedTags(attr); if (matchNameAttribute(attr, HTML.Tag.CONTENT)) { inContent = true; text(elem); } else if (matchNameAttribute(attr, HTML.Tag.COMMENT)) { comment(elem); } else { boolean isBlock = isBlockTag(elem.getAttributes()); if (inContent && isBlock) { writeLineSeparator(); indent(); } Object nameTag = (attr != null) ? attr.getAttribute(StyleConstants.NameAttribute) : null; Object endTag = (attr != null) ? attr.getAttribute(HTML.Attribute.ENDTAG) : null; boolean outputEndTag = false; // If an instance of an UNKNOWN Tag, or an instance of a // tag that is only visible during editing // if (nameTag != null && endTag != null && (endTag instanceof String) && ((String) endTag).equals("true")) { outputEndTag = true; } if (completeDoc && matchNameAttribute(attr, HTML.Tag.HEAD)) { if (outputEndTag) { // Write out any styles. writeStyles(((HTMLDocument) getDocument()).getStyleSheet()); } wroteHead = true; } write('<'); if (outputEndTag) { write('/'); } write(elem.getName()); writeAttributes(attr); write('>'); if (matchNameAttribute(attr, HTML.Tag.TITLE) && !outputEndTag) { Document doc = elem.getDocument(); String title = (String) doc.getProperty(Document.TitleProperty); write(title); } else if (!inContent || isBlock) { writeLineSeparator(); if (isBlock && inContent) { indent(); } } } }