Skip to content

Instantly share code, notes, and snippets.

@ipcjs
Created November 7, 2015 11:41
Show Gist options
  • Save ipcjs/ea87bf0cec2a02c124be to your computer and use it in GitHub Desktop.
Save ipcjs/ea87bf0cec2a02c124be to your computer and use it in GitHub Desktop.
XML的元素, {@link #toString()}方法可以便捷的生成xml字符串
package com.celink.common.common;
import android.util.Xml;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
/**
* XML的元素, {@link #toString()}方法可以便捷的生成xml字符串
* Created by JiangSong on 2015/11/7.
*/
public class Element {
private String name;
private String text;
private String namespace;
private List<Prefix> prefixes = new ArrayList<>();
private List<Attribute> attributes = new ArrayList<>();
private List<Element> children = new ArrayList<>();
/** 省略XML声明 */
private boolean omitXmlDeclaration;
/** 缩进输出 */
private boolean indentOut;
public Element(String name) {
this.name = name;
}
public List<Prefix> getPrefixes() {
return prefixes;
}
public List<Attribute> getAttributes() {
return attributes;
}
public List<Element> getChildren() {
return children;
}
public void addPrefix(String prefix, String namespace) {
prefixes.add(new Prefix(prefix, namespace));
}
/**
* 增加一个子元素
* @param name tag名
* @return
*/
public Element newChild(String name) {
Element child = new Element(name);
addChild(child);
return child;
}
public void addChild(Element element) {
children.add(element);
}
public boolean removeChild(Element element) {
return children.remove(element);
}
/**
* 增加属性
* @param namespace
* @param name
* @param value
*/
public void addAttribute(String namespace, String name, String value) {
attributes.add(new Attribute(namespace, name, value));
}
public String getName() {
return name;
}
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
protected void asXml(XmlSerializer xml) throws IOException {
for (Prefix prefix : prefixes) {
xml.setPrefix(prefix.prefix, prefix.namespace);
}
xml.startTag(namespace, name);
for (Attribute attribute : attributes) {
xml.attribute(attribute.namespace, attribute.name, attribute.value);
}
if (text != null) {
xml.text(text);
}
for (Element child : children) {
child.asXml(xml);
}
xml.endTag(namespace, name);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Element element = (Element) o;
if (name != null ? !name.equals(element.name) : element.name != null) return false;
return !(namespace != null ? !namespace.equals(element.namespace) : element.namespace != null);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (namespace != null ? namespace.hashCode() : 0);
return result;
}
@Override
public String toString() {
try {
StringWriter writer = new StringWriter();
XmlSerializer xml = Xml.newSerializer();
xml.setOutput(writer);
if (!omitXmlDeclaration) {
xml.startDocument("utf-8", null);
}
if (indentOut) {
xml.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
}
asXml(xml);
xml.endDocument();
return writer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public boolean isOmitXmlDeclaration() {
return omitXmlDeclaration;
}
public void setOmitXmlDeclaration(boolean omitXmlDeclaration) {
this.omitXmlDeclaration = omitXmlDeclaration;
}
public boolean isIndentOut() {
return indentOut;
}
public void setIndentOut(boolean indentOut) {
this.indentOut = indentOut;
}
/**
* 前缀
*/
public static class Prefix {
public final String prefix;
public final String namespace;
public Prefix(String prefix, String namespace) {
this.prefix = prefix;
this.namespace = namespace;
}
}
/**
* 属性
*/
public static class Attribute {
public final String namespace;
public final String name;
public final String value;
public Attribute(String namespace, String name, String value) {
this.namespace = namespace;
this.name = name;
this.value = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment