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


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


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    留言簿

    文章分類

    最新評論

    主站蜘蛛池模板: a级毛片视频免费观看| 久久国产美女免费观看精品| 亚洲AV成人影视在线观看| 国产精品亚洲一区二区无码| 嫩草在线视频www免费看| 成人无遮挡毛片免费看| 亚洲老妈激情一区二区三区| 亚洲AV无码一区二区三区人| 一级一黄在线观看视频免费| 免费观看无遮挡www的视频| 亚洲人成电影网站国产精品| 亚洲综合男人的天堂色婷婷| 搜日本一区二区三区免费高清视频 | 成人毛片18岁女人毛片免费看| 亚洲va精品中文字幕| 毛片在线免费视频| 久久久久久久亚洲Av无码| 免费人成大片在线观看播放电影| 国产成人免费在线| 免费在线观看亚洲| 亚洲乱码一二三四区麻豆| 国产好大好硬好爽免费不卡| 又爽又黄无遮挡高清免费视频| 污网站免费在线观看| 国产亚洲综合久久系列| 亚洲人成在线免费观看| 亚洲色在线无码国产精品不卡| 亚洲视频在线观看免费视频| 亚洲AV无码专区亚洲AV伊甸园 | 亚洲午夜精品在线| 成人永久免费福利视频网站| 亚洲国产日韩在线| 免费二级毛片免费完整视频| 中文字幕乱码免费看电影| 亚洲乱码一二三四区国产| 亚洲午夜精品一级在线播放放| 男女男精品网站免费观看| 亚洲尹人香蕉网在线视颅| 国产18禁黄网站免费观看| 真人无码作爱免费视频| 亚洲AV成人片色在线观看|