Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

XML pretty-printing in Java

Suppose there is a String with XML file content in it
String xmlStr= "<error><error-id>1</error-id><error-name>ERR-ONE</error-one></error>";

When the xmlStr is printed using Java streams, it prints in a single line sans formatting and indentation.

To get it ‘pretty-printed’, javax XML API library can be used.

Source xmlInput = new StreamSource(new StringReader(xml)); 
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
//transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
xml=xmlOutput.getWriter().toString();
System.out.println("THE RESULT XML "+xml);





20 comments:

  1. :) would be better if the example shows the result as well. :)

    ReplyDelete
  2. Thanks for this XML tutorial. I'm afraid I'm better acquainted with offset printing than XML printing!

    ReplyDelete
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

      Delete
  3. The CR/LFs, if any, need to be removed first, else it won't work!

    ReplyDelete
  4. I agree with JawaharI that it would be better if you can show us visual aid of the result of these codes. I will try this pretty-printing later.

    long island document scanning

    ReplyDelete
  5. due to the fact FileMaker pro considers the selected printer and print or web page setup data when it calculates margins and other measurements on the layout, you must affirm those settings before you create a Labels layout. Etiketten Druck

    ReplyDelete
  6. Marketing and advertising are essential for business success. In one way or another, the product or service needs to be promoted to the customer. Marketers use different techniques and media to convey their publicity message to the end user. Banners are one such way in which the marketing message is sent out to a wide audience. Banner printing near me for cheap

    ReplyDelete
  7. :Offset printing is a more seasoned process which prints utilizing metal plates to exchange the picture on to the paper.VSL

    ReplyDelete
  8. This procedure is practical when utilized for littler amounts (roughly 10-500 pcs).Office Printing is a moderately new strategy for printing. NYC Printing Company - VSL

    ReplyDelete
  9. Its a little bit complicated advice for me but I promise I will try to do it with all my strength. But I suppose that firstly I need to know everything about how to open xml file https://wikiext.com/xml and here I can found out all useful information for sure.

    ReplyDelete

  10. I found this is an informative and interesting post so i think so it is very useful and knowledgeable. I would like to thank you for the efforts you have made in writing this article.
    Luxury printing

    ReplyDelete
  11. There are various cost effective means of getting various products and services in front of the targeted audience and the roller banners are one of them. With the modern technology making forays into the advertising world more and more businesses are relying on the digital medium for getting their products to the target audience. Roll up Stands

    ReplyDelete
  12. Look books are a great way to display your unique collection of fashionable jewelry, footwear, eyewear etc. Therefore, if you want high-quality look books printed at affordable rates then get in touch with professional and reputed look book printing companies. They can provide you with a wide variety of designs and patterns to help you meet your look books publishing related requirements. VHS to DVD Conversion Services

    ReplyDelete
  13. f you are using an inkjet printer make sure you get paper suited for an inkjet printer and likewise if you have a laser printer. t shirt

    ReplyDelete
  14. They’re also cast as heels, allegedly crafted due to the McMahon family’s belief that the far right cost Linda the election. A tag feud between these two seems a natural fit, with Darren Young cast as the good guy. prototyp

    ReplyDelete
  15. I might want to state that this blog truly persuaded me to do it! Much appreciated, great post.
    Printing Dublin

    ReplyDelete
  16. Spot up for this write-up, I actually think this website wants much more consideration. I’ll probably be once more to read far more, thank you for that information. Imprentas en Cancun

    ReplyDelete
  17. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. (eddm®) every door direct mail prints

    ReplyDelete
  18. Another issue with iron-on was that the method was time consuming. On the contrary, printing was swift and efficient and also produced comparatively higher quality prints. cheap 3d printer

    ReplyDelete
  19. Once you've written a polished resume that's sure to impress potential employers, you should focus on your cover letter Letter Printing

    ReplyDelete

Search 24 Bytes