KTOV
KTOV

Reputation: 704

Creating XML File in android

So I call this method within my MainActivity..

public void CreateXMLFile() throws ParserConfigurationException, TransformerException {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("user");
    doc.appendChild(rootElement);

    Element name = doc.createElement("name");
    rootElement.appendChild(name);

    Element displayInstructions = doc.createElement("displayInstructions");
    rootElement.appendChild(displayInstructions);

    Element scores = doc.createElement("scores");
    rootElement.appendChild(scores);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(context.getFilesDir(), "info.xml"));
    transformer.transform(source, result);
}

The problem is.. i dont actually know if it's being created. Is there any way to check?

Upvotes: 1

Views: 357

Answers (1)

rakesh kashyap
rakesh kashyap

Reputation: 1466

if you want to check if the file is created or not, you can do something like below

File file = new File(this.getFilesDir(), "info.xml");
if (file.exists()) {
    //Do something
    Log.d("","file is available");
} else {
    // Do something else.
    Log.d("","file is not available");
}

Above code will check if the file exists or not.

Please note that this is based on my understanding on the limited information you have given on your problem.

Upvotes: 1

Related Questions