I have another question which I think might be also related to format
control.

Is there a way to parse XML from file and reformat the output with
XMLSerializer?
Serializer ignores all of my attempts to set indent and prints everything
in one line.

This is the original XML:
<root>
     <element1>test1</element1>
     <element2>test2</element2>
</root>

This is what I get:
<?xml version="1.0" encoding="UTF-8"?>
<root>  <element1>test1</element1>  <element2>test2</element2> </root>

I think the problem is the #TEXT nodes that contain original document
formating in the DOM structure (including new line characters and spaces).

Should I overwrite parser to get rid of the original formating (new line
characters) or there is a way to tell serializer to ignore it?

Here is an example of code that I'm using:
     DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance
();
     DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
     Document doc = docBuilder.parse(new InputSource("test.xml"));

     OutputFormat format = new OutputFormat(); //Serialize DOM
     format.setPreserveSpace(false);
     format.setMethod("xml");
     format.setIndenting(true);
     format.setIndent(4);

     StringWriter stringOut = new StringWriter(); //Writer will be a String
     XMLSerializer serial = new XMLSerializer(stringOut, format);
     serial.serialize(doc.getDocumentElement());

     System.out.println(stringOut.toString());

Regards,
Vlad



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to