dom4j解析xml
2014-03-27 21:46
148 查看
package com.minxin.svncontrol; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.tmatesoft.svn.core.SVNDirEntry; import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNNodeKind; import org.tmatesoft.svn.core.SVNProperties; import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory; import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory; import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl; import org.tmatesoft.svn.core.io.SVNRepository; import org.tmatesoft.svn.core.wc.SVNWCUtil; //import org.w3c.dom.Element; /** * SVNManager SVN 管理器 * @author <a href="xiangxji@gmail.com">xiangxji</a> * @since 2010-03-27 */ public class SVNDownlaodFile02{ private String url = "svn://192.168.9.16/minxin/Repositories/minxinloan/branches/code/minxinsalary"; private String username = "gongzaifu"; private String password = "gzf321"; private SVNRepository repository; private List<HashMap> listmap = new ArrayList<HashMap>(); /** * 初始化操作 * @throws Exception */ public void initialize() throws Exception { FSRepositoryFactory.setup(); DAVRepositoryFactory.setup(); SVNRepositoryFactoryImpl.setup(); repository = SVNRepositoryFactoryImpl.create(SVNURL .parseURIEncoded(this.url)); ISVNAuthenticationManager authManager = SVNWCUtil .createDefaultAuthenticationManager(this.username, this.password); repository.setAuthenticationManager(authManager); } /** * 从SVN服务器获取文件 * @param filePath 相对于仓库根目录的路径 * @param outputStream 要输出的目标流,可以是文件流 FileOutputStream * @param version 要checkout的版本号 * @return 返回checkout文件的版本号 * @throws Exception 可以自定义Exception */ public long getFileFromSVN(String filePath, OutputStream outputStream, long version) throws Exception { SVNNodeKind node = null; try { node = repository.checkPath(filePath, version); } catch (SVNException e) { throw new Exception("SVN检测不到该文件:" + filePath, e); } if (node != SVNNodeKind.FILE) { throw new Exception(node.toString() + "不是文件"); } SVNProperties properties = new SVNProperties(); try { repository.getFile(filePath, version, properties, outputStream); } catch (SVNException e) { throw new Exception("获取SVN服务器中的" + filePath + "文件失败", e); } return Long.parseLong(properties.getStringValue("svn:entry:revision")); } /** * 获取目录下的所有文件和子目录 * @param res 包含目录参数的资源对象.参加{@link Resource#getPath()} * @return 资源列表 * @throws Exception */ @SuppressWarnings("unchecked") public List<Resource> getChildren(Resource res) throws Exception { String path = res.getPath(); Collection<SVNDirEntry> entries; try { entries = repository.getDir(path, -1, null, (Collection) null); } catch (SVNException e) { throw new Exception("获得" + path + "下级目录失败", e); } List<Resource> result = new ArrayList<Resource>(); for (SVNDirEntry entry : entries) { if (containsSpecialFile(entry)) { Resource resource = new Resource(); resource.setName(entry.getName()); resource.setPath(entry.getURL().getPath()); resource.setFile(entry.getKind() == SVNNodeKind.FILE); result.add(resource); } } return result; } /** * 判断文件是否存在 * @param entry 要判断的节点.参加{@link SVNDirEntry} * @return * @throws Exception */ @SuppressWarnings("unchecked") private boolean containsSpecialFile(SVNDirEntry entry) throws Exception { if (entry.getKind() == SVNNodeKind.FILE) { return true; } else if (entry.getKind() == SVNNodeKind.DIR) { Collection<SVNDirEntry> entries; String path = entry.getURL().getPath(); try { entries = repository.getDir(path, -1, null, (Collection) null); } catch (SVNException e) { throw new Exception("获得" + path + "下级目录失败", e); } for (SVNDirEntry unit : entries) { if (containsSpecialFile(unit)) { return true; } } return false; } return false; } // public void parserXml(String fileName,HashMap hm) { // try { // DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // DocumentBuilder db = dbf.newDocumentBuilder(); // Document document = db.parse(fileName); // NodeList nl = document.getElementsByTagName("file"); //// NodeList nl2 = document.getElementsByTagName("dir"); // //nl2.item(0).getChildNodes(); // // for (int i = 0; i < nl.getLength(); i++) { // NamedNodeMap nnm = ((Element) nl.item(i)).getAttributes(); // //for (int j = 0; j < nnm.getLength(); j++) { // Node detailNode = nnm.item(0); // Node detailVer = nnm.item(1); // // Node parentNode = nl.item(i).getParentNode(); // parentNode.getNodeValue(); //// System.out.println(detailNode.getNodeName() + " :" //// + detailNode.getNodeValue()); // // if(hm !=null){ // hm.put(detailNode.getNodeValue(),detailVer.getNodeValue()); // //listmap.add(hm); // System.out.println(hm); // } // // //} // // } // System.out.println("解析完毕"); // } catch (Exception e) { // System.out.println(e.getMessage()); // } // } // // @Test public void testGetFile(String s1,String s2) { OutputStream outputStream; try { initialize(); String outFileName = "E:\\test\\" + s1; outputStream = new FileOutputStream(outFileName); //System.out.println(getFileFromSVN("minxinsalary/pom.xml",outputStream,12928L)); System.out.println(getFileFromSVN(s1,outputStream,Long.parseLong(s2))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { HashMap<String, String> hm = new HashMap<String, String>(); SVNDownlaodFile02 svne = new SVNDownlaodFile02(); try { svne.parserXml2("F:\\minxinbalancedfund\\program-svn-list.xml", hm); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } // for(int i = 0 ; i < svne.listmap.size() ; i++){ // svne.testGetFile(svne.listmap.get(i).get("name").toString(),svne.listmap.get(i).get("v").toString()); // } Iterator keySetIterator = hm.keySet().iterator(); while (keySetIterator.hasNext()) { String key = keySetIterator.next().toString(); String value = hm.get(key); svne.testGetFile(key, value); } } public void parserXml2(String fileName,HashMap hm) throws DocumentException{ SAXReader reader = new SAXReader(); Document document = reader.read(new File(fileName)); Element rootElm = document.getRootElement(); Element root1Elm = rootElm.element("source"); List<Element> list = new ArrayList<Element>(); list = SVNDownlaodFile02.getElementsByName((ArrayList) list, root1Elm, "file"); for(int i = 0 ; i < list.size(); i++){ String file = list.get(i).attributeValue("name"); String filev = list.get(i).attributeValue("v"); String dir = list.get(i).getParent().attributeValue("name"); if(dir!= null){ hm.put(dir+"\\"+file, filev); }else{ hm.put(file, filev); } } System.out.println(list.size()); } public static ArrayList getElementsByName(ArrayList elementName,Element top,String name) { if(top.getName().equalsIgnoreCase(name)) { elementName.add(top); } Iterator iter = top.elementIterator(); if (!iter.hasNext()){return elementName;} else { while(iter.hasNext()) { Element sub = (Element)iter.next(); getElementsByName(elementName,sub,name); } } return elementName; } }
相关文章推荐
- dom4j解析xml
- dom4j解析xml字符串 小实例
- 解析XML的 - dom4j 和 jdom
- dom4j 解析 XML 示例
- dom4j解析xml文件
- 使用Dom4j解析XML
- dom4j 解析xml样例代码
- dom4j 解析xml
- 使用 dom4j 解析 XML
- xml解析、dom4j解析
- 使用 dom4j 解析 XML
- 采用DOM4J解析xml,带例子
- java dom4j解析xml
- XML解析之dom4j解析
- dom4j 解析 xml标签属性
- dom4j 创建,解析xml
- java--DOM4j-SAXReader对xml文档的解析2
- jdom (dom4j)解析xml出错、校验dtd文件不生效
- Dom4j 解析 XML文件
- dom4j解析xml字符串实例