Formatting XML with LF - URGENT

Discussions

XML & Web services: Formatting XML with LF - URGENT

  1. Formatting XML with LF - URGENT (4 messages)

    Hello Everybody,

    I am generating a XML using Java Parsers(JAXP,DOM). The XML itself is well-formed and

    1. When viewed thru the IE it comes all with line-breaks, indendation and looks like a good formatted XML (hope u understand what i mean)

    But When I open it with notepad, wordpad or anything else, the XML is displayed in one single very long line.

    Please let me know how to produce the XML with line breaks, indendation. Basically format it in similar way as it appears in IE. I used XMLSPY and there is an option to do this, but i want to do it programatically. The XML gets transferred from Windows to Unix and longer lines causes problems in transfer.

    Also cannot use JDOM because the formatter needs org.jdom.Document whereas my entire tier is structured around org.w3c.dom.Document.

    Thanks for your efforts in advance.
  2. Hello Everybody,

    >
    > I am generating a XML using Java Parsers(JAXP,DOM). The XML itself is well-formed and
    >
    > 1. When viewed thru the IE it comes all with line-breaks, indendation and looks like a good formatted XML (hope u understand what i mean)
    >
    > But When I open it with notepad, wordpad or anything else, the XML is displayed in one single very long line.
    >
    > Please let me know how to produce the XML with line breaks, indendation. Basically format it in similar way as it appears in IE. I used XMLSPY and there is an option to do this, but i want to do it programatically. The XML gets transferred from Windows to Unix and longer lines causes problems in transfer.
    >
    > Also cannot use JDOM because the formatter needs org.jdom.Document whereas my entire tier is structured around org.w3c.dom.Document.
    >
    > Thanks for your efforts in advance.

    You can use the following example code (and Xerces for XML parsing):

            OutputFormat format = new OutputFormat(newCatalog, "UTF-8", true);
            format.setLineWidth(0);
            XMLSerializer serializer = new XMLSerializer(new FileWriter(outputDirectory + "/BooksCAT/catalog.xml"), format);
            serializer.serialize(newCatalog.getDocumentElement());

    Hope this helps
    Kalin
  3. Hi kalin,

    thanks for the reply. I did try the Serializer. But it did not work. It can still serialize to no. of destiantions/formats but cannot format the XML.
  4. In order to format the output you need to create OutputFormat object and set the last parameter to true in the corresponding constructor (as shown above).
    Setting this parameter to true will format the XML in the usual way as you are used to see it in IE for example. If you use Xerces and use the example code I think you'll get what you want (I haven't tried it with different XML parsers)

    Kalin
  5. Formatting XML with LF - URGENT[ Go to top ]

    If it is so god-damned URGENT, figure it out on your own and don't waste your time at this forum.