隨筆-13  評論-9  文章-9  trackbacks-0

          XML心得:
          一般獲取xml的document開始解析。每個屬性都有一個Attribute類。每個Attribute類都可以獲取其element類Element e=attribute.getParent(); 
          遍歷的時候有專門的elementIterator()方法.
          package com.birtsample.util;

          import java.io.File;
          import java.util.HashMap;
          import java.util.Iterator;
          import java.util.List;

          import org.dom4j.Attribute;
          import org.dom4j.Document;
          import org.dom4j.DocumentException;
          import org.dom4j.Element;
          import org.dom4j.io.SAXReader;

          public class XmlParseUtils {
           public static HashMap getXmlStyle(String filePath,String styleName)
           {
            SAXReader saxReader = new SAXReader();
            HashMap map=new HashMap();
            try {
             Document document = saxReader.read(new File(filePath));
          //找到節點屬性是"name"的路徑
             List list = document.selectNodes("/styles/style/@name" );
                   Iterator it = list.iterator();
                   List list2=null;
                
                   while(it.hasNext())
                   {
                    Attribute attribute = (Attribute)it.next();
                //節點屬性是"name"=特定值
                    if(attribute.getValue().equalsIgnoreCase(styleName))
                    {
                //通過屬性獲取其element類
                     Element e=attribute.getParent(); 
                     Iterator it2=null;
                     it2=e.elementIterator();
                     while(it2.hasNext())
                     {
                      Element element=(Element)it2.next(); 
                    //  System.out.println(element);
                //通過element獲取其屬性或者他下節點
                      Attribute attr=element.attribute("name");   
                      map.put(getMethodName(attr.getValue()), element.getText());
                      
                     } 
                    }
                   }
                   return map;
            } catch (DocumentException e) {
             // TODO 自動生成 catch 塊
             e.printStackTrace();
             return null;
            }

           
           }
           
           public static String getMethodName(String field)
           {
            String methodName=field.substring(1);
            String upperCase=field.substring(0,1).toUpperCase();
            methodName="set"+upperCase+methodName;
            return methodName;
           }

           public static void main(String arg[])
           {
            getMethodName("fontFamily");
           }
           

          }






          .import java.util.*;
          import java.io.File;

          import org.dom4j.Document;
          import org.dom4j.Element;
          import org.dom4j.DocumentException;
          import org.dom4j.io.SAXReader;
          import org.dom4j.Node;
          import org.dom4j.DocumentHelper;
          import org.dom4j.Attribute;

          public class Dom4jParseXml {
              //通過xml文件名得到DOM
              public Document getDocument(String xmlFileName) throws DocumentException {
                  SAXReader reader = new SAXReader();
                  Document d = reader.read(new File(xmlFileName));
                  return d;
              }
              //重載,通過xml文件內容得到DOM
              public Document getDocument(String xmlContent, boolean b) throws
                      DocumentException {
                  Document d = DocumentHelper.parseText(xmlContent);
                  return d;
              }

              //輸出字符串
              public String transformDOM(Document d) {
                  String xmlContent = "";
                  xmlContent = d.asXML();
                  return xmlContent;
              }

              //得到節點
              public Element getNode(Document d, String elePath, String eleValue) {
                  Element ele = null;
                  List l = d.selectNodes(elePath);
                  Iterator iter = l.iterator();
                  while (iter.hasNext()) {
                      Element tmp = (Element) iter.next();
                      if (tmp.getText().equals(eleValue)) {
                          ele = tmp;
                      }
                  }
                  return ele;
              }
              //重載,得到節點
              public Element getNode(Document d, String eleName) {
                  Element ele = (Element) d.selectSingleNode(eleName);
                  return ele;
              }

              //增加節點
              public void addNode(Element parentEle, String eleName, String eleValue) {
                  Element newEle = parentEle.addElement(eleName);
                  newEle.setText(eleValue);
              }

              //增加屬性節點
              public void addAttribute(Element ele, String attributeName,
                                       String attributeValue) {
                  ele.addAttribute(attributeName, attributeValue);
              }

              //刪除節點
              public void removeNode(Element parentEle, String eleName, String eleValue) {
                  Iterator iter = parentEle.elementIterator();
                  Element delEle = null;
                  while (iter.hasNext()) {
                      Element tmp = (Element) iter.next();
                      if (tmp.getName().equals(eleName) && tmp.getText().equals(eleValue)) {
                          delEle = tmp;
                      }
                  }
                  if (delEle != null) {
                      parentEle.remove(delEle);
                  }
              }

              //刪除屬性
              public void removeAttr(Element ele, String attributeName) {
                  Attribute att = ele.attribute(attributeName);
                  ele.remove(att);
              }

              //修改節點值
              public void setNodeText(Element ele, String newValue) {
                  ele.setText(newValue);
              }

              //修改屬性值
              public void setAttribute(Element ele, String attributeName,
                                       String attributeValue) {
                  Attribute att = ele.attribute(attributeName);
                  att.setText(attributeValue);
              }
          }

          posted on 2007-04-19 14:39 Dragonofson 閱讀(1264) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 亳州市| 印江| 恩施市| 抚松县| 兴仁县| 云霄县| 靖州| 莱芜市| 安宁市| 中卫市| 曲周县| 长子县| 通化县| 于都县| 仙游县| 阜宁县| 万荣县| 玉门市| 耿马| 涿鹿县| 汉阴县| 贵德县| 临颍县| 桐城市| 沙雅县| 定陶县| 互助| 聊城市| 台湾省| 德化县| 绥德县| 海门市| 邵阳市| 桃园县| 宝清县| 金山区| 永泰县| 许昌县| 平塘县| 香格里拉县| 怀宁县|