SAX方式把String转化为XML对象

import java.io.StringReader;

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

public class Demo {
	public static void main(String[] args) throws DocumentException {
		String result="<packets>" +
				"<iq type=\"result\" id=\"6h3yE-0\" from=\"aaaaaaaa\" to=\"bbbbbb\">" +
				"<unique xmlns=\"bbbbbbb\">1000272</unique>" +
				"</iq>" +
				"</packets>";
		SAXReader xmlReader = new SAXReader();
		Document doc = xmlReader.read(new StringReader(result));
		String uniqueId = doc.getRootElement().element("iq").element("unique").getTextTrim();
		System.out.println(uniqueId);
	}
}
 

你可能感兴趣的:(String)