List of usage examples for org.dom4j Document nodeIterator
Iterator<Node> nodeIterator();
From source file:com.noterik.bart.fs.cloudexporter.DiskExporter.java
License:Open Source License
private static boolean exportNodes(String exporturl, String exportpath) { // if (exporturl.indexOf("/user")!=-1) return true; //Skip backing up the indexes /*/* w ww. j av a 2s . c om*/ if (exporturl.indexOf("/collectionindex")!=-1) return true; if (exporturl.indexOf("/keywordindex")!=-1) return true; if (exporturl.indexOf("/subtitlesindex")!=-1) return true; if (exporturl.indexOf("/speakersindex")!=-1) return true; if (exporturl.indexOf("/themesindex")!=-1) return true; if (exporturl.indexOf("/topicsearch")!=-1) return true; */ System.out.println("(" + (count++) + ") URL=" + exporturl); File dirs = new File(exportpath); boolean result = dirs.mkdirs(); if (dirs.exists()) { result = true; } if (!result) return false; Document exportXML = FSXMLRequestHandler.instance().handleDocGET(exporturl, "<properties><depth>0</depth></properties>"); if (exportXML != null) { for (Iterator i = exportXML.nodeIterator(); i.hasNext();) { Element fsnode = (Element) i.next(); if (fsnode.getName().equals("error")) return true; // Check if the current node is <error> for (Iterator j = fsnode.nodeIterator(); j.hasNext();) { Element mnode = (Element) j.next(); for (Iterator k = mnode.nodeIterator(); k.hasNext();) { Element dnode = (Element) k.next(); String name = dnode.getName(); String id = dnode.attributeValue("id"); //System.out.println("DNAME="+name); if (Arrays.asList(ignorelist).contains(name)) { System.out.println("ignoring " + name); } else if (!name.equals("properties")) { exportNodes(exporturl + "/" + name + "/" + id, exportpath + "/" + name + "/" + id); } else { try { MimeType mimetype = FSXMLRequestHandler.instance().getMimeTypeOfResource(exporturl); BufferedWriter propfile = new BufferedWriter( new FileWriter(exportpath + "/properties.txt")); String propbody = "<properties>\n"; // set the mimetype propbody += "\t<mimetype>"; if (mimetype.toString().equals("MIMETYPE_FS_SCRIPT")) { propbody += "application/fsscript"; } else if (mimetype.toString().equals("MIMETYPE_FS_COMMAND")) { propbody += "application/fscommand"; } else { propbody += "text/fsxml"; } propbody += "</mimetype>\n"; for (Iterator l = dnode.nodeIterator(); l.hasNext();) { Object p = l.next(); if (p instanceof Element) { Element pnode = (Element) p; String pname = pnode.getName(); String pvalue = pnode.getText(); propbody += "\t<" + pname + ">" + pvalue + "</" + pname + ">\n"; } else if (p instanceof DefaultText) { DefaultText tnode = (DefaultText) p; //System.out.println("DEFTEXT="+tnode.toString()); String pname = tnode.getName(); String pvalue = tnode.getText(); //propbody += "\t<"+pname+">"+pvalue+"</"+pname+">\n"; } else if (p instanceof DefaultComment) { DefaultComment cnode = (DefaultComment) p; String pname = cnode.getName(); String pvalue = cnode.getText(); //propbody += "\t<!-- <"+pname+">"+pvalue+"</"+pname+"> -->\n"; //System.out.println("DEFCOMMENT="+cnode.toString()); } else { System.out.println("UNCATCHED TYPE=" + p.toString()); } } propbody += "</properties>\n"; propfile.write(propbody); propfile.close(); } catch (Exception e) { System.out.println("PROP ERROR"); e.printStackTrace(); } } if (mnode.attributeValue("id") != null && mnode.attributeValue("referid") != null && !name.equals("user")) { try { System.out.println("MNODE2=" + mnode.asXML()); BufferedWriter attrfile = new BufferedWriter( new FileWriter(exportpath + "/attributes.txt")); String attrbody = "<attributes>\n"; attrbody += "\t<referid>" + mnode.attributeValue("referid") + "</referid>\n"; attrbody += "</attributes>\n"; attrfile.write(attrbody); attrfile.close(); } catch (Exception e) { e.printStackTrace(); } } } } } return true; } else { System.out.println("EXPORT URL NOT VALID (" + exporturl + ")"); return false; } }
From source file:fr.gouv.culture.vitam.utils.XmlDom.java
License:Open Source License
public final static void removeEmptyDocument(Document doc) { @SuppressWarnings("unchecked") Iterator<Node> nodes = doc.nodeIterator(); while (nodes.hasNext()) { Node node = (Node) nodes.next(); if (node instanceof Element) { removeEmptyElement((Element) node); } else if (node instanceof Attribute) { removeEmptyAttribute((Attribute) node); }/*from www . j a va 2s .c om*/ } }
From source file:fr.gouv.vitam.xml.XmlDom4jTools.java
License:Open Source License
public final static void removeEmptyDocument(Document doc) { @SuppressWarnings("unchecked") Iterator<Node> nodes = doc.nodeIterator(); List<Attribute> toremove = new ArrayList<>(); while (nodes.hasNext()) { Node node = (Node) nodes.next(); if (node instanceof Element) { removeEmptyElement((Element) node); } else if (node instanceof Attribute) { if (((Attribute) node).getValue().length() == 0) { toremove.add(((Attribute) node)); }/*from ww w . j av a 2 s . co m*/ //removeEmptyAttribute((Attribute) node); } } for (Attribute attribute : toremove) { doc.remove(attribute); } toremove.clear(); }
From source file:io.mashin.oep.hpdl.XMLReadUtils.java
License:Open Source License
public static HashMap<String, Point> graphicalInfoFrom(Document document) { HashMap<String, Point> graphicalInfoMap = new HashMap<String, Point>(); try {//from www .jav a2 s. c om SAXReader reader = new SAXReader(); Pattern p = Pattern.compile("\\s*<workflow>.*</workflow>\\s*", Pattern.DOTALL); @SuppressWarnings("unchecked") Iterator<Node> iter = document.nodeIterator(); while (iter.hasNext()) { Node xmlNode = iter.next(); if (xmlNode.getNodeType() == Node.COMMENT_NODE) { String graphicalInfo = xmlNode.getText(); if (p.matcher(graphicalInfo).find()) { Element graphicalElement = reader.read(new StringReader(graphicalInfo)).getRootElement(); @SuppressWarnings("unchecked") Iterator<Node> gIter = graphicalElement.nodeIterator(); while (gIter.hasNext()) { Node gNode = gIter.next(); if (gNode.getName() != null && gNode.getName().equals("node")) { graphicalInfoMap.put(gNode.valueOf("@name"), new Point(Integer.parseInt(gNode.valueOf("@x")), Integer.parseInt(gNode.valueOf("@y")))); } } break; } } } } catch (DocumentException ex) { ex.printStackTrace(); } return graphicalInfoMap; }
From source file:net.sf.cpsolver.studentsct.GetInfo.java
License:Open Source License
public static void getInfo(File folder, List<Info> infos, String prefix) { File infoFile = new File(folder, "info.xml"); if (infoFile.exists()) { System.out.println("Reading " + infoFile + " ..."); try {/*from w w w . j ava 2 s .co m*/ Document document = (new SAXReader()).read(infoFile); HashMap<String, String> info = getInfo(document.getRootElement()); if (info != null && !info.isEmpty()) { infos.add(new Info(prefix, info)); return; } } catch (Exception e) { System.err.println("Error reading file " + infoFile + ", message: " + e.getMessage()); } } File outputFile = new File(folder, "output.csv"); if (outputFile.exists()) { System.out.println("Reading " + outputFile + " ..."); try { HashMap<String, String> info = getInfo(outputFile); if (info != null && !info.isEmpty()) { infos.add(new Info(prefix, info)); return; } } catch (Exception e) { System.err.println("Error reading file " + infoFile + ", message: " + e.getMessage()); } } File solutionFile = new File(folder, "solution.xml"); if (!solutionFile.exists()) return; try { System.out.println("Reading " + solutionFile + " ..."); Document document = (new SAXReader()).read(solutionFile); for (Iterator<?> i = document.nodeIterator(); i.hasNext();) { Node node = (Node) i.next(); if (node instanceof Comment) { Comment comment = (Comment) node; if (comment.getText().indexOf("Solution Info:") >= 0) { HashMap<String, String> info = getInfo(comment.getText()); if (info != null) infos.add(new Info(prefix, info)); } } } } catch (Exception e) { System.err.println("Error reading file " + solutionFile + ", message: " + e.getMessage()); } }