• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create XML file programmatically?

 
Prajkti Khadse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to create an XML file to Store my Application Settings into.
( I can't use SharedPrefs because i want that Settings file later to be accessed by Some other Code.)

I can easily create an XML with java's code and store it in File too.

but in Android
I can create xml with the same java code but can't save it into the file
coz they have removed the package javax.xml.transform from SDK.

Can anyone please help me out...
I am Attaching the Java code here...


 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recall that XML is just formatted text - You could easily build it in a StringBuilder and store as a String object or do println to a Stream.

That would also be faster and create fewer objects.

Bill
 
Prajkti Khadse
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks William,
But using StringBuilder means i have to append each and every thing
i.e<, /, >, =, and all that.
And that i find somewhat weird...

Don't we have Something like TransformerFactory in android?

With the Document object, can't i create the XML file?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh come on, its not that big a deal to handle the tags - you can define static Strings for all the common ones.

If the number of changable data items in the XML is small you can even create a static String[] that serves as a template for the final message. I have done this with "@1" etc values in the template string showing where real variables get substituted - small and fast and in the spirit of Android as far as I'm concerned.

Bill
 
emna bb
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every body,
Here a link to create an xML file in android mobile
http://www.anddev.org/write_a_simple_xml_file_in_the_sd_card_using_xmlserializer-t8350.html

he describes procedure like this
- create a File object
- link file object to a FileOutputStream
- create a XmlSerializer object and link to the FileOutputStream
- use XmlSerializer methods to create xml tree
- use XmlSerializer flush() method to write data into the new file
and he offers the code source to do it
enjoy
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!