Solved

DOM L3 can't pretty print?

Posted on 2004-08-11
20
1,834 Views
Last Modified: 2011-04-14
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-
0
Comment
Question by:ulf_k
  • 6
  • 5
  • 5
  • +1
20 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11770732
Have you tried XMLSerializer?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 11770735
0
 
LVL 35

Expert Comment

by:girionis
ID: 11770750
This feature might not be implemented in the java implementation. What do the docs say?
0
 
LVL 92

Expert Comment

by:objects
ID: 11770772
use the xerces dom3 implementation:
http://xml.apache.org/xerces2-j/dom3.html
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11770783
You can pretty-print with the XMLSerializer AFAIK
0
 
LVL 35

Expert Comment

by:girionis
ID: 11770788
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
 
LVL 35

Expert Comment

by:girionis
ID: 11770827
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
 
LVL 35

Expert Comment

by:girionis
ID: 11770831
> 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
 

Author Comment

by:ulf_k
ID: 11770844
@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
 
LVL 92

Expert Comment

by:objects
ID: 11770871
are you sure you are using the level 3 implementation, by default it is not used.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 35

Expert Comment

by:girionis
ID: 11770880
Do you set any other parameters before that one? There might be a conflict.
0
 
LVL 92

Expert Comment

by:objects
ID: 11770907
If you haven't already you'll need to grab the source and build the jars-dom3 target.
0
 

Author Comment

by:ulf_k
ID: 11770980
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
 
LVL 35

Expert Comment

by:girionis
ID: 11770996
If XMLSerializer is the way to go then you should award the points to CEHJ.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11771000
>>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
 

Author Comment

by:ulf_k
ID: 11771121
@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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11771171
>>but can't be set to TRUE, thats what the exception reads.

Sorry yes - maybe it's read-only?
0
 
LVL 92

Expert Comment

by:objects
ID: 11771247
> impl = (DOMImplementationLS)registry.getDOMImplementation("LS");

your using the wrong implementation
0
 

Author Comment

by:ulf_k
ID: 11771547
@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
 
LVL 92

Expert Comment

by:objects
ID: 11779047
Its supports pretty print
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
IT Company 5 69
computer science syllabus 3 52
what is a "java.lang.System Property"   ? 20 35
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now