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

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

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

    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)  編輯  收藏


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


    網站導航:
     

    導航

    留言簿

    文章分類

    最新評論

    主站蜘蛛池模板: 久久精品国产亚洲AV麻豆网站| 日韩一级视频免费观看| 中文亚洲成a人片在线观看| 看Aⅴ免费毛片手机播放| 免费高清av一区二区三区| 中文字幕无码亚洲欧洲日韩| 亚洲欧洲免费无码| 亚洲人成在线播放| 亚洲高清中文字幕免费| 亚洲欧洲日本在线观看| 在线免费观看一级毛片| 久久亚洲精品成人无码| 亚洲?v女人的天堂在线观看| 在线播放国产不卡免费视频| 亚洲人AV永久一区二区三区久久| 一级特黄录像免费播放中文版| 亚洲欧洲久久av| 光棍天堂免费手机观看在线观看| 亚洲国产女人aaa毛片在线| 国产免费拔擦拔擦8X高清在线人| 久久丫精品国产亚洲av| www.黄色免费网站| 亚洲日韩在线中文字幕综合| 亚洲精品久久久www | 日本亚洲国产一区二区三区| 免费观看一区二区三区| 亚洲精品国产啊女成拍色拍| 在线观看人成网站深夜免费| 三级片免费观看久久| 亚洲动漫精品无码av天堂| 91精品免费在线观看| 国产成人精品日本亚洲语音| 亚洲中文字幕在线第六区| 免费精品一区二区三区第35 | 亚洲福利视频一区二区| 中文字幕免费不卡二区| 亚洲第一成年网站大全亚洲| 国产嫩草影院精品免费网址| 成人影片一区免费观看| 亚洲人成777在线播放| 亚洲av日韩av欧v在线天堂|