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

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

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

    隨筆 - 154  文章 - 60  trackbacks - 0
    <2007年11月>
    28293031123
    45678910
    11121314151617
    18192021222324
    2526272829301
    2345678

    聲明:

    該blog是為了收集資料,認識朋友,學(xué)習(xí)、提高技術(shù),所以本blog的內(nèi)容除非聲明,否則一律為轉(zhuǎn)載!!

    感謝那些公開自己技術(shù)成果的高人們!!!

    支持開源,尊重他人的勞動!!

    常用鏈接

    留言簿(3)

    隨筆分類(148)

    隨筆檔案(143)

    收藏夾(2)

    其他

    學(xué)習(xí)(技術(shù))

    觀察思考(非技術(shù))

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    由于需要對XML文件進行操作。所以上網(wǎng)找了一此資料。
    用了一下org.w3c.dom解析,不太方便,特別是進行修改時。
    后來找到了dom4j這個工具包。方便多了。呵。。記錄一下,以后用得著。


    首先通過org.w3c.dom解析
    InitFromXML.java

    package system.init;

    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Set;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    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.xml.sax.SAXException;


    /**
    * @author yymoth
    *
    */
    public class InitFromXML {

        private HashMap prop = new HashMap(); //存儲從文件中讀取的所有配置信息
        private HashMap temp = new HashMap(); //傳入的參數(shù),用來生成新的XML配置文件
        private static String xmlFileName ="SystemInit.xml";
        private static String xmlFilePath = InitFromXML.class.getClassLoader().getResource(xmlFileName).getPath();
        
        private static InitFromXML instance = null;
        
        public static synchronized InitFromXML getInstance()
        {
            if(instance == null)
            {    instance = new InitFromXML();    }
            
            return instance;
        }
        
        /**
         *
         */
        public InitFromXML() {
            super();
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            new InitFromXML().getSystemProperty();
            
            HashMap temp = new HashMap();
            temp.put("outputImageDevice","outputImageDeviceVVVVV");
            temp.put("inputSceneFilePath","inputSceneFilePathVVVVV");
            temp.put("fetchIRADFactor",0.11);
            temp.put("fcpr_upLimit",111);
            
            new InitFromXML().updateSystemProperty(temp);
        
        }

        /**
         * 把xml文件讀取內(nèi)容,在內(nèi)存中產(chǎn)生dom樹; 修改它的內(nèi)容不會影響文件;
         * @return
         */
        public HashMap getSystemProperty()
        {        
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db;
            try {
                db = dbf.newDocumentBuilder();
                System.out.println("開始讀取配置文件= " + xmlFilePath);
                Document d = db.parse(xmlFilePath);
                
                NodeList nl = d.getElementsByTagName("outputImageDevice");
                Node mynode = nl.item(0);
                String outputImageDevice = mynode.getFirstChild().getNodeValue();
                prop.put("outputImageDevice",outputImageDevice);
                
                System.out.println("輸出路徑= "+prop.get("outputImageDevice").toString());
                
                nl = d.getElementsByTagName("inputSceneFilePath");
                mynode = nl.item(0);
                String inputSceneFilePath = mynode.getFirstChild().getNodeValue();
                prop.put("inputSceneFilePath",inputSceneFilePath);
                
                System.out.println("輸入場景路徑= "+prop.get("inputSceneFilePath"));
                
                
                nl = d.getElementsByTagName("fetchIRADFactor");
                mynode = nl.item(0);
                String fetchIRADFactor= mynode.getFirstChild().getNodeValue();
                prop.put("fetchIRADFactor",fetchIRADFactor);
                
                System.out.println("空閑Render選擇因子= "+prop.get("fetchIRADFactor"));
                
                
                nl = d.getElementsByTagName("fcpr_upLimit");
                mynode = nl.item(0);
                int fcpr_upLimit = Integer.parseInt(mynode.getFirstChild().getNodeValue().toString().trim());
                prop.put("fcpr_upLimit",fcpr_upLimit);
                
                System.out.println("單臺Render最大渲染幀數(shù)= "+prop.get("fcpr_upLimit"));
                
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return this.prop;
        }
        
        
        /**
         * 更新系統(tǒng)參數(shù)
         *
         */
        public void updateSystemProperty(HashMap temp)
        {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = null;
            
            try {
                db = dbf.newDocumentBuilder();
                Document doc = db.parse(xmlFilePath);
                
                
                NodeList nl = doc.getElementsByTagName("outputImageDevice");
                Node mynode = nl.item(0);
                mynode.setTextContent((String)temp.get("outputImageDevice"));
                //mynode.setNodeValue((String)temp.get("outputImageDevice"));
            
                
                nl = doc.getElementsByTagName("inputSceneFilePath");
                mynode = nl.item(0);
                mynode.setTextContent((String)temp.get("inputSceneFilePath"));
            
                nl = doc.getElementsByTagName("fetchIRADFactor");
                mynode = nl.item(0);
                mynode.setTextContent((String)temp.get("fetchIRADFactor"));
                
                nl = doc.getElementsByTagName("fcpr_upLimit");
                mynode = nl.item(0);
                mynode.setTextContent((String)temp.get("fcpr_upLimit"));
                
                
                writeToXML(doc);
                
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
        
            
            
            
        }
        
        
        /**
         * 把Document對象生成文件;
         * @param doc
         */
        
        public void writeToXML(Document doc)
        {
            TransformerFactory tfactory = TransformerFactory.newInstance();
            try {
                Transformer tf = tfactory.newTransformer();
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(new File(xmlFilePath));
                tf.transform(source,result);
            } catch (TransformerConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (TransformerException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
    }




    下面這個是通過DOM4J, 使用之前要下載dom4j包。官方站點:www.dom4j.org

    Dom4j.java


    package wcrs_master.test;

    /**
    * @author yymoth
    *
    */
    import java.io.IOException;
    import java.io.File;
    import java.io.UnsupportedEncodingException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    import org.dom4j.io.SAXReader;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Node;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.Attribute;

    import system.init.InitFromXML;

    import java.io.FileWriter;

    public class Dom4j {
        private static String xmlFileName ="SystemInit.xml";
        private static String xmlFilePath = Dom4j.class.getClassLoader().getResource(xmlFileName).getPath();
        private static Dom4j instance = null;
        public Dom4j() {
        }

        public static synchronized Dom4j getInstance()
        {
            if(instance == null)
            {    instance = new Dom4j();    }
            
            return instance;
        }
        
        
        
        public Document parse(String sfile) throws DocumentException {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(sfile));
            return document;
        }

        public void update(HashMap temp) throws IOException {
            Document document = null;
                try {
                    document = new Dom4j().parse(xmlFilePath);
                    Element root = document.getRootElement(); //得到根節(jié)點目錄
                    Iterator iter = root.elementIterator();
                    System.out.println("\r\n****** 獲取的數(shù)據(jù)如下 ******");
                    System.out.println(xmlFilePath);
                    while (iter.hasNext()) {
                        Element titleElement = (Element) iter.next();
                        // 修改xml元素
                        System.out.print(titleElement.getName()+"  ==  ");
                        System.out.println(titleElement.getData().toString());
                        
                        if (titleElement.getName().equals("outputImageDevice")) {
                            titleElement.setText(temp.get("outputImageDevice").toString());
                        }
                        if (titleElement.getName().equals("fetchIRADFactor")) {
                            titleElement.setText(temp.get("fetchIRADFactor").toString());
                        }
                        if (titleElement.getName().equals("inputSceneFilePath")) {
                            titleElement.setText(temp.get("inputSceneFilePath").toString());
                        }
                        if (titleElement.getName().equals("fcpr_upLimit")) {
                            titleElement.setText(temp.get("fcpr_upLimit").toString());
                        }
                    }

                
                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                writeToXML(document);

        }

        /**
         * 把xml文件讀取內(nèi)容,在內(nèi)存中產(chǎn)生dom樹; 修改它的內(nèi)容不會影響文件;
         * @return
         */
        public HashMap getSystemProperty()
        {
            HashMap temp = new HashMap();
            Document document ;
            try {
                document = new Dom4j().parse(xmlFilePath);
                Element root = document.getRootElement(); //得到根節(jié)點目錄
                Iterator iter = root.elementIterator();
                while(iter.hasNext())
                {
                    Element titleElement = (Element) iter.next();
                    temp.put(titleElement.getName(),titleElement.getData());
                }
                
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            return temp;
        }
        
        
        /**
         * 寫入文件
         * @param document
         */
        
        public void writeToXML(Document document)
        {
            // 輸出全部原始數(shù)據(jù),在編譯器中顯示
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer;
            try {
                writer = new XMLWriter(System.out, format);
                //System.out.println("\r\n------------------Start------------------");
                writer.write(document);  
                //System.out.println("\r\n-------------------End-------------------");
                writer.close();
                // 輸出全部原始數(shù)據(jù),并用它生成新的我們需要的XML文件
                XMLWriter writer2 = new XMLWriter(new FileWriter(new File(
                        xmlFilePath)), format);
                writer2.write(document); //輸出到文件
                writer2.close();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
        }
        
        
        
        public static void main(String[] args) {
            
            HashMap temp = new HashMap();
            
            temp.put("outputImageDevice","outputImageDeviceVVVVV");
            temp.put("inputSceneFilePath","inputSceneFilePathVVVVV");
            temp.put("fetchIRADFactor",0.11);
            temp.put("fcpr_upLimit",11);
            
            Dom4j dom4j = new Dom4j();
            try {
                dom4j.update(temp);

            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }

    posted on 2007-11-21 14:02 lk 閱讀(768) 評論(0)  編輯  收藏 所屬分類: xml
    主站蜘蛛池模板: 亚洲免费在线观看视频| 黄色网页在线免费观看| 免费又黄又硬又爽大片| 三上悠亚在线观看免费| 色屁屁在线观看视频免费| 亚洲毛片不卡av在线播放一区| 野花香高清视频在线观看免费 | 久久不见久久见中文字幕免费 | 免费看片在线观看| 国产成人人综合亚洲欧美丁香花| 久久精品免费视频观看| 亚洲中文字幕无码av永久| 久久久久亚洲精品无码网址| 亚洲精品在线免费看| 黄页网址大全免费观看12网站 | 一级人做人a爰免费视频| 亚洲日本乱码一区二区在线二产线| 免费无码又爽又刺激毛片| 国内精品免费视频精选在线观看 | 国产成人免费a在线资源| 久久免费观看国产99精品| 亚洲Av永久无码精品黑人| 高清国语自产拍免费视频国产| 成人a毛片免费视频观看| 亚洲国产精品线观看不卡| 中文字幕不卡亚洲 | 久久亚洲精品无码aⅴ大香| 亚洲 小说区 图片区 都市| 黄在线观看www免费看| 中文字幕手机在线免费看电影| 亚洲成在人线aⅴ免费毛片| 久久亚洲中文字幕精品有坂深雪| 永久免费视频网站在线观看| 国产免费牲交视频免费播放| 亚洲中文字幕无码亚洲成A人片| 亚洲V无码一区二区三区四区观看 亚洲αv久久久噜噜噜噜噜 | 无人视频在线观看免费播放影院| 91亚洲精品自在在线观看| 国产成人亚洲综合色影视| 最近中文字幕高清免费中文字幕mv| 亚洲国产系列一区二区三区|