my code stock.com

Snippet options

Download: Download snippet as xmlcreator.java.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

package com.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.widget.TextView;

import com.sk8battle.R;

public class XMLcreator extends Activity 
{
	private static final String TAG = "XML";

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		Log.d(TAG,"onCreate");
		setContentView(R.layout.main);
		super.onCreate(savedInstanceState);
        //create a new file called "new.xml" in the SD card
        File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/new2.xml");
        
        try
        {
                newxmlfile.createNewFile();
        }
        catch(Exception e)
        {
                Log.e(TAG, "exception in createNewFile() method");
                Log.e(TAG, e.getMessage());
        }
        //we have to bind the new file with a FileOutputStream
        FileOutputStream fileos = null;        
        try
        {
                fileos = new FileOutputStream(newxmlfile);
        }
        catch(Exception e)
        {
                Log.e(TAG, "can't create FileOutputStream");
                Log.e(TAG, e.getMessage());
        }
        //we create a XmlSerializer in order to write xml data
        XmlSerializer serializer = Xml.newSerializer();
        try {
                //we set the FileOutputStream as output for the serializer, using UTF-8 encoding
                        serializer.setOutput(fileos, "UTF-8");
                        //Write <?xml declaration with encoding (if encoding not null) and standalone flag (if standalone not null)
                        serializer.startDocument(null, Boolean.valueOf(true));
                        //set indentation option
                        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
                        //start a tag called "root"
                        serializer.startTag(null, "geo");
                        //i indent code just to have a view similar to xml-tree
                                                               
                                serializer.startTag(null, "punto");
                                //set an attribute called "attribute" with a "value" for <child2>
                                serializer.attribute(null, "idpunto", "1");
                                serializer.endTag(null, "punto");
                       
                              
                               
                        serializer.endTag(null, "geo");
                        serializer.endDocument();
                        //write xml data into the FileOutputStream
                        serializer.flush();
                        //finally we close the file stream
                        fileos.close();
                       Log.d(TAG, "BIEN");
                
                } 
        catch (Exception e) 
        {
                        Log.e(TAG,"error occurred while creating xml file");           
                        Log.e(TAG, e.getMessage()); 
        }
    
	
	}//onCreate	

}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.