XML editing w/o loosing formating

Hi all,I am looking for a way of editing an XML file that allows me to change it preserving its formatting, like new lines, spaces, tab,.... Any ideas?Thanks for any help,Alex
[203 byte] By [Alexandrinoa] at [2007-11-27 6:35:43]
# 1
By default JAXP 1.3 preserves the XML format. http://java.sun.com/j2se/1.5.0/docs/guide/xml/jaxp/JAXP-Compatibility_150.html#preserving
dvohra09a at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

This is a sample XML that I am using for testing.

<?xml version="1.0" encoding="UTF-8"?>

<sample attr1="v1"

attr2="v2"

attr3="v3">

<content anotherAttr="" />

</sample>

This is the output after a simple read and write

<?xml version="1.0" encoding="UTF-8"?><sample attr1="v1" attr2="v2" attr3="v3">

<content anotherAttr=""/>

</sample>

This is my test code:

public static void main(String[] args) throws Exception {

File fileInput = new File("input.xml");

File fileOutput = new File("output.xml");

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setExpandEntityReferences(false);

DocumentBuilder builder = factory.newDocumentBuilder();

document = builder.parse(fileInput);

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(document);

FileOutputStream fos = new FileOutputStream(fileOutput);

StreamResult result = new StreamResult(fos);

transformer.transform(source, result);

}

Am I doing something wrong?

Thanks,

Alex

Alexandrinoa at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Sorry... I forgot the code tags...

This is a sample XML that I am using for testing.

<?xml version="1.0" encoding="UTF-8"?>

<sample attr1="v1"

attr2="v2"

attr3="v3">

<content anotherAttr="" />

</sample>

This is the output after a simple read and write

<?xml version="1.0" encoding="UTF-8"?><sample attr1="v1" attr2="v2" attr3="v3">

<content anotherAttr=""/>

</sample>

This is my test code:

public static void main(String[] args) throws Exception {

File fileInput = new File("input.xml");

File fileOutput = new File("output.xml");

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setExpandEntityReferences(false);

DocumentBuilder builder = factory.newDocumentBuilder();

document = builder.parse(fileInput);

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(document);

FileOutputStream fos = new FileOutputStream(fileOutput);

StreamResult result = new StreamResult(fos);

transformer.transform(source, result);

}

Alexandrinoa at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Are the newline characters added in an XML editor?<?xml version="1.0" encoding="UTF-8"?><sample attr1="v1"attr2="v2"attr3="v3"><content anotherAttr="" /></sample>
dvohra09a at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
In my test case, yes, but it shouldn't matter.
Alexandrinoa at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Add indentaion.transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "true");transformer.setOutputProperty("{ http://xml.apache.org/xslt}indent-amount", "2");
dvohra09a at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
Add indentation.
dvohra09a at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
Thanks, but didn't work. Besides, I am trying to preserve the original format, whatever it is, so can't be anything specific to this case.
Alexandrinoa at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

Use JDK 1.4.x for indentation to get added. Setting the indentation adds the newline between the xml declaration and he root element tag. Whitespace between elements is considered to be a text node and is preserved. Whitespace/newline between attributes is not a text node and is not preserved.

The following input.xml would produce the same output as an input.xml without whitespaces and newlines.

<?xml version="1.0" encoding="UTF-8"?>

<sample attr1="v1"

attr2="v2"

attr3="v3">

<content anotherAttr="" />

</sample>

dvohra09a at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10
What is the JDK version?Setting the indentation adds the newline between the xml declaration and the root element tag.
dvohra09a at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 11
Yes, but it still putting all the attributes on a single line. I am using JDK 1.5.
Alexandrinoa at 2007-7-12 18:02:56 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...