dom4j解析xml和xmlstr

用到的包
import java.io.File;
import java.util.Iterator;

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


  String str = ""; //xml字符串


  SAXReader saxReader = new SAXReader();		
		Document document = null;
		
		try {
			document = DocumentHelper.parseText(str);
			//document = saxReader.read(new File("d:/NewFile.xml"));
		} catch (DocumentException e) {
			System.out.println(">>>>>>>>>>>>" + e.getMessage());
			e.printStackTrace();
		}
		Element root = document.getRootElement(); 
		
		Element selitem = null, item = null;
		for (Iterator it = root.elementIterator("item"); it.hasNext();) {
			item = (Element) it.next();
				String sub1= item.element("sub1").getText();
				String sub2= item.elementText("sub2");
				System.out.println(sub1+">>>>>>>>>"+sub2);
		}

你可能感兴趣的:(dom4j)