<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    package net.better_best.www.dynamicADPics.utils;

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

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

    public class DOM4jUtils {
        
    /**
         * 在一個(gè)xml文件中,遍歷xml文件,根據(jù)Element的elementName,取出所有復(fù)合條件的Element元素,把它們轉(zhuǎn)換為javabean類型,并放入List中返回。
         
    */

        
    public static List parseElementsToObjects(Element root, String elementName) {
            List
    <Banner> result =  new ArrayList<Banner>();
              List
    <Element> list = root.elements(elementName);      
              
    for (Element element : list) {
                 Banner banner 
    = new Banner();
                 
    for(Iterator it=element.attributeIterator();it.hasNext();){
                     Attribute attribute 
    = (Attribute) it.next();
                     
    if(attribute.getName().equals(DOM4jConstant.ELEMENT_ATTRIBUTE_BGURL)){
                         banner.setBnurl(attribute.getValue());
                     }
    else if(attribute.getName().equals(DOM4jConstant.ELEMENT_ATTRIBUTE_IMG)){
                         banner.setImg(attribute.getValue());
                     }
    else if(attribute.getName().equals(DOM4jConstant.ELEMENT_ATTRIBUTE_BNTARGET)){
                         banner.setBntarget(attribute.getValue());
                     }

                 }

                 result.add(banner);            
            }

              
    return result ;
        }

        
    /**
         * 根據(jù)xml文件的文件名,把xml文件轉(zhuǎn)換成Document對(duì)象并返回。
         
    */

        
    public static Document getDocument(String fileUrl)throws DocumentException {
            File file 
    = new File(fileUrl);
            SAXReader reader 
    = new SAXReader();
            Document document 
    = reader.read(file);
            
    return document;
        }

        
    /**
         * 根據(jù)root 元素的rootName,Element元素的javabean對(duì)象以及Element元素的elementName創(chuàng)建Document對(duì)象并返回。
         
    */


        
    public static Document createDocument(String rootName,Banner banner,String elementName) {
            Document doc 
    = DocumentHelper.createDocument();
            Element root 
    = doc.addElement(rootName);
            setDocumentHeader(root);
            addElement(root,banner,elementName);
            
    return doc;
        }

        
    /**
         * 根據(jù)Element元素的javabean對(duì)象以及Element元素的elementName在Element root下增加一個(gè)Element 。
         
    */

        
    public static void addElement(Element root, Banner banner,String elementName) {
            Element element 
    = root.addElement(elementName);
            element.addAttribute(DOM4jConstant.ELEMENT_ATTRIBUTE_IMG, banner.getImg()).addAttribute(DOM4jConstant.ELEMENT_ATTRIBUTE_BGURL, banner.getBnurl()).addAttribute(DOM4jConstant.ELEMENT_ATTRIBUTE_BNTARGET, banner.getBntarget()).addText(
    "");
        }

        
    /**
         * 在Document對(duì)象中,以elementName,attributeName,attributeValue為參數(shù)刪除對(duì)應(yīng)的Element元素。
         
    */

        
    public static Document removeElementByAttribute(Document document, String elementName,String attributeName,String attributeValue) {
              List
    <Element> list = document.getRootElement().elements(elementName);        
            exit:   
    for (Element element : list) {
                 
    for(Iterator it=element.attributeIterator();it.hasNext();){
                     Attribute attribute 
    = (Attribute) it.next();   
                     
    if(attribute.getName().equals(attributeName)&&attribute.getValue().equals(attributeValue)){
                         element.getParent().remove(element);
                         
    break exit;
                     }

                 }
               
            }

              
    return document ;        
        }

        
    /**
         * 把Document對(duì)象與filePath對(duì)應(yīng)的物理文件進(jìn)行同步。
         
    */

        
    public static void writeDocumentToFile(Document document, String filePath)throws IOException {
            XMLWriter writer 
    = new XMLWriter(new FileWriter(filePath));
            writer.write(document);
            writer.flush();
            writer.close();
        }

        
    /**
         * 設(shè)置Document對(duì)象的文件頭。
         
    */

        
    public static void setDocumentHeader(Element root) {
            Element fixedbn 
    = root.addElement("fixedbn");
            fixedbn.addElement(
    "fixedlink").addAttribute("posx""0")
                                           .addAttribute(
    "posy""0")
                                           .addAttribute(
    "scalex""1510")
                                           .addAttribute(
    "scaley""800")
                                           .addAttribute(
    "bnurl""http://www.lanrentuku.com/")
                                           .addAttribute(
    "bntarget""_self")
                                           .addText(
    "");
            fixedbn.addElement(
    "fixedlink").addAttribute("posx""0")
                                           .addAttribute(
    "posy""80")
                                           .addAttribute(
    "scalex""1510")
                                           .addAttribute(
    "scaley""1150")
                                           .addAttribute(
    "bnurl""http://www.lanrentuku.com/")
                                           .addAttribute(
    "bntarget""_self")
                                           .addText(
    "");
            fixedbn.addAttribute(
    "alpha_num""0");
        }


    }




    package net.better_best.www.dynamicADPics.utils;

    public class DOM4jConstant {
        
    public static final String ELEMENT_ATTRIBUTE_IMG  = "img";
        
    public static final String ELEMENT_ATTRIBUTE_BGURL  = "bnurl";
        
    public static final String ELEMENT_ATTRIBUTE_BNTARGET  = "bntarget";
        
        
    public static final String FILE_PATH_NAME  = "/WEB-INF/xml.xml";

    }







    package net.better_best.www.dynamicADPics.utils;

    public class Banner {
        
    private String img;
        
    private String bnurl;
        
    private String bntarget;
        
        
    public String getImg() {
            
    return img;
        }

        
    public void setImg(String img) {
            
    this.img = img;
        }

        
    public String getBnurl() {
            
    return bnurl;
        }

        
    public void setBnurl(String bnurl) {
            
    this.bnurl = bnurl;
        }

        
    public String getBntarget() {
            
    return bntarget;
        }

        
    public void setBntarget(String bntarget) {
            
    this.bntarget = bntarget;
        }

        

    }




    package net.better_best.www.dynamicADPic.action;

    import java.util.List;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import net.better_best.www.dynamicADPic.form.DynamicADPicForm;
    import net.better_best.www.dynamicADPics.utils.Banner;
    import net.better_best.www.dynamicADPics.utils.DOM4jConstant;
    import net.better_best.www.dynamicADPics.utils.DOM4jUtils;

    import org.apache.struts.action.ActionForm;
    import org.apache.struts.action.ActionForward;
    import org.apache.struts.action.ActionMapping;
    import org.apache.struts.actions.DispatchAction;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;

    public class DynamicADPicAction extends DispatchAction {
        @Override
        
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                
    throws Exception {
            
    return super.execute(mapping, form, request, response);
        }

         
    public  ActionForward add(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
             String filePath
    =request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
              Document document 
    = DOM4jUtils.getDocument(filePath);
              DynamicADPicForm dynamicADPic 
    = (DynamicADPicForm)form ;
              String elementName
    ="banner";
              Banner banner 
    = new Banner();
                banner.setImg(dynamicADPic.getImg());
                banner.setBnurl(dynamicADPic.getBnurl());
                banner.setBntarget(
    "_blank");
              DOM4jUtils.addElement(document.getRootElement(), banner, elementName);  
              DOM4jUtils.writeDocumentToFile(document, filePath);
            
    return  this.list(mapping, form, request, response);
            }

         
    public  ActionForward list(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
                                
             String elementName 
    = "banner";
             String filePath
    =request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
             Document document 
    = null;
                
    try {
                     document 
    = DOM4jUtils.getDocument(filePath) ;
                }
     catch (DocumentException e) {
                    init(request);
                     document 
    = DOM4jUtils.getDocument(filePath) ;
                }

            
              List list 
    = DOM4jUtils.parseElementsToObjects(document.getRootElement(), elementName); 
                request.setAttribute(
    "piclist", list);
                request.getSession().setAttribute(
    "n", request.getParameter("n"));
                
    return mapping.findForward("list");
            }

         
    public  ActionForward delete(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
             String filePath
    =request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;     
             Document document 
    = DOM4jUtils.getDocument(filePath);
             document 
    = DOM4jUtils.removeElementByAttribute(document, "banner""img", request.getParameter("img"));
             DOM4jUtils.writeDocumentToFile(document, filePath);
             
    return  this.list(mapping, form, request, response);
            }

         
    public  void  init(HttpServletRequest request) throws Exception {
                Banner banner 
    = new Banner();
                banner.setImg(
    "images/01.jpg");
                banner.setBnurl(
    "http://www.lanrentuku.com/");
                banner.setBntarget(
    "_blank");
                String elementName
    ="banner";
                String rootName
    ="banner_swf";
                Document document 
    = DOM4jUtils.createDocument(rootName, banner, elementName);
                String filePath
    =request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
                DOM4jUtils.writeDocumentToFile(document, filePath);
               
            }

    }



    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产成人亚洲精品91专区手机| 欧亚一级毛片免费看| 在线美女免费观看网站h| 青青草原亚洲视频| 人成免费在线视频| 亚洲精品第一国产综合精品99| 美女被暴羞羞免费视频| 免费永久在线观看黄网站| 边摸边吃奶边做爽免费视频99| 免费国产小视频在线观看| 五级黄18以上免费看| 国产成人精品日本亚洲专区61 | 亚洲成a人片毛片在线| 91精品导航在线网址免费| 亚洲不卡视频在线观看| 永久免费av无码网站大全| 美女被爆羞羞网站在免费观看| 国产精品亚洲w码日韩中文| a级日本高清免费看| 久久精品国产亚洲AV无码麻豆| 午夜福利不卡片在线播放免费| 亚洲熟女综合色一区二区三区| 亚洲av中文无码| 最新久久免费视频| 亚洲国产亚洲片在线观看播放 | 亚洲人成网站观看在线播放| 中文字幕av无码不卡免费| 久久久久亚洲AV片无码下载蜜桃 | 亚洲精品一卡2卡3卡三卡四卡| 在线看片无码永久免费视频| 久久精品国产亚洲AV电影网| 亚洲熟妇无码另类久久久| 91嫩草免费国产永久入口| 风间由美在线亚洲一区| 亚洲精品无码av人在线观看 | 一级一级毛片免费播放| 久久夜色精品国产噜噜亚洲AV| 妞干网免费视频观看| 中国一级全黄的免费观看| 亚洲 日韩经典 中文字幕| 亚洲精品中文字幕乱码三区|