1

I'm trying to print an XML file colored on console using java, but I have never done this before, and I have no idea how to do that.

This code prints an XML file on console.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder1;
            
builder1 = factory.newDocumentBuilder();

Document document;
document = builder1.parse(new File(pathFilename));


TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setAttribute("indent-number", new Integer(2));
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

DOMSource source = new DOMSource(document);


StreamResult result2 = new StreamResult(System.out);
transformer.transform(source, result2);

I would like to print it on console like an editor, for example Notepad++. Any suggests how to do it?

An example of output on console:

<ControllerMode dataItemId="mode" sequence="286201" timestamp="2019-06-27T11:23:02.641182Z">AUTOMATIC</ControllerMode>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Scripta14
  • 463
  • 2
  • 8
  • 22
  • You can create an HTML page based on an XML file using XSLT (eXtensible Stylesheet Language Transformations) – Paolo Mossini Jul 03 '19 at 08:51
  • Sorry, maybe I've not espressed in a clear way what I would do with my program. Have a look above, I put a simple example of output on console. – Scripta14 Jul 03 '19 at 09:14
  • What do you mean with _"like an editor"_? – Paolo Mossini Jul 03 '19 at 09:17
  • Setting by code java a different Syntax Coloring. To print on black prompt windows console an output of my file with a different syntax coloring. – Scripta14 Jul 03 '19 at 09:31
  • Does this answer your question? [How to print color in console using System.out.println?](https://stackoverflow.com/questions/5762491/how-to-print-color-in-console-using-system-out-println) – Siberio Oct 02 '20 at 13:44

0 Answers0