Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Write formatted XML to a file

Thread view: 
pavel.orehov@gmail.com - 17 Jul 2007 08:52 GMT
Hi,

I create the XML with DOM and save it to a file. But when I open such
file with Notepad, for instance, I see the XML file in one line - it
is not formatted/aligned. If I open it with IExplorer it looks fine.

I am looking for the way to save XML to a file so that XML would be
formatted.

The save code:
-------------------
doc.getDocumentElement().normalize();
DOMSource ds = new DOMSource(doc);
StreamResult sr = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(ds, sr);

Thanks,
Pavel
evgchech - 17 Jul 2007 10:07 GMT
On Jul 17, 9:52 am, "pavel.ore...@gmail.com" <pavel.ore...@gmail.com>
wrote:
> Hi,
>
[quoted text clipped - 16 lines]
> Thanks,
> Pavel

   public static String formatIdent(Document xmlNode) throws
IOException {
         StringWriter strWriter = null;
         XMLSerializer probeMsgSerializer = null;
         OutputFormat outFormat = null;
         String identString = null;
         try {
           probeMsgSerializer = new XMLSerializer();
           strWriter = new StringWriter();
           outFormat = new OutputFormat();

           // Setup format settings
           outFormat.setEncoding("UTF-8");
           outFormat.setVersion("1.0");
           outFormat.setIndenting(true);
           // Define a Writer
           probeMsgSerializer.setOutputCharStream(strWriter);
           // Apply the format settings
           probeMsgSerializer.setOutputFormat(outFormat);
           // Serialize XML Document
           probeMsgSerializer.serialize(xmlNode);

           identString = strWriter.toString();
           if(identString.indexOf("\n") != -1){
             identString =
                 identString.substring(
                     identString.indexOf("\n") + 1,
identString.length());
           }
           strWriter.close();

         }
         catch (IOException ioEx) {
           throw new IOException(
               "Failed to format xml document." + ioEx.getMessage());
         }
         return identString;
       }
RedGrittyBrick - 17 Jul 2007 21:28 GMT
> Hi,
>
[quoted text clipped - 4 lines]
> I am looking for the way to save XML to a file so that XML would be
> formatted.

You are missing three vital steps ...

> The save code:
> -------------------
> doc.getDocumentElement().normalize();
> DOMSource ds = new DOMSource(doc);
> StreamResult sr = new StreamResult(out);

StreamResult sr = new StreamResult(
  new OutputStreamWriter(out, "utf-8"));

> TransformerFactory tf = TransformerFactory.newInstance();

tf.setAttribute("indent-number", 4);

> Transformer trans = tf.newTransformer();

trans.setOutputProperty(OutputKeys.INDENT,"yes");

> trans.transform(ds, sr);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.