隨筆-35  評(píng)論-97  文章-0  trackbacks-0

          使用xml格式字符串轉(zhuǎn)載數(shù)據(jù)和卸載數(shù)據(jù),在數(shù)據(jù)傳送時(shí)可以使用,避免直接對(duì)節(jié)點(diǎn)和屬性等操作。

          也可用來(lái)讀取配置文件。

          Root類(lèi):

          Root是跟節(jié)點(diǎn)。結(jié)點(diǎn)名默認(rèn)為"root",但是可以根據(jù)需要改變,使用setNodeName(String nodeName)。

          put(String key, Object value)是添加屬性值。

          setValue(String value)是設(shè)定節(jié)點(diǎn)值。

          parseXml(String xml)將xml字符串解析出Root對(duì)象。

          toXml()將Root對(duì)象轉(zhuǎn)為字符串。

          addItem(Item item)為添加子節(jié)點(diǎn)。

           

          Item類(lèi):

          addItem(Item item)為添加子節(jié)點(diǎn),可以嵌套。

          parseElement(Element ele)為解析節(jié)點(diǎn)。 

          Element toElement()將Item對(duì)象轉(zhuǎn)成元素。

          其他的跟Root類(lèi)差不多。

          String toString()減Item轉(zhuǎn)成字符串。

           

          不多做解釋了,看看源碼就知道,很簡(jiǎn)單,雖不是很完善,但也可用用了。你可以再根據(jù)自己需要修改。

          Root

          import java.util.ArrayList;
          import java.util.Iterator;
          import java.util.LinkedHashMap;
          import java.util.List;
          import java.util.Map;

          import org.apache.commons.collections.CollectionUtils;
          import org.apache.commons.lang.StringUtils;
          import org.dom4j.Attribute;
          import org.dom4j.Document;
          import org.dom4j.DocumentHelper;
          import org.dom4j.Element;


          /**
           * 
          @author haoyu
           * 
           
          */

          public class Root
          {
              
          private String nodeName = "root";
              
          private String name;
              
          private String description;

              
          private Map<String, Object> attributes = new LinkedHashMap<String, Object>();
              
          private List<Item> items;


              
          public Root()
              
          {
              }



              
          public Root(String nodeName)
              
          {
                  
          if(StringUtils.isNotBlank(nodeName))
                  
          {
                      
          this.nodeName = nodeName;
                  }

              }



              
          public String getDescription()
              
          {
                  
          return description;
              }



              
          public void setDescription(String description)
              
          {
                  
          if(StringUtils.isNotBlank(description))
                  
          {
                      
          this.description = description;
                  }

              }



              
          public String getName()
              
          {
                  
          return name;
              }



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



              
          public String getNodeName()
              
          {
                  
          return nodeName;
              }



              
          public void setNodeName(String nodeName)
              
          {
                  
          this.nodeName = nodeName;
              }



              
          public void put(String key, Object value)
              
          {
                  attributes.put(key, value);
              }



              
          public Object get(String key)
              
          {
                  
          return attributes.get(key);
              }



              
          public Map<String, Object> getAttributes()
              
          {
                  
          return attributes;
              }



              
          public void setAttributes(Map<String, Object> attributes)
              
          {
                  
          this.attributes = attributes;
              }



              
          public List<Item> getItems()
              
          {
                  
          return items;
              }



              
          public void addItem(Item item)
              
          {
                  
          if(items == null)
                  
          {
                      items 
          = new ArrayList<Item>();
                  }

                  items.add(item);
              }



              
          public String toXml()
              
          {
                  Document doc 
          = DocumentHelper.createDocument();
                  Element rootEle 
          = doc.addElement(nodeName);
                  
          if(name != null)
                  
          {
                      rootEle.addAttribute(
          "name", name);
                  }

                  
          if(description != null)
                  
          {
                      rootEle.addAttribute(
          "description", description);
                  }


                  
          for(Iterator it = attributes.entrySet().iterator(); it.hasNext();)
                  
          {
                      Map.Entry entry 
          = (Map.Entry)it.next();
                      
          if(entry == null || entry.getKey() == null || entry.getValue() == null
                          
          || StringUtils.isBlank(String.valueOf(entry.getKey())))
                      
          {
                          
          continue;
                      }

                      rootEle.addAttribute(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
                  }


                  
          if(CollectionUtils.isNotEmpty(items))
                  
          {
                      
          for(Iterator<Item> it = items.iterator(); it.hasNext();)
                      
          {
                          Item item 
          = it.next();
                          rootEle.add(item.toElement());
                      }

                  }

                  
          return doc.asXML();
              }



              
          public String toString()
              
          {
                  
          return toXml();
              }



              @SuppressWarnings(
          "unchecked")
              
          public void parseXml(String xml)
              
          {
                  
          try
                  
          {
                      Document doc 
          = DocumentHelper.parseText(xml);
                      Element rootEle 
          = doc.getRootElement();
                      
          this.setNodeName(rootEle.getName());
                      
          this.setName(rootEle.attributeValue("name"));
                      
          this.setDescription(rootEle.attributeValue("description"));

                      
          for(Iterator it = attributes.entrySet().iterator(); it.hasNext();)
                      
          {
                          Map.Entry entry 
          = (Map.Entry)it.next();
                          
          if(entry == null || entry.getKey() == null || entry.getValue() == null
                              
          || StringUtils.isBlank(String.valueOf(entry.getKey())))
                          
          {
                              
          continue;
                          }


                          rootEle.addAttribute(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
                      }


                      
          for(Iterator it = rootEle.attributeIterator(); it.hasNext();)
                      
          {
                          Attribute attr 
          = (Attribute)it.next();
                          
          this.put(attr.getName(), attr.getText());
                      }


                      List
          <Element> items = rootEle.elements();

                      
          if(CollectionUtils.isNotEmpty(items))
                      
          {
                          
          for(Iterator<Element> it = items.iterator(); it.hasNext();)
                          
          {
                              Element recordEle 
          = it.next();
                              Item item 
          = new Item();
                              item.parseElement(recordEle);
                              
          this.addItem(item);
                          }

                      }

                  }

                  
          catch(Exception e)
                  
          {
                      e.printStackTrace();
                  }

              }



              
          public Document trun2Doc()
              
          {
                  
          try
                  
          {
                      
          return DocumentHelper.parseText(this.toString());
                  }

                  
          catch(Exception e)
                  
          {
                      e.printStackTrace();
                  }

                  
          return null;
              }

          }

           

          Item

          import java.util.ArrayList;
          import java.util.Iterator;
          import java.util.LinkedHashMap;
          import java.util.List;
          import java.util.Map;

          import org.apache.commons.collections.CollectionUtils;
          import org.apache.commons.lang.StringUtils;
          import org.dom4j.Attribute;
          import org.dom4j.DocumentHelper;
          import org.dom4j.Element;


          /**
           * 
          @author haoyu
           * 
           
          */

          public class Item
          {

              
          private String nodeName = "item";

              
          private String value;

              
          private Map<String, Object> attributes = new LinkedHashMap<String, Object>();

              
          private List<Item> items;


              
          public Item()
              
          {
              }



              
          public Item(String nodeName)
              
          {
                  
          if(StringUtils.isNotBlank(nodeName))
                  
          {
                      
          this.nodeName = nodeName;
                  }

              }



              
          public String getNodeName()
              
          {
                  
          return nodeName;
              }



              
          public void setNodeName(String nodeName)
              
          {
                  
          if(StringUtils.isNotBlank(nodeName))
                  
          {
                      
          this.nodeName = nodeName;
                  }

              }



              
          public void put(String key, Object value)
              
          {
                  attributes.put(key, value);
              }



              
          public Object get(String key)
              
          {
                  
          return attributes.get(key);
              }



              
          public Map<String, Object> getAttributes()
              
          {
                  
          return attributes;
              }



              
          public void setAttributes(Map<String, Object> attributes)
              
          {
                  
          this.attributes = attributes;
              }



              
          public String getValue()
              
          {
                  
          return value;
              }



              
          public void setValue(String value)
              
          {
                  
          this.value = value;
              }



              
          public List<Item> getItems()
              
          {
                  
          return items;
              }



              
          public void addItem(Item item)
              
          {
                  
          if(items == null)
                  
          {
                      items 
          = new ArrayList<Item>();
                  }

                  items.add(item);
              }



              
          public void parseElement(Element ele)
              
          {
                  
          this.nodeName = ele.getName();
                  
          for(Iterator it = ele.attributeIterator(); it.hasNext();)
                  
          {
                      Attribute attr 
          = (Attribute)it.next();
                      
          this.put(attr.getName(), attr.getText());
                  }

                  
          if(StringUtils.isNotBlank(ele.getText()))
                  
          {
                      
          this.setValue(ele.getText());
                  }

                  List
          <Element> itemEles = ele.elements();
                  
          if(CollectionUtils.isNotEmpty(itemEles))
                  
          {
                      
          for(Iterator it = itemEles.iterator(); it.hasNext();)
                      
          {
                          Element itemEle 
          = (Element)it.next();
                          Item item 
          = new Item(itemEle.getName());
                          item.parseElement(itemEle);
                          
          this.addItem(item);
                      }


                  }

              }



              
          public Element toElement()
              
          {
                  Element itemEle 
          = DocumentHelper.createElement(nodeName);
                  
          for(Iterator it = attributes.entrySet().iterator(); it.hasNext();)
                  
          {
                      Map.Entry entry 
          = (Map.Entry)it.next();
                      
          if(entry == null || entry.getKey() == null || entry.getValue() == null
                          
          || StringUtils.isBlank(String.valueOf(entry.getKey())))
                      
          {
                          
          continue;
                      }


                      itemEle.addAttribute(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
                  }

                  
          if(CollectionUtils.isNotEmpty(items))
                  
          {
                      
          for(Iterator it = items.iterator(); it.hasNext();)
                      
          {
                          Item item 
          = (Item)it.next();
                          itemEle.add(item.toElement());
                      }

                  }

                  
          if(value != null)
                  
          {
                      itemEle.setText(value);
                  }


                  
          return itemEle;
              }



              
          public String toString()
              
          {
                  StringBuffer sb 
          = new StringBuffer();
                  sb.append(
          "<").append(nodeName);
                  
          for(Iterator it = attributes.entrySet().iterator(); it.hasNext();)
                  
          {
                      Map.Entry entry 
          = (Map.Entry)it.next();
                      sb.append(
          " ").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
                  }

                  
          if(value != null)
                  
          {
                      sb.append(
          ">").append(value).append("</").append(nodeName).append(">");
                  }

                  
          else
                  
          {
                      sb.append(
          "/>");
                  }

                  
          return sb.toString();
              }

          }

           

          簡(jiǎn)單應(yīng)用一下試試:

              public static void main(String[] args)
              
          {
                  Root bookmark 
          = new Root();
                  bookmark.setNodeName(
          "書(shū)簽");

                  Item news 
          = new Item();
                  news.setNodeName(
          "類(lèi)別");
                  news.put(
          "描述""新聞");
                  Item sina 
          = new Item();
                  sina.setNodeName(
          "網(wǎng)點(diǎn)");
                  sina.put(
          "名稱(chēng)""新浪");
                  sina.setValue(
          "http://www.sina.com");
                  news.addItem(sina);

                  Item search 
          = new Item();
                  search.setNodeName(
          "網(wǎng)點(diǎn)");
                  search.setNodeName(
          "類(lèi)別");
                  news.put(
          "描述""搜索引擎");
                  Item google 
          = new Item();
                  google.setNodeName(
          "網(wǎng)點(diǎn)");
                  google.put(
          "名稱(chēng)""谷歌");
                  google.setValue(
          "http://www.google.com");
                  Item baidu 
          = new Item();
                  baidu.setNodeName(
          "網(wǎng)點(diǎn)");
                  baidu.put(
          "名稱(chēng)""百度");
                  baidu.setValue(
          "http://www.baidu.com");
                  search.addItem(google);
                  search.addItem(baidu);

                  bookmark.addItem(news);
                  bookmark.addItem(search);
                  System.out.println(bookmark.toString());

                  Root newBookmark 
          = new Root();
                  newBookmark.parseXml(bookmark.toString());
                  System.out.println(newBookmark.toString());
              }

           

          結(jié)果輸出:

          <?xml version="1.0" encoding="UTF-8"?>
          <書(shū)簽><類(lèi)別 描述="新聞"><網(wǎng)點(diǎn) 名稱(chēng)="新浪">http://www.sina.com</網(wǎng)點(diǎn)></類(lèi)別><類(lèi)別 描述="搜索引擎"><網(wǎng)點(diǎn) 名稱(chēng)="谷歌">http://www.google.com</網(wǎng)點(diǎn)><網(wǎng)點(diǎn) 名稱(chēng)="百度">http://www.baidu.com</網(wǎng)點(diǎn)></類(lèi)別></書(shū)簽>
          <?xml version="1.0" encoding="UTF-8"?>
          <書(shū)簽><類(lèi)別 描述="新聞"><網(wǎng)點(diǎn) 名稱(chēng)="新浪">http://www.sina.com</網(wǎng)點(diǎn)></類(lèi)別><類(lèi)別 描述="搜索引擎"><網(wǎng)點(diǎn) 名稱(chēng)="谷歌">http://www.google.com</網(wǎng)點(diǎn)><網(wǎng)點(diǎn) 名稱(chēng)="百度">http://www.baidu.com</網(wǎng)點(diǎn)></類(lèi)別></書(shū)簽>
          posted on 2007-08-15 01:08 三告習(xí)習(xí) 閱讀(1491) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java/jdk
          主站蜘蛛池模板: 汨罗市| 松桃| 江门市| 新乐市| 雅安市| 长丰县| 濮阳市| 襄城县| 巩留县| 旬邑县| 乌审旗| 右玉县| 西昌市| 万盛区| 调兵山市| 黄山市| 乐亭县| 恩平市| 迁西县| 阳西县| 法库县| 息烽县| 嘉荫县| 科尔| 万安县| 大同县| 巨野县| 华亭县| 太原市| 抚远县| 民县| 永州市| 华坪县| 桂平市| 城步| 南郑县| 托克逊县| 巴彦淖尔市| 大新县| 内黄县| 乌审旗|