johnsdilon

          Save Document object to XML file

            1
            2    
            3    /**
            4     * write to the .xml file
            5     * @param doc
            6     * @param encoding
            7     * @param file
            8     * @param tab
            9     */

           10    public void write2xml(Document doc, String encoding, String file, int tab) {
           11        if (doc == null{
           12            return;
           13        }

           14        StringBuffer sb = new StringBuffer();
           15        // head
           16        sb.append("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n");
           17        this.writeFormal(doc.getDocumentElement(), sb, tab);
           18        // save data from memory to the *.xml file
           19        FileOutputStream fos = null;
           20        BufferedOutputStream bos = null;
           21        try {
           22            fos = new FileOutputStream(file);
           23            bos = new BufferedOutputStream(fos);
           24            bos.write(sb.toString().getBytes(encoding));
           25            bos.close();
           26            fos.close();
           27        }
           catch (FileNotFoundException e) {
           28            e.printStackTrace();
           29            try {
           30                if (bos != null{
           31                    bos.close();
           32                }

           33                if (fos != null{
           34                    fos.close();
           35                }

           36            }
           catch (IOException e1) {
           37                e1.printStackTrace();
           38            }

           39        }
           catch (IOException e) {
           40            e.printStackTrace();
           41            try {
           42                if (bos != null{
           43                    bos.close();
           44                }

           45                if (fos != null{
           46                    fos.close();
           47                }

           48            }
           catch (IOException e1) {
           49                e1.printStackTrace();
           50            }

           51        }

           52    }

           53    
           54    /** private function */
           55    
           56    private void writeFormal(Element e, StringBuffer sb, int intTab) {
           57        org.w3c.dom.NodeList list = e.getElementsByTagName("*");
           58        // if e has not child
           59        if (list != null{
           60            // blank
           61            for (int i=0; i<intTab; i++{
           62                sb.append("\t");
           63            }

           64            // node name
           65            sb.append("<" + e.getNodeName() + ">\n");
           66            // node value
           67            if (list.getLength() == 0{
           68                String value = e.getFirstChild().getNodeValue().trim();
           69                if (value.length() > 0{
           70                    // blank
           71                    for (int i=0; i<intTab; i++{
           72                        sb.append("\t");
           73                    }

           74                    sb.append("\t" + value + "\n");
           75                }

           76            }

           77            else {
           78                org.w3c.dom.Node node;
           79                Element eChild;
           80                // write the child
           81                org.w3c.dom.NodeList listChild = e.getChildNodes();
           82                for (int i=0; i<list.getLength(); i++{
           83                    node = list.item(i);
           84                    // check if "node" is the close child
           85                    boolean bBeChild = false;
           86                    for (int k=0; k<listChild.getLength(); k++{
           87                        if (listChild.item(k).equals(node)) {
           88                            bBeChild = true;
           89                            break;
           90                        }

           91                    }

           92                    // if "node" is the close child
           93                    if (bBeChild == true{
           94                        eChild = (Element) node;
           95                        this.writeFormal(eChild, sb, intTab+1);
           96                    }
           // if (bBeChild == true)
           97                }
           // for (int i=0; i<list.getLength(); i++)
           98            }

           99            // blank
          100            for (int i=0; i<intTab; i++{
          101                sb.append("\t");
          102            }

          103            // end of node name
          104            sb.append("</" + e.getNodeName() + ">\n");
          105        }
           // if (list != null)
          106    }

          posted on 2007-12-30 20:04 johnsdilon 閱讀(281) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          留言簿

          文章分類

          最新評論

          主站蜘蛛池模板: 中山市| 会昌县| 泸州市| 昔阳县| 海城市| 台北市| 鱼台县| 霍邱县| 遂溪县| 筠连县| 广汉市| 民县| 孝感市| 玉溪市| 屏山县| 大石桥市| 钦州市| 明光市| 承德县| 霍城县| 潜山县| 桑日县| 饶阳县| 民权县| 当雄县| 新民市| 淮北市| 囊谦县| 吉安市| 越西县| 巴塘县| 富平县| 凯里市| 秦安县| 毕节市| 象州县| 江门市| 丹棱县| 福清市| 分宜县| 沿河|