Write to file or OutPutStream a DOMDocument

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

Write to file or OutPutStream a DOMDocument

Postby massimo85 » Fri Feb 29, 2008 6:40 pm

In Java if I had to write a DOMDocument to file I do this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.             TransformerFactory tf = TransformerFactory.newInstance();
  2.  
  3.             tf.setAttribute("indent-number", new Integer(4));
  4.  
  5.             Transformer t = tf.newTransformer();
  6.  
  7.             t.setOutputProperty(OutputKeys.INDENT, "yes");
  8.  
  9.             t.setOutputProperty(OutputKeys.METHOD, "xml");
  10.  
  11.             t.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
  12.  
  13.        
  14.  
  15.             t.transform(new DOMSource(yourDOMDocument), new StreamResult(new         OutputStreamWriter(yourFileOutputStream)));
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


In Android there aren't APIs for XSLT, so, how can I write a Document to file?
In my Android application I have to create an XML document, but I don't want to
create it manually with strings, I want to create it with DOMDocument interface.
The problem is that I don't know how can I write it in an OutputStream.
Thank-youvery much.
massimo85
Freshman
Freshman
 
Posts: 2
Joined: Fri Feb 29, 2008 8:35 am

Top

Postby plusminus » Sat Mar 01, 2008 2:13 pm

Hello massimo,

for opening a file and receiving the corresponding OutPutStream, have a look here:
:arrow: http://www.anddev.org/viewtopic.php?t=115

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

I answer at myself

Postby massimo85 » Sat Mar 01, 2008 2:47 pm

It's strange but in Android there aren't XML APIS for XSLT, so if we have to write a DOMDocument (XML) we have 2 choice:

  • Create file using strings concatenation (NOT THE TOP!!)
  • Use XMLPULL APIs as the following:
    Syntax: [ Download ] [ Hide ]
    Using java Syntax Highlighting
    1. XmlPullParserFactory factory = XmlPullParserFactory.newInstance(
    2. System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
    3.  
    4. org.xmlpull.v1.XmlSerializer serializer = null;
    5. serializer=factory.newSerializer();
    6. serializer.setOutput(out, "utf-8");
    7. serializer.startDocument(null, false);
    8. serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
    9. serializer.text("\n");
    10. serializer.startTag(NAMESPACE, "kml");
    11. ........
    12. .....
    13.  
    Parsed in 0.032 seconds, using GeSHi 1.0.8.4

    You can find the complete documentation at http://xmlpull.org
    It isn't a good method, XSLT APIs are better, but surely better than string concatenation!!!

Bye and thank-you!!

Massimo
massimo85
Freshman
Freshman
 
Posts: 2
Joined: Fri Feb 29, 2008 8:35 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests