利用DOM4J创建XML格式字串

代码如下:

public class XmlUtils {
public static String buildXml(TradeBill tradeBill) {
		Document doc = DocumentHelper.createDocument();
		doc.setXMLEncoding("GBK");
               //创建节点
		Element root, bill, item;
		//根节点
		root = doc.addElement("document");
		
		bill = root.addElement("bill");
		bill.addElement("serialNo").setText(tradeBill.getTradecode());
		bill.addElement("termNo").setText(tradeBill.getPosid());
		bill.addElement("saleId").setText(""+tradeBill.getSaleid());
		bill.addElement("saleName").setText(tradeBill.getSalename());
		bill.addElement("buyId").setText(""+tradeBill.getBuyid());
		bill.addElement("buyName").setText(tradeBill.getBuyname());
		bill.addElement("totalWeight").setText(""+tradeBill.getTotalweight());
		bill.addElement("saleDate").setText(""+tradeBill.getTradetime());
		bill.addElement("tzName").setText(tradeBill.getSalename());
		bill.addElement("tzDate").setText(""+tradeBill.getTzDate());
		bill.addElement("jyOperator").setText(tradeBill.getJyOperator());
		bill.addElement("status").setText(""+tradeBill.getStatus());
		
		for (TradeBillItem tradeBillItem : tradeBill.getItems()) {
			item = bill.addElement("billItem");
			item.addElement("variety").setText(tradeBillItem.getName());
			item.addElement("weight").setText(""+tradeBillItem.getWeight());
			item.addElement("price").setText(""+tradeBillItem.getPrice());
			item.addElement("total").setText(""+tradeBillItem.getMoney());
		}

		return doc.asXML();
	}
}
 

你可能感兴趣的:(xml)