protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.doPost(request, response); try { if (!ServletFileUpload.isMultipartContent(request)) { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } ServletFileUpload sfU = new ServletFileUpload(); FileItemIterator items = sfU.getItemIterator(request); while (items.hasNext()) { FileItemStream item = items.next(); if (!item.isFormField()) { InputStream stream = item.openStream(); Document doc = editor.toDocument(stream); stream.close(); handleDocument(request, response, doc); return; } } response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No XML uploaded"); } catch (FileUploadException fuE) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, fuE.getMessage()); } catch (JDOMException jE) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, jE.getMessage()); } }
public ParseDoc(Object xmlFile, String rootId, TreeMap prevIdMap) { SAXBuilder saxBuilder = new SAXBuilder(); try { if (xmlFile instanceof String) { document = saxBuilder.build((String) xmlFile); } else if (xmlFile instanceof URL) { document = saxBuilder.build((URL) xmlFile); } root = ParseUtils.parseRoot(document, rootId); objects = new TreeMap(); // Build description string (but not for the snippets.xml file) if (xmlFile instanceof String) { List authorL = root.getChildren("author"); String author = "<unknown>"; if (root.getAttributeValue("name") != null) { author = root.getAttributeValue("name"); } else if (authorL.size() > 0) { author = ((Element) authorL.get(0)).getValue(); } String description = "from file " + xmlFile.toString() + " (author: " + author + ") on " + new Date().toString(); HasIdentifiers.addGlobalIdentifier("_description_", description); } // Get all macro definitions, and remove them from document TreeMap macroMap = ParseUtils.getMacroDefs(root); // Process all macro expansions; replace macro expansion request with result ParseUtils.expandMacros(root, macroMap); // Get all elements in document, and assign identifiers to them; idMap = ParseUtils.parseId(root, prevIdMap); // Rewriting done; output debug XML code if (root.getAttributeValue("debug") != null) { XMLOutputter outputter = new XMLOutputter(); FileOutputStream fos = new FileOutputStream(xmlFile + ".debug"); outputter.output(document, fos); fos.close(); } } catch (JDOMException e) { // indicates a well-formedness or other error throw new Error("JDOMException: " + e.getMessage()); } catch (IOException e) { // indicates an IO problem throw new Error("IOException: " + e.getMessage()); } }
/** Create a new step from an in-line XML string. */ public static Step createStep(Resolver resolver, String str) throws InvalidScriptException, IOException { StringReader reader = new StringReader(str); try { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(reader); Element el = doc.getRootElement(); return createStep(resolver, el); } catch (JDOMException e) { throw new InvalidScriptException(e.getMessage()); } }
private Document getXMLDocument(InputStream is) { SAXBuilder builder = new SAXBuilder(); builder.setValidation(false); builder.setIgnoringElementContentWhitespace(true); Document doc = null; try { doc = builder.build(is); } catch (JDOMException e) { e.printStackTrace(); System.exit(-1); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } return doc; }
private void openFileMenuItemWidgetSelected(SelectionEvent evt) { FileDialog dialog = new FileDialog(getShell(), SWT.OPEN); String filename = dialog.open(); if (filename != null) { getShell().setText(filename); File file = new File(filename); XML.load(file); try { XML.Match(); setStatus(XML.Output); } catch (JDOMException e) { e.printStackTrace(); } } }
/** * * @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(); } }
/** * Notifies the registered {@link ErrorHandler SAX error handler} (if any) of an input processing * error. The error handler can choose to absorb the error and let the processing continue. * * @param exception <code>JDOMException</code> containing the error information; will be wrapped * in a {@link SAXParseException} when reported to the SAX error handler. * @throws JDOMException if no error handler has been registered or if the error handler fired a * {@link SAXException}. */ private void handleError(JDOMException exception) throws JDOMException { if (errorHandler != null) { try { errorHandler.error(new SAXParseException(exception.getMessage(), null, exception)); } catch (SAXException se) { if (se.getException() instanceof JDOMException) { throw (JDOMException) (se.getException()); } throw new JDOMException(se.getMessage(), se); } } else { throw exception; } }
//改写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"); } }