
com.ktanx.common.xml.XmlNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktanx-common Show documentation
Show all versions of ktanx-common Show documentation
Ktanx is a java development kit.
The newest version!
package com.ktanx.common.xml;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by liyd on 16/12/27.
*/
public class XmlNode {
private String name;
private String text;
private Map attributes;
private List childNodes;
public XmlNode() {
this(null);
}
public XmlNode(String nodeName) {
this.name = nodeName;
this.attributes = new HashMap();
this.childNodes = new ArrayList();
}
/**
* 添加属性
*
* @param name
* @param value
*/
public void addAttribute(String name, String value) {
this.attributes.put(name, value);
}
/**
* 获取属性
*
* @param attributeName
* @return
*/
public String getAttribute(String attributeName) {
return attributes == null ? "" : attributes.get(attributeName);
}
/**
* 获取子节点
*
* @param nodeName
* @return
*/
public XmlNode getSingleChildNode(String nodeName) {
for (XmlNode childNode : this.childNodes) {
if (StringUtils.equals(nodeName, childNode.getName())) {
return childNode;
}
}
return null;
}
/**
* 获取子节点
*
* @param nodeName
* @return
*/
public List getChildNodes(String nodeName) {
List nodes = new ArrayList<>();
for (XmlNode childNode : this.childNodes) {
if (StringUtils.equals(nodeName, childNode.getName())) {
nodes.add(childNode);
}
}
return nodes;
}
/**
* 获取子节点内容
*
* @param nodeName
* @return
*/
public String getSingleChildNodeText(String nodeName) {
XmlNode childNode = getSingleChildNode(nodeName);
return childNode == null ? "" : childNode.getText();
}
/**
* 添加子节点
*
* @param xmlNode
*/
public void addChildNode(XmlNode xmlNode) {
childNodes.add(xmlNode);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map getAttributes() {
return attributes;
}
public void setAttributes(Map attributes) {
this.attributes = attributes;
}
public List getChildNodes() {
return childNodes;
}
public void setChildNodes(List childNodes) {
this.childNodes = childNodes;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy