dom4j解析xml

package com.it.huawei.xml;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ElementAnalyzer {

	public void read(String path,String jobPath) {
		try {
			List<Element> jobs = getAllJobs(jobPath);
			SAXReader sax = new SAXReader();
			Document doc = sax.read(new File(path));
			Element defTable = doc.getRootElement();
			Element cloneDefTable = cloneElement(defTable,jobs);// def_table,删除其子节点
			 
		 
			System.out.println("new:"+cloneDefTable.asXML());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * s
	 * @param element
	 * @param nodeName
	 * @return
	 */
	public Element cloneElement(Element element,List<Element> jobs) {
		Element node = (Element) element.clone();
		try {
			Element smartFolder = node.element(XmlConstants.SMART_FOLDER);
					Iterator childIter = smartFolder.elementIterator();
					while(childIter.hasNext()) { 
						Element child= (Element)childIter.next();
						if(child.getName().equals(XmlConstants.SUB_FOLDER)){
							cloneElements(child,jobs);
							List list = child.elements();
							boolean bl = false;
							if(list.size() >0){
							bl = true;	
							}
							if(!bl){
								System.out.println("======第一层subFolder节点:"+child.getName()+"folderName:"+child.attributeValue(XmlConstants.FOLDER_NAME)+"被删除");
								smartFolder.remove(child);
							}
						}
					}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return node;
	}
	
	/**
	 * 判断当前节点下面子节点存在JOB节点与list容易内相同JOBNAME,添加改节点及JOB节点,否则不添加改节点
	 * 
	 * @param element
	 * @param nodeName
	 * @return
	 */
	public Element cloneElements(Element element,List<Element> jobs) {
		try {
			 Iterator subFolder = element.elementIterator();
			 boolean bl = false;
			 while(subFolder.hasNext()) {
				 Element child = (Element)subFolder.next();
				 if(child.getName().equals(XmlConstants.JOB)){
					 String jobName = child.attributeValue(XmlConstants.JOBNAME);
					 boolean flag = false;
					 for(Element job: jobs) {
						 System.out.println("比较"+jobName+":"+job.attributeValue(XmlConstants.JOBNAME));
						 if(jobName.equals(job.attributeValue(XmlConstants.JOBNAME))){
							 flag = true;
							 bl = true;
							 jobs.remove(job);
							 break;
						 } 
					 }
					 if(!flag){
						 System.out.println("======节点:"+child.getName()+"jobName:"+child.attributeValue(XmlConstants.JOBNAME)+"被删除");
						 element.remove(child);
					 }
				 }else if(child.getName().equals(XmlConstants.SUB_FOLDER)){
					 return cloneElements(child,jobs);
				 }
			 }
			 if(!bl) {
				 System.out.println("======父节点节点:"+element.getName()+"jobName:"+element.attributeValue(XmlConstants.JOBNAME)+"被删除");
				element.getParent().remove(element);
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return element;
	}
	
	/**
	 * 获取所有JOB节点
	 * @param path
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Element> getAllJobs(String path) {
		List<Element> jobs = new ArrayList<Element>();
		
		try {
			SAXReader sax = new SAXReader();
			Document doc = sax.read(new File(path));
			Element root = doc.getRootElement();
			Element smartFolder = root.element(XmlConstants.SMART_FOLDER);
			jobs =  smartFolder.elements(XmlConstants.JOB);
			for(Element job:jobs){
				System.out.println("jobname:"+job.attributeValue(XmlConstants.JOBNAME));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jobs;
	}
}



package com.it.huawei.xml;

public class Test {
	public static final String EXPORT_FIRST_PATH = "d:/nasdata/test/";
	public static final String EXPORT_SECOND_PATH = "D:/nasdata/test/";
	
	public static void main(String[] args) {
		ElementAnalyzer ea = new ElementAnalyzer();
		ea.read(EXPORT_FIRST_PATH+"335.xml",EXPORT_FIRST_PATH+"789.xml");
		
	}

}


package com.it.huawei.xml;

public class XmlConstants {
	public static final String JOB = "JOB";
	public static final String DEFTABLE = "DEF_TABLE";
	public static final String JOBNAME = "JOBNAME";
	public static final String PARENT_FOLDER = "PARENT_FOLDER";
	public static final String SMART_FOLDER = "SMART_FOLDER";
	public static final String SUB_FOLDER = "SUB_FOLDER";
	public static final String FOLDER_NAME = "FOLDER_NAME";
}

你可能感兴趣的:(dom4j解析xml)