empty XML Document - Android XML

Android examples for XML:XML Document

Description

empty XML Document

Demo Code


//package com.java2s;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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.NodeList;

public class Main {
    public static void main(String[] argv) throws Exception {
        String path = "java2s.com";
        emptyXMLDocument(path);//from  ww w .j  a  v  a  2  s .  co  m
    }

    public static void emptyXMLDocument(String path) {
        emptyXMLDocument(new File(path));
    }

    public static void emptyXMLDocument(File file) {
        if (file.exists()) {
            Document document = loadXMLDocument(file);
            if (document != null) {
                Element root = getRoot(document);
                NodeList nodeList = root.getChildNodes();
                for (int i = 0; i < nodeList.getLength(); i++) {
                    root.removeChild(nodeList.item(i));
                }
                saveXMLDocument(document, file);
            }
        }
    }

    public static Document loadXMLDocument(String path) {
        return loadXMLDocument(new File(path));
    }

    public static Document loadXMLDocument(File file) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(file);
            return document;
        } catch (Exception ex) {
            return null;
        }
    }

    public static Element getRoot(String path) {
        return getRoot(new File(path));
    }

    public static Element getRoot(File file) {
        return getRoot(loadXMLDocument(file));
    }

    public static Element getRoot(Document document) {
        return getRoot(document, "root");
    }

    public static Element getRoot(Document document, String root) {
        if (document == null) {
            throw new IllegalArgumentException("Null document");
        }

        NodeList nodeList = document.getElementsByTagName(root);
        return nodeList.item(0) != null ? (Element) nodeList.item(0) : null;
    }

    public static void saveXMLDocument(Document document, String path) {
        saveXMLDocument(document, new File(path));
    }

    public static void saveXMLDocument(Document document, File file) {
        FileOutputStream output = null;
        try {
            output = new FileOutputStream(file);
            Source source = new DOMSource(document);
            Result result = new StreamResult(output);
            Transformer transformer = TransformerFactory.newInstance()
                    .newTransformer();
            transformer.transform(source, result);
        } catch (Exception ex) {
            try {
                output.close();
            } catch (IOException io) {
            }
        } finally {
            try {
                output.close();
            } catch (IOException io) {
            }
        }
    }
}

Related Tutorials