aspose file tools
The moose likes XML and Related Technologies and the fly likes getting Document fragment as String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "getting Document fragment as String" Watch "getting Document fragment as String" New topic
Author

getting Document fragment as String

manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308
hi,

i want to get a string from a fragment of Document. something like:

public String getStringFromDocumentElement(Element element);

param element is a node somewhere in a Document.
i thought i could import given element to another document and write it to a StringWriter. or just by passing it as a Node directly to the following:



but it does not work. to me it is an obvious functionality to get a document fragment as a String. but i haven't found anything on the net so far.

thanks.


aldana software engineering blog & .more
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16487
    
    2

Yes, that is exactly how you do it. Could you give some information beyond "does not work"?
manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308
hi,

yes you're right it works this way. the error i had, was that a whitespace sneeked in and therefore not an element was printed but a text element.

i did check this mistake, because i had a different signature as in my first post and therefore did not get a ClassCastException (passing in a text node instead of element).

//NOT receiving ClassCastException passing in text node
public String getStringFromElement(Node node){...}


//receiving ClassCastException passing in text node
public String getStringFromElement(Element element){...}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting Document fragment as String
 
Similar Threads
XML doument to a string
Problems converting a node to string
Exception while parsing XML document
Document object into Database
Getting length(no of bytes) of Dom document which is in memory