更新時間:2021-08-19 11:51:54 來源:動力節點 瀏覽2067次
java生成XML格式的字符串,支持多種XML格式,具體可以看類內方法:
代碼:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 根據該對象可以構造Xml字符串
*
*
*/
public class XmlObject {
private static String HEAD = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
private String name;
private Object value;
private List<Attribute> attributes;
private List<XmlObject> subXmlObjects;
/**
* 根據name構造XmlObject
* @param name 生成xml時標簽名,如name="html",則生成xml為<html/>
*/
public XmlObject(String name) {
this.name = name;
}
/**
* 獲得當前對象的名稱
* @return 返回當前對象的名稱
*/
public final String getName() {
return name;
}
/**
* 設置當前對象的名稱
* @param name 給定名稱
*/
public final void setName(String name) {
this.name = name;
}
/**
* 獲得當前對象的值
* @return 返回當前對象的值
*/
public final Object getValue() {
return value;
}
/**
* 設置當前對象的值
* @param value 給定值
*/
public final void setValue(Object value) {
this.value = value;
}
/**
* 為當前XmlObject添加屬性
* @param name 屬性名
* @param value 屬性值
*/
public final void setAttribute(String name, Object value) {
if (attributes == null) {
attributes = new ArrayList<Attribute>();
}
Attribute attribute = null;
for (Attribute att : attributes) {
if (name.equalsIgnoreCase(att.getName())) {
attattribute = att;
break;
}
}
if (attribute == null) {
attribute = new Attribute(name, value);
attributes.add(attribute);
} else {
attribute.setValue(value);
}
}
/**
* 根據屬性名稱獲得當前XmlObject對象的屬性值
* @param name 屬性名稱
* @return 屬性值
*/
public final Object getAttributeValue(String name) {
return getAttributeValue(name, null);
}
/**
* 根據屬性名稱獲得當前XmlObject對象的屬性值
* @param name 屬性名稱
* @param defaultValue 默認值
* @return 若屬性存在,則返回屬性值,否則返回默認值
*/
public final Object getAttributeValue(String name, Object defaultValue) {
Attribute attribute = null;
for (Attribute att : attributes) {
if (name.equalsIgnoreCase(att.getName())) {
attattribute = att;
break;
}
}
if (attribute == null) {
return defaultValue;
} else {
return attribute.getValue();
}
}
/**
* 為當前XmlObject對象添加子XmlObject對象
* @param xmlObject 給定XmlObject對象
*/
public final void addSubXmlObject(XmlObject xmlObject) {
if (subXmlObjects == null) {
subXmlObjects = new ArrayList<XmlObject>();
}
subXmlObjects.add(xmlObject);
}
/**
* 構造當前對象的壓縮XML字符串
*
* @return XML字符串
*/
public final String toCompactXml() {
return HEAD + getNoHeadXml("", "");
}
/**
* 根據格式化留白("\t")和默認的行分隔符("\t")構造當前對象的XML字符串
*
* @return XML字符串
*/
public String toFormatXml() {
return toFormatXml("\t");
}
/**
* 根據格式化留白和默認的行分隔符構("\n")造當前對象的XML字符串
*
* @param blank
* 格式化留白
* @return XML字符串
*/
protected final String toFormatXml(String blank) {
return HEAD + "\n" + getNoHeadXml(blank, "\n");
}
/**
* 根據格式化留白和行分隔符構造當前對象的無頭的XML字符串
*
* @param blank
* 格式化留白
* @param split
* 行分隔符
* @return 無頭的XML字符串
*/
protected final String getNoHeadXml(String blank, String split) {
return getNoHeadXml(blank, split, 0);
}
private final 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(blanks + "<" + name);
if (attributes != null) {
Iterator<Attribute> iterator = attributes.iterator();
while (iterator.hasNext()) {
Attribute attribute = iterator.next();
sb.append(" " + attribute.getName() + "=\""
+ attribute.getValue() + "\"");
}
}
if (subXmlObjects == null) {
if (value == null) {
sb.append("/>" + split);
} else {
sb.append(">");
sb.append(value);
sb.append("</" + name + ">" + split);
}
} else {
sb.append(">" + split);
Iterator<XmlObject> iterator = subXmlObjects.iterator();
count += 1;
while (iterator.hasNext()) {
XmlObject xmlObject = iterator.next();
sb.append(xmlObject.getNoHeadXml(blank, split, count));
}
sb.append(blanks + "</" + name + ">" + split);
}
return sb.toString();
}
class Attribute {
private String name;
private Object value;
public Attribute(String name, Object value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}
}
以上就是動力節點小編介紹的"Java生成XML格式的字符串",希望對大家有幫助,想了解更多可查看Java在線學習。動力節點在線學習教程,針對沒有任何Java基礎的讀者學習,讓你從入門到精通,主要介紹了一些Java基礎的核心知識,讓同學們更好更方便的學習和了解Java編程,感興趣的同學可以關注一下。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習