xml文件读写

1、读多行数据的属性值

SAXReader reader = new SAXReader();
Document document = reader.read(xmlFilePath);
Element root = document.getRootElement();

airlineList = new ArrayList<AirlineBean>();

List list = root.elements();
for (Object o : list) {
	Element e = (Element)o;

	AirlineBean bean = new AirlineBean();
	bean.setCode(e.attributeValue("code"));
	bean.setCnname(e.attributeValue("cnname"));
	bean.setEnname(e.attributeValue("enname"));
	airlineList.add(bean);
}

 

2、读xml格式的字符串

Document document = DocumentHelper.parseText(xml);
Element root = document.getRootElement();

bean.setCategoryId(getSingleNodeText(root, "/cki/cont/cagegoryid"));
bean.setPageSize(getSingleNodeText(root, "/cki/cont/pagesize"));
bean.setPageNumber(getSingleNodeText(root, "/cki/cont/pagenumber"));

 

protected String getSingleNodeText(Element e, String nodePath) {
	if (e == null) {
		return null;
	}

	Node node = e.selectSingleNode(nodePath);
	if (node != null) {
		return StringUtil.trim(node.getText());
	}
	return null;
}

 

你可能感兴趣的:(文件读写)