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 閱讀(284) 評論(0)  編輯  收藏


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


          網站導航:
           

          導航

          留言簿

          文章分類

          最新評論

          主站蜘蛛池模板: 甘肃省| 新疆| 阿巴嘎旗| 霸州市| 资兴市| 连江县| 长岛县| 安康市| 东至县| 汶上县| 江川县| 营山县| 长治县| 黄山市| 诏安县| 莆田市| 富裕县| 松阳县| 繁昌县| 北安市| 澄城县| 长垣县| 抚松县| 泸水县| 卫辉市| 新绛县| 井研县| 大石桥市| 花莲县| 民县| 崇仁县| 安多县| 焦作市| 水富县| 灌南县| 普兰县| 乌恰县| 车致| 汤阴县| 吴堡县| 桑日县|