How to create XML file programmatically?

Put your problem here if it does not fit any of the other categories.

How to create XML file programmatically?

Postby prajkti.khadse » Sat Aug 08, 2009 9:21 am

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...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class CreatXMLFile {
  3.  
  4. public static void main(String[] args) throws Exception {
  5.  
  6. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  7.  
  8. DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
  9.  
  10. Document document = documentBuilder.newDocument();
  11.  
  12. Element rootElement = document.createElement("map");
  13.  
  14. document.appendChild(rootElement);
  15.  
  16.  
  17.  
  18. Element em = document.createElement("string");
  19.  
  20. em.setAttribute("name", "FirstName");
  21.  
  22. em.appendChild(document.createTextNode("Rita"));
  23.  
  24. rootElement.appendChild(em);
  25.  
  26.  
  27.  
  28. em = document.createElement("string");
  29.  
  30. em.setAttribute("name", "LastName");
  31.  
  32. em.appendChild(document.createTextNode("Roy"));
  33.  
  34. rootElement.appendChild(em);
  35.  
  36.  
  37.  
  38. TransformerFactory transformerFactory = TransformerFactory.newInstance(); // This code
  39.  
  40. Transformer transformer = transformerFactory.newTransformer();    // doesn't work
  41.  
  42. DOMSource source = new DOMSource(document);                         // with Android
  43.  
  44. StreamResult result = new StreamResult("c:\\abc.xml");                //  <img src="http://www.anddev.org/images/smilies/sad.png" alt=":(" title="Sad" />
  45.  
  46. transformer.transform(source, result);
  47.  
  48. }
  49.  
  50. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
prajkti.khadse
Developer
Developer
 
Posts: 32
Joined: Thu Mar 19, 2009 1:01 pm
Location: Bangalore

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google Feedfetcher, MSNbot Media and 20 guests