使用dom4j解析XML简单示例

从字符串解析XML

public class Dom4jSample {
	static final String xmlString = "<persons><person age=\"32\"><name>张三</name></person>"
			+ "<person age=\"26\"><name>李四</name></person></persons>";

	public static void main(String[] args) {
		try {
			Document doc = DocumentHelper.parseText(xmlString);
			List<Element> personList = doc.selectNodes("//persons/person");
			for (Element person : personList) {
				System.out.println("姓名:" + person.elementText("name") + ";年龄:"
						+ person.attributeValue("age"));
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
 

你可能感兴趣的:(sam79_dom4j,sam79_xml)