Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DOM L3 can't pretty print?

Posted on 2004-08-11
20
Medium Priority
?
1,991 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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 …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

636 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