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

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

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

    甜咖啡

    我的IT空間

    java對xml文件做增刪改查

    package com.wss;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Text;

    public class GPS_GNSS_XML_Color {
        //刪除節(jié)點(diǎn)時傳入的參數(shù)
        private static String deleteNumber;
        
        //修改節(jié)點(diǎn)時傳入的參數(shù)
        private static String updateNumber;
        
        //讀取傳入的路徑,返回一個document對象
        public static Document loadInit(String filePath){
            Document document = null;
            try{
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                document = builder.parse(new File(filePath));
                document.normalize();
                return document;
            }catch(Exception e){
                e.printStackTrace();
                System.out.println(e.getMessage());
                return null;
            }
        }

        /**
         * 刪除制定的xml
         * @param filePath
         * @return
         */
        public static boolean deleteXML(String filePath){
            deleteNumber = "421f481e-790c-41be-91e3-27d215b73ce2";
            Document document = loadInit(filePath);
            try{
                NodeList nodeList = document.getElementsByTagName("color");
                for(int i=0; i<nodeList.getLength(); i++){
                    String number_ = document.getElementsByTagName("number").item(i).getFirstChild().getNodeValue();
                    //刪除節(jié)點(diǎn)時傳入的參數(shù)
                    if(number_.equals(deleteNumber)){
                        Node node = nodeList.item(i);
                        node.getParentNode().removeChild(node);
                        saveXML(document, filePath);
                    }
                }
                return true;
            }catch(Exception e){
                e.printStackTrace();
                System.out.println(e.getMessage());
                return false;
            }
        }
        
        /**
         * 修改制定的xml
         * @param filePath
         * @return
         */
        public static boolean updateXML(String filePath){
            updateNumber = "421f481e-790c-41be-91e3-27d215b73ce2";
             //讀取傳入的路徑,返回一個document對象
             Document document = loadInit(filePath);
             try{
                //獲取葉節(jié)點(diǎn)
                 NodeList nodeList = document.getElementsByTagName("color");
                //遍歷葉節(jié)點(diǎn)
                 for(int i=0; i<nodeList.getLength(); i++){
                     String number = document.getElementsByTagName("number").item(i).getFirstChild().getNodeValue();
                     String colorValue = document.getElementsByTagName("colorValue").item(i).getFirstChild().getNodeValue();
                     Double minValue = Double.parseDouble(document.getElementsByTagName("minValue").item(i).getFirstChild().getNodeValue());
                     Double maxValue = Double.parseDouble(document.getElementsByTagName("maxValue").item(i).getFirstChild().getNodeValue());
                     //修改節(jié)點(diǎn)時傳入的參數(shù)
                     if(number.equals(updateNumber)){
                         document.getElementsByTagName("colorValue").item(i).getFirstChild().setNodeValue("black");
                         document.getElementsByTagName("minValue").item(i).getFirstChild().setNodeValue("2222");
                         document.getElementsByTagName("maxValue").item(i).getFirstChild().setNodeValue("22222");
                         System.out.println();
                     }
                 }
                 saveXML(document, filePath);
                 return true;
             }catch(Exception e){
                 e.printStackTrace();
                 System.out.println(e.getMessage());
                 return false;
             }
        }
        
        /**
         * 添加節(jié)點(diǎn)
         * @param filePath
         * @return
         */
        public static boolean addXML(String filePath){
            try{
                //讀取傳入的路徑,返回一個document對象
                Document document = loadInit(filePath);
                //創(chuàng)建葉節(jié)點(diǎn)
                Element eltColor = document.createElement("color");
                Element eltNumber = document.createElement("number");//創(chuàng)建葉節(jié)點(diǎn)的第一個元素
                Element eltColorValue = document.createElement("colorValue");//創(chuàng)建葉節(jié)點(diǎn)的第二個元素
                Element eltMinValue = document.createElement("minValue");//創(chuàng)建葉節(jié)點(diǎn)的第三個元素
                Element eltMaxValue = document.createElement("maxValue");//創(chuàng)建葉節(jié)點(diǎn)的第四個元素
                Text number_ = document.createTextNode(UUID.randomUUID().toString());//創(chuàng)建葉節(jié)點(diǎn)的第一個元素下的文本節(jié)點(diǎn)
                eltNumber.appendChild(number_);//把該文本節(jié)點(diǎn)加入到葉節(jié)點(diǎn)的第一個元素里面
                Text colorValue_ = document.createTextNode("colorValue");//創(chuàng)建葉節(jié)點(diǎn)的第二個元素下的文本節(jié)點(diǎn)
                eltColorValue.appendChild(colorValue_);//把該文本節(jié)點(diǎn)加入到葉節(jié)點(diǎn)的第二個元素里面
                Text minValue_ = document.createTextNode("100");//創(chuàng)建葉節(jié)點(diǎn)的第三個元素下的文本節(jié)點(diǎn)
                eltMinValue.appendChild(minValue_);//把該文本節(jié)點(diǎn)加入到葉節(jié)點(diǎn)的第三個元素里面
                Text maxValue_ = document.createTextNode("200");//創(chuàng)建葉節(jié)點(diǎn)的第四個元素下的文本節(jié)點(diǎn)
                eltMaxValue.appendChild(maxValue_);//把該文本節(jié)點(diǎn)加入到葉節(jié)點(diǎn)的第四個元素里面
                //把葉節(jié)點(diǎn)下的元素加入到葉節(jié)點(diǎn)下
                eltColor.appendChild(eltNumber);
                eltColor.appendChild(eltColorValue);
                eltColor.appendChild(eltMinValue);
                eltColor.appendChild(eltMaxValue);
                //獲取根節(jié)點(diǎn)
                Element eltRoot = document.getDocumentElement();
                //把葉節(jié)點(diǎn)加入到根節(jié)點(diǎn)下
                eltRoot.appendChild(eltColor);
                //更新修改后的源文件
                saveXML(document, filePath);
                return true;
            }catch(Exception e){
                e.printStackTrace();
                System.out.println(e.getMessage());
                return false;
            }
        }
        
        /**
         * 把修改后的document寫進(jìn)源文件(更新源文件)
         * @param document
         * @param filePath
         * @return
         */
        public static boolean saveXML(Document document, String filePath){
            try{
                TransformerFactory tFactory = TransformerFactory.newInstance();
                Transformer transformer = tFactory.newTransformer();
                
                DOMSource source = new DOMSource(document);
                StreamResult result = new StreamResult(new File(filePath));
                transformer.transform(source, result);
                return true;
            }catch(Exception e){
                e.printStackTrace();
                System.out.println(e.getMessage());
                return false;
            }
        }
        
        /**
         * 獲取xml文件的所有記錄
         * @param filePath
         * @return
         */
        public static List<ColorValue> selectXML(String filePath){
             List<ColorValue> colorValueList = new ArrayList<ColorValue>();
             try{
                 //讀取傳入的路徑,返回一個document對象
                 Document document = loadInit(filePath);
                 //獲取葉節(jié)點(diǎn)
                 NodeList nodeList = document.getElementsByTagName("color");
                 //遍歷葉節(jié)點(diǎn)
                 for(int i=0; i<nodeList.getLength(); i++){
                     ColorValue colorValue = new ColorValue();
                     String number_ = document.getElementsByTagName("number").item(i).getFirstChild().getNodeValue();
                     String colorValue_ = document.getElementsByTagName("colorValue").item(i).getFirstChild().getNodeValue();
                     Double minValue_ = Double.parseDouble(document.getElementsByTagName("minValue").item(i).getFirstChild().getNodeValue());
                     Double maxValue_ = Double.parseDouble(document.getElementsByTagName("maxValue").item(i).getFirstChild().getNodeValue());
                     colorValue.setNumber(number_);
                     colorValue.setColorValue(colorValue_);
                     colorValue.setMinValue(minValue_);
                     colorValue.setMaxValue(maxValue_);
                     colorValueList.add(colorValue);
                 }
                 return colorValueList;
             }catch(Exception e){
                 e.printStackTrace();
                 System.out.println(e.getMessage());
                 return null;
             }
        }
    }






    package com.wss;

    public class ColorValue {

        private String number;
        private String colorValue;
        private Double minValue;
        private Double maxValue;
        public String getNumber() {
            return number;
        }
        public void setNumber(String number) {
            this.number = number;
        }
        public String getColorValue() {
            return colorValue;
        }
        public void setColorValue(String colorValue) {
            this.colorValue = colorValue;
        }
        public Double getMinValue() {
            return minValue;
        }
        public void setMinValue(Double minValue) {
            this.minValue = minValue;
        }
        public Double getMaxValue() {
            return maxValue;
        }
        public void setMaxValue(Double maxValue) {
            this.maxValue = maxValue;
        }
    }



    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Colors>
        <color>
            <number>7007b384-fab3-4779-9171-229d0664b6b5</number>
            <colorValue>black</colorValue>
            <minValue>2222</minValue>
            <maxValue>22222</maxValue>
        </color>
        <color>
            <number>421f481e-790c-41be-91e3-27d215b73ce2</number>
            <colorValue>colorValue</colorValue>
            <minValue>100</minValue>
            <maxValue>200</maxValue>
        </color>
    </Colors>

    posted on 2011-11-08 21:56 甜咖啡 閱讀(9560) 評論(1)  編輯  收藏

    評論

    # re: java對xml文件做增刪改查 2014-09-27 21:29

    非常好,謝謝!!  回復(fù)  更多評論   


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


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    <2011年11月>
    303112345
    6789101112
    13141516171819
    20212223242526
    27282930123
    45678910

    統(tǒng)計

    常用鏈接

    留言簿(1)

    我參與的團(tuán)隊

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 1000部拍拍拍18勿入免费视频软件 | 亚洲嫩草影院久久精品| 少妇太爽了在线观看免费视频| 亚洲精品网站在线观看你懂的| 我想看一级毛片免费的| 一级女性全黄生活片免费看| 99久久亚洲综合精品成人网| 狼友av永久网站免费观看| 你是我的城池营垒免费看| 亚洲人成片在线观看| 亚洲欧洲中文日韩av乱码| 最近2019免费中文字幕视频三| 老子影院午夜伦不卡亚洲| 亚洲av无码专区国产乱码在线观看| 噼里啪啦免费观看高清动漫4| 日韩大片免费观看视频播放 | 久久久青草青青国产亚洲免观 | 四虎影院在线免费播放| 国色精品va在线观看免费视频| 亚洲综合av一区二区三区不卡| 国产亚洲精AA在线观看SEE | 亚洲av无码一区二区三区天堂古代| 免费人成视频x8x8入口| xxxx日本免费| 中文字幕在线免费观看视频| 亚洲精品国产摄像头| 亚洲网站在线播放| 亚洲欧洲久久久精品| 在线免费观看色片| 免费福利在线播放| 国产婷婷成人久久Av免费高清| 亚洲av综合av一区二区三区| 亚洲小说区图片区| 亚洲AV综合色一区二区三区| 亚洲一级特黄大片在线观看 | 亚洲精品午夜视频| 亚洲国产精彩中文乱码AV| 全部免费a级毛片| 女人18特级一级毛片免费视频| 18禁美女黄网站色大片免费观看 | 一区二区免费视频|