package dom4j;
import java.io.File;
import java.util.ArrayList;
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;
public class TestDom4J {
@SuppressWarnings(“unchecked”)
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("1.1.1.1");
list.add("2.2.2.2");
list.add("3.3.3.3");
list.add("4.4.4.4");
List<String> tempList = new ArrayList<String>();
SAXReader reader = new SAXReader();
try {
String path = "D:\\develop\\test\\test.xml";
Document read = reader.read(new File(path));
Element rootElement = read.getRootElement();
/*对所有元素遍历
* Iterator<Element> attributeIterator = rootElement.elementIterator();
while(attributeIterator.hasNext()){
Element next = attributeIterator.next();
String textTrim = next.getTextTrim();
tempList.add(textTrim);
}*/
//对指定子节点进行遍历
List<Element> elements = rootElement.elements("ip");
for (Element element : elements) {
String textTrim = element.getTextTrim();
tempList.add(textTrim);
}
for (String string : tempList) {
System.out.println(string);
}
System.out.println("=================");
list.removeAll(tempList);
for (String string : list) {
System.out.println(string);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}