private static String HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
private String name; //对象的名称
private Object value; //返回对象的值.
private Map<String,Object> attributes;
private List<XmlObjectUtil> subXmlObjects;
/**
* 根据name构造XmlObject
*
* @param name
* 生成xml时标签名,如name="html",则生成xml为<html/>
*/
public XmlObjectUtil(String name) {
this.name = name;
}
/**
* 获得当前对象的名称
*
* @return 返回当前对象的名称
*/
public String getName() {
return name;
}
/**
* 设置当前对象的名称
*
* @param name
* 给定名称
*/
public void setName(String name) {
this.name = name;
}
/**
* 获得当前对象的值
*
* @return 返回当前对象的值
*/
public Object getValue() {
return value;
}
/**
* 设置当前对象的值
*
* @param value
* 给定值
*/
public void setValue(Object value) {
this.value = value;
}
/**
* 为当前XmlObject添加属性
*
* @param name
* 属性名
* @param value
* 属性值
*/
public void addAttribute(String name,Object value) {
if (attributes == null) {
attributes = new LinkedHashMap<String,Object>();
}
if (name != null && !name.trim().equals("") && !name.equals(this.name)) {
attributes.put(name,value);
}
}
/**
* 为当前XmlObject添加属性
*
* @param name
* 属性名
* @param value
* 属性值
*/
public void setAttribute(String name,Object value) {
addAttribute(name,value);
}
/**
* 根据属性名称获得当前XmlObject对象的属性值
*
* @param name
* 属性名称
* @return 属性值
*/
public Object getAttributeValue(String name) {
return getAttributeValue(name,null);
}
/**
* 根据属性名称获得当前XmlObject对象的属性值
*
* @param name
* 属性名称
* @param defaultValue
* 默认值
* @return 若属性存在,则返回属性值,否则返回默认值
*/
public Object getAttributeValue(String name,Object defaultValue) {
Object value = attributes.get(name);
return value == null ? defaultValue : value;
}
/**
* 为当前XmlObject对象添加子XmlObject对象
*
* @param xmlObject
* 给定XmlObject对象
*/
public void addSubXmlObject(XmlObjectUtil xmlObject) {
if (subXmlObjects == null) {
subXmlObjects = new ArrayList<XmlObjectUtil>();
}
if (xmlObject != null) {
subXmlObjects.add(xmlObject);
}
}
public List<XmlObjectUtil> getSubXmlObjectsByName(String name) {
List<XmlObjectUtil> xmlObjects = new ArrayList<XmlObjectUtil>();
for (XmlObjectUtil temp: subXmlObjects) {
if (temp.getName().equals(name)) {
xmlObjects.add(temp);
}
}
return xmlObjects;
}
public XmlObjectUtil getUniqueSubXmlObjectByName(String name) {
for (XmlObjectUtil temp: subXmlObjects) {
if (temp.getName().equals(name)) {
return temp;
}
}
return null;
}
/**
* 构造当前对象的压缩XML字符串
*
* @return XML字符串
*/
public String toCompactXml() {
return HEAD + getNoHeadXml("","");
}
/**
* 根据格式化留白("\t")和默认的行分隔符("\t")构造当前对象的XML字符串
*
* @return XML字符串
*/
public String toFormatXml() {
return HEAD + getNoHeadXml("\t","\n");
}
/**
* 根据格式化留白和默认的行分隔符构("\n")造当前对象的XML字符串
*
* @param blank
* 格式化留白
* @return XML字符串
*/
public String toFormatXml(String blank) {
return HEAD + getNoHeadXml(blank,"\n");
}
/**
* 根据格式化留白和行分隔符构造当前对象的无头的XML字符串
*
* @param blank
* 格式化留白
* @param split
* 行分隔符
* @return 无头的XML字符串
*/
private String getNoHeadXml(String blank,String split) {
return getNoHeadXml(blank,split,0);
}
private String getNoHeadXml(String blank,String split,int count) {
String blanks = "";
for (int i = 0; i < count; i++) {
blanks += blank;
}
StringBuffer sb = new StringBuffer();
sb.append(count == 0 ? split : "");
sb.append(blanks + "<" + name);
if (attributes != null) {
Set<Entry<String,Object>> set = attributes.entrySet();
for (Entry<String,Object> entry : set) {
String tempName = entry.getKey();
Object tempValue = entry.getValue();
if (tempName != null && tempValue != null) {
sb.append(" " + tempName + "=\"" + tempValue + "\"");
}
}
}
if (subXmlObjects == null) {
if (value == null) {
sb.append("/>" + split);
} else {
sb.append(">");
sb.append(value);
sb.append("</" + name + ">" + split);
}
} else {
sb.append(">" + split);
Iterator<XmlObjectUtil> iterator = subXmlObjects.iterator();
count += 1;
while (iterator.hasNext()) {
XmlObjectUtil xmlObject = iterator.next();
sb.append(xmlObject.getNoHeadXml(blank,count));
}
sb.append(blanks + "</" + name + ">" + split);
}
return sb.toString();
}
public static String createXML(String xmlHead,Map<String,String> contentMap ){
XmlObjectUtil util = new XmlObjectUtil(xmlHead);
XmlObjectUtil tmpUtil = null;
String name,value;
for (Map.Entry<String,String> entry : contentMap.entrySet()) {
name=entry.getKey();
value=entry.getValue();
tmpUtil = new XmlObjectUtil(name);
tmpUtil.setName(name);
tmpUtil.setValue(value);
util.addSubXmlObject(tmpUtil);
}
return util.toFormatXml();
}
使用:XmlObjectUtil util = new XmlObjectUtil("RequestRealData"); //添加子节点. XmlObjectUtil node1 = new XmlObjectUtil("eventInfo"); util.addSubXmlObject(node1); node1.setName("eventInfo"); node1.setValue("123"); //添加子节点. XmlObjectUtil node2 = new XmlObjectUtil("source"); util.addSubXmlObject(node2); node2.setName("source"); node2.setValue("456"); xmlStr = util.toFormatXml();
原文链接:https://www.f2er.com/xml/299351.html