[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

DOM L3 can't pretty print?

Posted on 2004-08-11
20
Medium Priority
?
2,016 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 400 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

872 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