解决拼装xml问题

时间:2022-09-07 13:05:37
package com.hw.utils;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


/**
 * 解析由String类型拼接的XML格式
 * 
 * @author Administrator
 */
public class Test6 {


public static void main(String[] args) throws DocumentException {
String xml = "<?xml version= '1.0' encoding='gb2312'?><weighData><weighTime>2012-12-2 12:23:12</weighTime><cardNum>2</cardNum><cfid>123</cfid></weighData>";
StringBuilder builder = new StringBuilder();
builder.append("<?xml version=\"1.0\" ?>\n<root>\n");
builder.append("<code>").append(1).append("</code>\n");
builder.append("<message>").append("数据下载成功").append("</message>\n");
builder.append("<rownum>").append(1).append("</rownum>\n");
builder.append("<hphm>").append("2").append("</hphm>\n");
builder.append("<hpzl>").append("2").append("</hpzl>\n");
builder.append("<hpys>").append("3").append("</hpys>\n");
builder.append("<fx>").append("4").append("</fx>\n");
builder.append("<wfxx>").append("4").append("</wfxx>\n");
builder.append("</root>");
System.out.println(builder.toString());
Document dom = DocumentHelper.parseText(builder.toString());
Element root = dom.getRootElement();
/*
* String weighTime=root.element("weighTime").getText(); String
* cardNum=root.element("cardNum").getText();
*/
String hpzl = root.element("hpzl").getText();


Map<String, Object> map = new HashMap<String, Object>();
/*
* map.put("weighTime", weighTime); map.put("cardNum", cardNum);
* map.put("cfid", cfid);
*/


BuildXml buildxml = new BuildXml();
// buildxml.setCardNum(cardNum);
// buildxml.setWeighTime(weighTime);
// buildxml.setCfid(cfid);
buildxml.setHpzl(hpzl);
// List<String>
// List<String> mlist = new ArrayList<>();
// mlist.add("weighTime", weighTime);
// System.out.println(weighTime);
System.out.println(buildxml);


}


}



package com.hw.utils;


public class BuildXml {
private String weighTime;
private String cardNum;
private String cfid;
private String code;
private String message;
private String rownum;
private String hphm;
private String hpzl;
private String hpys;
private String fx;
private String wfxx;

public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getRownum() {
return rownum;
}
public void setRownum(String rownum) {
this.rownum = rownum;
}
public String getHphm() {
return hphm;
}
public void setHphm(String hphm) {
this.hphm = hphm;
}
public String getHpzl() {
return hpzl;
}
public void setHpzl(String hpzl) {
this.hpzl = hpzl;
}
public String getHpys() {
return hpys;
}
public void setHpys(String hpys) {
this.hpys = hpys;
}
public String getFx() {
return fx;
}
public void setFx(String fx) {
this.fx = fx;
}
public String getWfxx() {
return wfxx;
}
public void setWfxx(String wfxx) {
this.wfxx = wfxx;
}


public String getWeighTime() {
return weighTime;
}
public void setWeighTime(String weighTime) {
this.weighTime = weighTime;
}
public String getCardNum() {
return cardNum;
}
public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}
public String getCfid() {
return cfid;
}
public void setCfid(String cfid) {
this.cfid = cfid;
}
}