xml文件解析(XMLNode)
package com.huawei.bss.xml;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* The Class XmlNode.
*/
public class XmlNode
{
/** The tag name. */
private String tagName = null;
/** The parent. */
private XmlNode parent = null;
/** The attributes. */
private Map<String, String> attributes = new LinkedHashMap<String, String>();
/** The nodes. */
private List<Object> nodes = new ArrayList<Object>();
/**
* Instantiates a new xml node.
*/
public XmlNode()
{
}
/**
* Instantiates a new xml node.
*
* @param tagName the tag name
*/
public XmlNode(String tagName)
{
this.tagName = tagName;
}
/**
* Gets the attributes.
*
* @return the attributes
*/
public Map<String, String> getAttributes()
{
return this.attributes;
}
/**
* Sets the attributes.
*
* @param attributes the attributes
*/
public void setAttributes(Map<String, String> attributes)
{
if (attributes != null)
{
this.attributes = attributes;
}
}
/**
* Gets the children.
*
* @return the children
*/
public List<Object> getChildren()
{
return this.nodes;
}
/**
* Gets the children by tag name.
*
* @param name the name
* @return the children by tag name
*/
public List<XmlNode> getChildrenByTagName(String name)
{
List<XmlNode> list = new ArrayList<XmlNode>();
List<Object> ch = getChildren();
for (int i = 0; i < ch.size(); ++i)
{
if ((ch.get(i).getClass() != XmlNode.class)
|| (!(((XmlNode) ch.get(i)).getTagName().equals(name))))
{
continue;
}
list.add((XmlNode) ch.get(i));
}
return list;
}
/**
* Gets the first.
*
* @param tag the tag
* @return the first
*/
public XmlNode getFirst(String tag)
{
List<XmlNode> tem = getChildrenByTagName(tag);
if (tem.isEmpty())
{
return null;
}
return (tem.get(0));
}
/**
* Gets the last.
*
* @param tag the tag
* @return the last
*/
public XmlNode getLast(String tag)
{
List<XmlNode> tem = getChildrenByTagName(tag);
if (tem.isEmpty())
{
return null;
}
return (tem.get(tem.size() - 1));
}
/**
* Gets the first.
*
* @return the first
*/
public Object getFirst()
{
List<Object> list = getChildren();
if (list.isEmpty())
{
return null;
}
return list.get(0);
}
/**
* Gets the last.
*
* @return the last
*/
public Object getLast()
{
List<Object> list = getChildren();
if (list.isEmpty())
{
return null;
}
return list.get(list.size() - 1);
}
/**
* Adds the node.
*
* @param obj the obj
* @return the xml node
*/
public XmlNode addNode(Object obj)
{
if (obj == null)
{
throw new IllegalArgumentException("addNode:node cant null");
}
this.nodes.add(obj);
if (obj instanceof XmlNode)
{
((XmlNode) obj).setParent(this);
}
return this;
}
/**
* Sets the attribute.
*
* @param name the name
* @param value the value
* @return the xml node
*/
public XmlNode setAttribute(String name, String value)
{
this.attributes.put(name, value);
return this;
}
/**
* Gets the parent.
*
* @return the parent
*/
public XmlNode getParent()
{
return this.parent;
}
/**
* Sets the parent.
*
* @param parent the new parent
*/
void setParent(XmlNode parent)
{
this.parent = parent;
}
/**
* Checks if is root.
*
* @return true, if is root
*/
public boolean isRoot()
{
return (this.parent == null);
}
/**
* Gets the tag name.
*
* @return the tag name
*/
public String getTagName()
{
return this.tagName;
}
/**
* Sets the tag name.
*
* @param tagName the tag name
* @return the xml node
*/
public XmlNode setTagName(String tagName)
{
this.tagName = tagName;
return this;
}
public String getText()
{
if (getChildren().isEmpty())
{
return null;
}
if (getChildren().size() != 1)
{
throw new RuntimeException("Not leaf node,nodeName:" + getTagName());
}
return getChildren().get(0).toString();
}
public String getAttribute(String arrtibuteName)
{
return getAttributes().get(arrtibuteName);
}
}
爱上