自己写的一个XML元素类

package edu.basejava.util;

class Element
{
 private String name;

 private String text;

 private Element parent;

 private LinkedList<Attribute> attributes;

 private LinkedList<Element> children;

 public boolean isRoot()
 {
  return parent == null;
 }

 public boolean isLeaf()
 {
  return children == null;
 }

 public boolean hasChild()
 {
  return children != null;
 }

 public boolean hasAttribute()
 {
  return attributes != null;
 }

 public void addAttribute(Attribute ab)
 {
  if (!hasAttribute())
   attributes = new LinkedList<Attribute>();
  attributes.append(ab);
 }

 public void editAttribute(String name, String value)
 {
  for (Attribute ab : attributes)
   if (ab.getName().equals(name))
    ab.setValue(value);
 }

 public Attribute getAttribute(String name)
 {
  for (Attribute ab : attributes)
   if (ab.getName().equals(name))
    return ab;
  return null;
 }

 public void delAttribute(String name)
 {
  this.attributes.setFirst();
  for (Attribute ab : this.attributes)
   if (ab.getName().equals(name))
    attributes.remove(ab);
 }

 public void editChild(Element e)
 {
  for (Element cd : this.children)
   if (cd.getName().equals(e.getName()))
   {
    cd.getParent().getChildren().remove(cd);
    cd.setAttributes(e.getAttributes());
    cd.setChildren(e.getChildren());
    cd.setParent(e.getParent());
    cd.setText(e.getText());

   }
 }

 public void delChild(String name)
 {
  this.children.setFirst();
  for (Element cd : this.children)
   if (cd.getName().equals(name))
    children.remove(cd);
 }

 public void addChild(Element e)
 {
  if (children == null)
   children = new LinkedList<Element>();
  e.setParent(this);
  children.append(e);
 }

 public Element getChild(String name)
 {
  for (Element e : children)
  {
   if (e.getName().equals(e))
    return e;
  }
  return null;
 }

 public Element(String name, Attribute... attributes)
 {
  this.name = name;
  if (attributes.length != 0)
  {
   this.attributes = new LinkedList<Attribute>();
   for (Attribute ab : attributes)
    this.addAttribute(ab);
  }
 }

 public Element(String name, String text, Attribute... attributes)
 {
  this.name = name;
  this.text = text;
  if (attributes.length != 0)
  {
   this.attributes = new LinkedList<Attribute>();
   for (Attribute ab : attributes)
    this.attributes.append(ab);
  }
 }

 private String recurElement(Element e)
 {
  StringBuffer sb = new StringBuffer();
  if (e != null)
  {
   if (e.hasAttribute())
   {
    sb.append(XmlSymbol
      .startElement(e.getName(), e.getAttributes()));
   } else
    sb.append(XmlSymbol.startElement(e.getName()));
   if (e.getText() != null)
    sb.append(e.getText());
   if (e.hasChild())
   {
    for (Element el : e.getChildren())
     sb.append(e.recurElement(el));
   }
   sb.append(XmlSymbol.endElement(e.getName()));
  }
  return sb.toString();
 }

 public String toString()
 {
  return this.recurElement(this);
 }

 public static void main(String[] args)
 {
  Element e1 = new Element("long", new Attribute("test", "value"));
  Element e2 = new Element("child", "子节点", new Attribute("id1",
    "firstchild"), new Attribute("id2", "firstchild"));
  Element e3 = new Element("叶子");
  e1.addChild(e2);
  e2.editAttribute("id2", "edit");
  e2.addChild(e3);
  e2.delAttribute("id1");
  e2.addAttribute(new Attribute("tt", "eeer"));
  e2.addAttribute(new Attribute("tt2", "eeer"));
  Log.println(e3);
  // Log.debug(el);
 }

 public LinkedList<Attribute> getAttributes()
 {
  return attributes;
 }

 public void setAttributes(LinkedList<Attribute> attributes)
 {
  this.attributes = attributes;
 }

 public String getName()
 {
  return name;
 }

 public void setName(String name)
 {
  this.name = name;
 }

 public Element getParent()
 {
  return parent;
 }

 public void setParent(Element parent)
 {
  this.parent = parent;
 }

 public LinkedList<Element> getChildren()
 {
  return children;
 }

 public void setChildren(LinkedList<Element> children)
 {
  for (Element e : children)
   e.setParent(this);
  this.children = children;
 }

 public String getText()
 {
  return text;
 }

 public void setText(String text)
 {
  this.text = text;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值