<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 {
        //刪除節點時傳入的參數
        private static String deleteNumber;
        
        //修改節點時傳入的參數
        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();
                    //刪除節點時傳入的參數
                    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{
                //獲取葉節點
                 NodeList nodeList = document.getElementsByTagName("color");
                //遍歷葉節點
                 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());
                     //修改節點時傳入的參數
                     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;
             }
        }
        
        /**
         * 添加節點
         * @param filePath
         * @return
         */
        public static boolean addXML(String filePath){
            try{
                //讀取傳入的路徑,返回一個document對象
                Document document = loadInit(filePath);
                //創建葉節點
                Element eltColor = document.createElement("color");
                Element eltNumber = document.createElement("number");//創建葉節點的第一個元素
                Element eltColorValue = document.createElement("colorValue");//創建葉節點的第二個元素
                Element eltMinValue = document.createElement("minValue");//創建葉節點的第三個元素
                Element eltMaxValue = document.createElement("maxValue");//創建葉節點的第四個元素
                Text number_ = document.createTextNode(UUID.randomUUID().toString());//創建葉節點的第一個元素下的文本節點
                eltNumber.appendChild(number_);//把該文本節點加入到葉節點的第一個元素里面
                Text colorValue_ = document.createTextNode("colorValue");//創建葉節點的第二個元素下的文本節點
                eltColorValue.appendChild(colorValue_);//把該文本節點加入到葉節點的第二個元素里面
                Text minValue_ = document.createTextNode("100");//創建葉節點的第三個元素下的文本節點
                eltMinValue.appendChild(minValue_);//把該文本節點加入到葉節點的第三個元素里面
                Text maxValue_ = document.createTextNode("200");//創建葉節點的第四個元素下的文本節點
                eltMaxValue.appendChild(maxValue_);//把該文本節點加入到葉節點的第四個元素里面
                //把葉節點下的元素加入到葉節點下
                eltColor.appendChild(eltNumber);
                eltColor.appendChild(eltColorValue);
                eltColor.appendChild(eltMinValue);
                eltColor.appendChild(eltMaxValue);
                //獲取根節點
                Element eltRoot = document.getDocumentElement();
                //把葉節點加入到根節點下
                eltRoot.appendChild(eltColor);
                //更新修改后的源文件
                saveXML(document, filePath);
                return true;
            }catch(Exception e){
                e.printStackTrace();
                System.out.println(e.getMessage());
                return false;
            }
        }
        
        /**
         * 把修改后的document寫進源文件(更新源文件)
         * @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);
                 //獲取葉節點
                 NodeList nodeList = document.getElementsByTagName("color");
                 //遍歷葉節點
                 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 甜咖啡 閱讀(9566) 評論(1)  編輯  收藏

    評論

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

    非常好,謝謝!!  回復  更多評論   


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


    網站導航:
     

    導航

    <2014年9月>
    31123456
    78910111213
    14151617181920
    21222324252627
    2829301234
    567891011

    統計

    常用鏈接

    留言簿(1)

    我參與的團隊

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 三级毛片在线免费观看| 在线a亚洲老鸭窝天堂av高清| 美女免费精品高清毛片在线视| 97碰公开在线观看免费视频| 久久久久亚洲精品成人网小说| 成人A毛片免费观看网站| 亚洲精品国产精品国自产观看 | jlzzjlzz亚洲jzjzjz| 一级毛片在线免费观看| 亚洲va久久久噜噜噜久久天堂| 黄色免费网站在线看| 在线观看亚洲免费视频| 亚洲日本VA午夜在线影院| 女人18一级毛片免费观看| 日韩亚洲人成在线| 国产免费直播在线观看视频| 日本激情猛烈在线看免费观看| 亚洲欧洲久久av| 国产精品成人免费观看| 亚洲精品无码永久在线观看你懂的| 黄色网址免费在线| 亚洲Av无码乱码在线观看性色 | av片在线观看永久免费| 手机看片久久国产免费| 久久免费视频一区| 亚洲婷婷国产精品电影人久久| 四虎1515hh永久久免费| 亚洲国产精品成人精品无码区在线 | 麻豆成人久久精品二区三区免费| 九月婷婷亚洲综合在线| 国产V片在线播放免费无码| 免费欧洲毛片A级视频无风险| 久久99精品视免费看| 亚洲国产精品一区二区久久| 69av免费观看| 亚洲国产综合AV在线观看| 亚洲另类视频在线观看| a毛片在线还看免费网站| 国产国拍亚洲精品福利 | 亚洲综合另类小说色区| 久久久久亚洲AV无码去区首|