Dom4j递归删除

递归查找XML节点,如果节点属性符合要求,保留该节点以及所有父亲节点;如果节点以及子节点都不符合要求,全部删除。

 

package test.mytest;

import java.util.List;

import org.apache.ibatis.io.Resources;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class XMLHandle {

	private boolean walkChildElement = true;
	private boolean exclusiveElement = true;
	private boolean hasElement=true;

	public String  getXMLString(List<String> datalist) throws Exception {

		

		SAXReader saxReader = new SAXReader();
		Document document = saxReader.read(Resources
				.getResourceAsStream("list.xml"));

		Element tElement = (Element) document.selectSingleNode("/root");
		treeWalk(tElement, datalist);
		return tElement.asXML();
		

	}

	private void treeWalk(Element element, List<String> datalist) {

		for (int i = 0, size = element.nodeCount(); i < size; i++) {
			walkChildElement = true;
			exclusiveElement = true;
			Node node = element.node(i);
			if (node instanceof Element) {

				Element childelement = (Element) node;
				
				boolean hasContent = node.hasContent();

				System.out.println(childelement.attribute("i").getValue());
				for (String str : datalist) {
					if (childelement.attribute(0).getValue().equals(str)) {

						walkChildElement = false;
						exclusiveElement = false;

					}
				}
				if (walkChildElement && hasContent) {
					treeWalk((Element) node, datalist);
				}
				if (exclusiveElement && !hasContent) {
					System.out.println("!hasContent:  "
							+ childelement.attribute("i").getValue());
					element.remove(childelement);
					i--;
				}

			}
		}
		hasElement = element.hasContent();
		if (element.isTextOnly()) {
			hasElement = false;
		}
		if (!hasElement) {
			//System.out.println("content: " + element.attribute("i").getValue());
			if(!element.isRootElement()){
				element.getParent().remove(element);
			}
			
		}

	}

}

 

package test.mytest;

import java.util.ArrayList;
import java.util.List;

public class Test {

	public static void main(String[] args) throws Exception {
		List<String> datalist = new ArrayList<String>();
		datalist.add("item1");
		datalist.add("item2");
		datalist.add("item3");
		datalist.add("item4");
		XMLHandle xmlHandle=new XMLHandle();
		String xmlString=xmlHandle.getXMLString(datalist);
		System.out.println(xmlString);
	}
}

 

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <n i="o1">
        <n i="one1">
            <n i="item1"></n>
            <n i="item5"></n>
            <n i="item6"></n>
            <n i="item7"></n>
        </n>
    </n>
    <n i="o2">
        <n i="one2">
            <n i="item8"></n>
            <n i="item9"></n>
            <n i="item10"></n>
            <n i="item11"></n>
        </n>
    </n>
    <n i="o3">
        <n i="item3">
            <n i="item8"></n> 
            <n i="item9"></n>
            <n i="item10"></n>
            <n i="item11"></n>
        </n>
    </n>
     <n i="o4">
        <n i="item">
            <n i="item8"></n>
            <n i="item4"></n>
            <n i="item10"></n>
            <n i="item11"></n>
        </n>
    </n>

</root>

 

 

 

 

 

你可能感兴趣的:(递归,dom4j)