Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Creating a Document from a Node

Reply
Thread Tools

Creating a Document from a Node

 
 
pwaring@gmail.com
Guest
Posts: n/a
 
      07-28-2008
I'm trying to use JTidy (latest release version, I can't use the svn
version) to pretty print a Node (org.w3c.dom.Node). However, the only
method for printing nodes in org.w3c.tidy.Tidy is:

public void pprint(org.w3c.dom.Document doc, java.io.OutputStream out)

So I need to create a Document containing only one node so that I can
pass it as an argument to this method Does anyone know how I can do
this? I've tried a simple cast (which doesn't work), and creating an
empty document then adding a node to it with the following code:

try
{
content.appendChild(currentDiv);
}
catch ( DOMException dome )
{
System.out.println("Error: " + dome.getMessage());
}

However, this throws an exception with the message: "Error: newChild
cannot be a child of this node". Does anyone know how/if I can get
round this? It seems like an awful lot of hassle just to print a Node.

Thanks in advance,

Paul
 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      07-28-2008
On Mon, 28 Jul 2008, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> I'm trying to use JTidy (latest release version, I can't use the svn
> version) to pretty print a Node (org.w3c.dom.Node). However, the only
> method for printing nodes in org.w3c.tidy.Tidy is:
>
> public void pprint(org.w3c.dom.Document doc, java.io.OutputStream out)
>
> So I need to create a Document containing only one node so that I can
> pass it as an argument to this method Does anyone know how I can do
> this? I've tried a simple cast (which doesn't work),


What did the actual class of the Node turn out to be?

> and creating an empty document then adding a node to it with the
> following code:
>
> try
> {
> content.appendChild(currentDiv);
> }
> catch ( DOMException dome )
> {
> System.out.println("Error: " + dome.getMessage());
> }
>
> However, this throws an exception with the message: "Error: newChild
> cannot be a child of this node". Does anyone know how/if I can get round
> this?


Try Document.adoptNode or importNode, according to whether you want to
remove the Node from the original Document or not.

> It seems like an awful lot of hassle just to print a Node.


Welcome to the wonderful world of the W3C DOM!

tom

--
an optical recording release. copyright digitally mastered. .,
 
Reply With Quote
 
 
 
 
pwaring@gmail.com
Guest
Posts: n/a
 
      07-28-2008
On Jul 28, 2:01 pm, Tom Anderson <(E-Mail Removed)> wrote:
> What did the actual class of the Node turn out to be?


Just a Node (i.e. not a subclass) as far as I could tell.

> > However, this throws an exception with the message: "Error: newChild
> > cannot be a child of this node". Does anyone know how/if I can get round
> > this?

>
> Try Document.adoptNode or importNode, according to whether you want to
> remove the Node from the original Document or not.


I've tried that, with the following code:

Document content = createEmptyDocument();
Node currentDiv = divs.item(i); // divs is just all the <div> nodes
from a document
content.adoptNode(currentDiv);

which results in the following error (at runtime - no problems during
compilation):

