DOM L3 can't pretty print?

Hi all,
i'm working with xerces-j 2 and want to output a xml-file to disk using the LSSerializer.
I want the xml file to be pretty printed. From xerces-c i know, there's a feature called 'pretty-print' which can be set.
But if i try this in java, the following exception always shows up:
org.w3c.dom.DOMException: FEATURE_NOT_SUPPORTED: The parameter format-pretty-print is recognized but the requested value cannot be set.

The line in which the exception occurrs is:
domWriter.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);

I don't understand why it's goin' wrong.
Maybe you can help.
Thanks in advantage for any suggestions.

Greetings, -ulf-
ulf_kAsked:
Who is Participating?
 
CEHJCommented:
Have you tried XMLSerializer?
0
 
girionisCommented:
This feature might not be implemented in the java implementation. What do the docs say?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
objectsCommented:
use the xerces dom3 implementation:
http://xml.apache.org/xerces2-j/dom3.html
0
 
CEHJCommented:
You can pretty-print with the XMLSerializer AFAIK
0
 
girionisCommented:
Look at the DOMConfiguration documentation, there is no such parameter:

http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/DOMConfiguration.html
0
 
girionisCommented:
Try to set the canonical-form to false and see if this helps:

domWriter.getDomConfig().setParameter("canonical-form", Boolean.FALSE);

According to the docs by setting this parameter to false automatically sets the pretty-print to true, so it might work the other way round.
0
 
girionisCommented:
> According to the docs by setting this parameter to false automatically sets the pretty-print to true, so it might work the other way round.

Should be

According to the docs by setting this parameter to true automatically sets the pretty-print to false, so it might work the other way round.
0
 
ulf_kAuthor Commented:
@girionis
I use the xerces implementation, not the one from java 1.5. even if it maybe the same.
But the docs tell me, that this param exists. I can set it to FALSE, but not to TRUE.
And the exception message also tells me that this feature is recognized but can't be set.
But why can't is be set?!

Greetings, -ulf-
0
 
objectsCommented:
are you sure you are using the level 3 implementation, by default it is not used.
0
 
girionisCommented:
Do you set any other parameters before that one? There might be a conflict.
0
 
objectsCommented:
If you haven't already you'll need to grab the source and build the jars-dom3 target.
0
 
ulf_kAuthor Commented:
Well, thats the code i used:

System.setProperty(DOMImplementationRegistry.PROPERTY,"org.apache.xerces.dom.DOMXSImplementationSourceImpl");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS impl = (DOMImplementationLS)registry.getDOMImplementation("LS");
LSSerializer domWriter = impl.createLSSerializer();
domWriter.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
LSOutput dOut = new DOMOutputImpl(); FileOutputStream fos = new FileOutputStream(fileName); dOut.setByteStream(fos);
domWriter.write(((Node)getRoot()).getDomNode(),dOut);

You can see, I've set no other parameters.
But in the meanwhile i played around with XMLSerializer and it works the way i wanted it and even the code to write is much shorter.
I'm thinking about to give CEJH the credits, coz he suggested it.

The code to use the XMLSerializer is:
OutputFormat outputFormat = new OutputFormat("XML","UTF-8",true);
outputFormat.setIndent(2);
FileWriter fileWriter = new FileWriter(fileName);
XMLSerializer xmlSerializer = new XMLSerializer(fileWriter, outputFormat);
 xmlSerializer.asDOMSerializer();
xmlSerializer.serialize((((Node)getRoot()).getDomNode()).getOwnerDocument().getDocumentElement());

So, if you agree, i'll give CEJH the cedits.

Thanks for all your answers!!
Greetings, -ulf-
0
 
girionisCommented:
If XMLSerializer is the way to go then you should award the points to CEHJ.
0
 
CEHJCommented:
>>So, if you agree, i'll give CEJH the cedits.

I'm in agreement ;-)

As far as the DOM properties are concerned, why not just iterate them to see what's available?
0
 
ulf_kAuthor Commented:
@CEJH
 The property is available, but can't be set to TRUE, thats what the exception reads.

@girionis
The XMLSerializer was a nice hint to solve my problem. But thanks very much for comments!!

Greetings, -ulf-
0
 
CEHJCommented:
>>but can't be set to TRUE, thats what the exception reads.

Sorry yes - maybe it's read-only?
0
 
objectsCommented:
> impl = (DOMImplementationLS)registry.getDOMImplementation("LS");

your using the wrong implementation
0
 
ulf_kAuthor Commented:
@objects
Isn't LS the DOM3 implementation just for Read/Write access?
I didn't find anything else to put in there.

Greetings, -ulf-
0
 
objectsCommented:
Its supports pretty print
0
All Courses

From novice to tech pro — start learning today.