?
Solved

How to get  an org.w3c.dom.Document from an org.apache.crimson.tree.XmlDocument ??

Posted on 2003-02-27
24
Medium Priority
?
745 Views
Last Modified: 2007-12-19
Hi,

I've got a problem to transform a JDOM to DOM.
I only get an org.apache.crimson.tree.XmlDocument...

My code :
==========================================================
org.jdom.Document jdom is a JDOM document
org.w3c.dom.Document dom = null;

DOMOutputter domOut = new org.jdom.output.DOMOutputter(
          "org.jdom.adapters.CrimsonDOMAdapter");
dom = domOut.output(jdom);
==========================================================

and dom is an org.apache.crimson.tree.XmlDocument.
And I need an org.w3c.dom.Document...
How to resolve my problem ?

Thanx in advance.
0
Comment
Question by:nico51
[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
  • 10
  • 10
  • 2
  • +2
24 Comments
 

Author Comment

by:nico51
ID: 8034742
My problem is to use it with SOAP.
And SOAP only accept org.w3c.dom.Document witch is serializable...
0
 
LVL 6

Expert Comment

by:mightyone
ID: 8037070
not sure about the crimson jdom, but as they all inherit from xmldocument,
try a cast
org.w3c.dom.Document dom = (...Document)jdom;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8038072
This will do the conversion:

try {
     org.jdom.output.DOMOutputter outputter = new org.jdom.output.DOMOutputter();
     org.w3c.dom.Document dom = outputter.output(jdom);
     // dom now holds your w3c document
}
catch(Exception e) {
     e.printStackTrace();
}    
 
0
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.

 

Author Comment

by:nico51
ID: 8040365
mightyone=>I tried to cast but I always obtain an org.apache.crimson.tree.XmlDocument.

CEHJ=>I tried your code but it don't change the problem. I always obtain an org.apache.crimson.tree.XmlDocument.

Thanx for your help.
I think I will try with an inputStream.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8040806
Could you tell me what the output from this is?:

try {
    org.jdom.output.DOMOutputter outputter = new org.jdom.output.DOMOutputter();
    System.out.println("Argument to outputter is of type " + jdom.getClass().getName());
    org.w3c.dom.Document dom = outputter.output(jdom);
    // dom now holds your w3c document
    System.out.println("Result from outputter is of type " + dom.getClass().getName());
}
catch(Exception e) {
    e.printStackTrace();
}    
0
 

Author Comment

by:nico51
ID: 8042107
CEHJ =>

Argument to outputter is of type org.jdom.Document
Result from outputter is of type org.apache.xerces.dom.DocumentImpl
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8042168
And can you tell me what happens when you insert this into the appropriate place?

System.out.println("Result from outputter is of type " + dom.getClass().getName());
System.out.println("And is this an org.w3c.dom.Document? - answer - " + (dom instanceof org.w3c.dom.Document));
0
 

Author Comment

by:nico51
ID: 8042184
CEHJ =>

Argument to outputter is of type org.jdom.Document
Result from outputter is of type org.apache.xerces.dom.DocumentImpl
0
 

Author Comment

by:nico51
ID: 8042206
sorry...I try...
0
 

Author Comment

by:nico51
ID: 8042228
Here is what's happening :


Result from outputter is of type org.jdom.Document
And is this an org.w3c.dom.Document? - answer - false
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8042330
That's pretty odd! That's not what the docs say about the outputter AFAIK. I'll look into it further.
0
 
LVL 6

Expert Comment

by:mightyone
ID: 8042705
try
DOMOutputter converter = new DOMOutputter();
org.w3c.dom.Document domDocument = converter.output(jdomDocument);


 explanation see below:


copied from javaandxml by harold:

"
Serializing JDOM Objects
All the major JDOM classes such as Element, Document, Namespace implement the java.io.Serializable interface. This means JDOM objects can be passed between machines using remote method invocation (RMI) and stored in files using ObjectOutputStream and ObjectInputStream.

Of course just because something is possible doesn't mean it's a good idea. XML is itself a serialization format for JDOM objects, and it's a much more broadly supported one. What's not as broadly known is that pure textual XML is also generally much faster and smaller than Java's binary object serialization. There's very little reason to use serialized JDOM objects instead of passing genuine XML documents back and forth.

At the time of this writing, the long-term plan for JDOM serialization is still under discussion. While you can use object serialization to pass JDOM objects from one virtual machine to another, both of which have the same version of JDOM, using anything beyond that is up in the air. In particular, it is highly doubtful that an object you serialize today will be able to be deserialized tomorrow in a different version of JDOM. For long-term persistence, you should absolutely use XML documents instead of serialized objects. " and

"Talking to DOM Programs
JDOM is not an acronym. It does not stand for Java Document Object Model. JDOM is not directly compatible with DOM (which is an acronym). That is to say, a JDOM Element is not a DOM Element. The JDOM Element class does not implement the DOM Element interface. JDOMs Element class has methods that the DOM Element interface does not have and vice versa. You cannot pass a JDOM Element to a method that expects a DOM Element or a DOM Element to a method that expects a JDOM Element. The same is true for the JDOM Document class and the DOM Document interface, the JDOM Attribute class and the DOM Attr interface, the JDOM ProcessingInstruction class and the DOM ProcessingInstruction interface, and so forth.

That being said, JDOM does allow you to convert JDOM documents to and from DOM documents. I dont recommend this for new projects (which should pick one API or the other and stick with it); but it is useful for integrating new JDOM programs with legacy DOM code and vice versa.

If you already have a DOM Document object, then the org.jdom.input.DOMBuilder class can use it to generate the JDOM equivalent. The syntax is straightforward. Use the no-args DOMBuilder() to create a DOMBuilder object and then pass the DOM Document object to its build() method. For example, assuming that the variable domDocument points to an object of type org.w3c.dom.Document, this code fragment build an org.jdom.Document object from it:

DOMBuilder builder = new DOMBuilder();
org.jdom.Document jdomDocument = builder.build(domDocument);
// work with the JDOM document
The original DOM object is not changed in any way. Furthermore, changes to the JDOM Document do not affect the DOM Document it was built from. In the other direction, future changes to the DOM Document do not affect the JDOM Document.

Moving in the other direction, from JDOM to DOM, the org.jdom.output.DOMOutputter class produces DOM Document objects from JDOM Document objects. Since this isnt really serialization, but rather than just converting from one model to another, there arent nearly as many options to set as with XMLOutputter. For instance, you cant add extra white space or select the encoding. (You could always do that later with a DOM serializer of some kind, if necessary.) For example,

DOMOutputter converter = new DOMOutputter();
org.w3c.dom.Document domDocument = converter.output(jdomDocument);
// work with the DOM document
Once again, these documents are not connected after the initial creation of one from the other. Changes to one are not reflected in the other. "


this helps!



0
 

Author Comment

by:nico51
ID: 8042859
I'm ok with you
that's the problem ;o)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8042930
What i don't understand here, nico51 is that:

>>org.w3c.dom.Document dom = outputter.output(jdom);


does not throw an exception

and yet

System.out.println("And is this an org.w3c.dom.Document? - answer - " + (dom instanceof org.w3c.dom.Document));

prints false. Do you have any ideas?

0
 

Author Comment

by:nico51
ID: 8043097
sorry
but I have no idea.

I will look at the mightyone comment's.


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8043163
mightyone's point is that it's probably more performant and more portable to write the xml directly to the stream rather than use a serialized format, with which i agree. His code, however, is identical to mine:

>>
DOMOutputter converter = new DOMOutputter();
org.w3c.dom.Document domDocument = converter.output(jdomDocument);
>>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8043238
>>Do you have any ideas?

Of course, dom being null in the below would account for the situation. Is that the case?

>>org.w3c.dom.Document dom = outputter.output(jdom);
0
 

Author Comment

by:nico51
ID: 8057772
The problem is that SOAP doesn't accept non serializable doc.
I will try with File.
thanx for your help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057878
It's time to sort this out - i see you have other questions open around the same subject. Now JDOM is one of the libraries you are using. Can you tell me what version and also the name and version numbers of any other libraries?
0
 

Author Comment

by:nico51
ID: 8063050
I'm sorry but I discover it recently.
I use JDOM b8.

I think I found a solution :

// I create a DOM
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document document = builder.newDocument();  
         
// I get bytes from JDOM
XMLOutputter xmlOutputter = new XMLOutputter();
String xmlString = xmlOutputter.outputString(jdom);
byte[] bytes = xmlString.getBytes();          
         
// I put bytes in my DOM
document.setNodeValue(new String(bytes));
       
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 8063440
OK. Bear in mind that this will probably turn whatever the original encoding was into platform default:

>>
byte[] bytes = xmlString.getBytes();          
         
// I put bytes in my DOM
document.setNodeValue(new String(bytes));
>>
0
 

Expert Comment

by:CleanupPing
ID: 9059033
nico51:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9671529
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept CEHJ's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9721006
:-)
0

Featured Post

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.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month8 days, 6 hours left to enroll

765 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