java.lang.AbstractMethodError:
org.w3c.tidy.DOMDocumentImpl.adoptNode(Lorg/w3c/dom/NodeLorg/w3c/dom/
Node;

Paul
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      07-28-2008
On Mon, 28 Jul 2008, (E-Mail Removed) wrote:

> On Jul 28, 2:01 pm, Tom Anderson <(E-Mail Removed)> wrote:
>> What did the actual class of the Node turn out to be?

>
> Just a Node (i.e. not a subclass) as far as I could tell.


Node is an interface. This probably isn't important, but if you wanted to
see what the implementing class was, you could do
someNode.getClass().getName().

>>> However, this throws an exception with the message: "Error: newChild
>>> cannot be a child of this node". Does anyone know how/if I can get
>>> round this?

>>
>> Try Document.adoptNode or importNode, according to whether you want to
>> remove the Node from the original Document or not.

>
> I've tried that, with the following code:
>
> Document content = createEmptyDocument();
> Node currentDiv = divs.item(i); // divs is just all the <div> nodes from a document
> content.adoptNode(currentDiv);
>
> which results in the following error (at runtime - no problems during
> compilation):
>
> java.lang.AbstractMethodError:
> org.w3c.tidy.DOMDocumentImpl.adoptNode(Lorg/w3c/dom/NodeLorg/w3c/dom/Node;


Well that's not good.

AbstractMethodError usually means there's a library screwup: your code is
fine, but somewhere, there's a class A which thinks class B has an
implementation of some method, while class B declares that method as
abstract. Typically, A and B are in separate JARs, and A was compiled
against a previous version of B, which has since changed to turn a method
from concrete to abstract.

I'd look at Tidy, your XML parser (if any) and any other DOM-related
libraries, and see if (a) the one you think is being loaded really is (by
looking round your classpath etc), (b) they're the versions you think they
are and (c) whether they could or should be more up to date.

tom

--
an optical recording release. copyright digitally mastered. .,
 
Reply With Quote
 
pwaring@gmail.com
Guest
Posts: n/a
 
      07-29-2008
On Jul 28, 5:17 pm, Tom Anderson <(E-Mail Removed)> wrote:
> Node is an interface. This probably isn't important, but if you wanted to
> see what the implementing class was, you could do
> someNode.getClass().getName().


It turned out to be org.w3c.tidy.DOMElemImpl, though I don't think
that had anything to do with the problem.

> I'd look at Tidy, your XML parser (if any) and any other DOM-related
> libraries, and see if (a) the one you think is being loaded really is (by
> looking round your classpath etc), (b) they're the versions you think they
> are and (c) whether they could or should be more up to date.


I've no idea what's on the classpath when the code executes (it's
a .war file being deployed by Tomcat in the end). In the end I got so
fed up of trying to serialise the Node that I just wrote my own method
to do exactly what I want. Not terribly reusable or clean, but I need
something which works now and I don't have time to debug library
conflicts.

Thanks for the help/pointers,

Paul
 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      07-29-2008
On Tue, 29 Jul 2008, (E-Mail Removed) wrote:

> On Jul 28, 5:17 pm, Tom Anderson <(E-Mail Removed)> wrote:
>
>> I'd look at Tidy, your XML parser (if any) and any other DOM-related
>> libraries, and see if (a) the one you think is being loaded really is (by
>> looking round your classpath etc), (b) they're the versions you think they
>> are and (c) whether they could or should be more up to date.

>
> I've no idea what's on the classpath when the code executes (it's a .war
> file being deployed by Tomcat in the end). In the end I got so fed up of
> trying to serialise the Node that I just wrote my own method to do
> exactly what I want. Not terribly reusable or clean, but I need
> something which works now and I don't have time to debug library
> conflicts.


So often the way!

In fact, that's exactly what i'm doing now. I need the Node.getTextContent
method from DOM Level 3, which my current implementations don't support.
So, i've written a version myself, which uses the lower-level DOM methods
to walk the tree and construct the string. Only it turns out that in DOM4J
1.3, which is what i have, if you call getFirstChild() on an Element which
has an attribute, rather than returning an Attribute node, as it should,
or stepping over it, it throws an exception. If i upgrade to DOM4J 1.6, it
works, but then i have to upgrade to the latest Jaxen too, and then loads
of other things break.

AAAAAAAAAAAAAAAAAAAARRRGH!

tom

--
The glass is twice as big as it needs to be.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
how to creating new node inside a xml node Geagleeye ASP General 2 08-03-2006 12:09 PM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
How to drag a sub-node to another node in TreeView? wolf ASP .Net 0 08-18-2004 11:51 PM
[HELP] Create a DOM using a node from another node yio@free.fr Java 0 07-16-2003 12:30 PM



Advertisments