aspose file tools
The moose likes XML and Related Technologies and the fly likes Problems converting a node to string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Problems converting a node to string" Watch "Problems converting a node to string" New topic

Problems converting a node to string

Miguel Capo

Joined: Oct 08, 2006
Posts: 14
I am using an java extension in a XSLT and passing a node as the parameter.
In the Java code I want to get the string representation of that node and do some processing to it and return it back to the stylesheet.

The problem that I am having is that when I use the code to transform the node to a string it is transforming the whole document instead of just the node.

I have inserted code to make sure that the node that I am passing is the one that I want and not the document one. Here is the code that I am using.

Any help will be very much appeciated


public static String ProcessText(Node the_node){
String res="";
//This code here is just to make sure that the the_node argument contains the node that I want to process

res+= "Node name:" + the_node.getNodeName();
res= "Attr name:"+ (the_node.getAttributes()).getNamedItem("name").getNodeValue();

try {

Source source = new DOMSource(the_node);
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
return res+stringWriter.getBuffer().toString();
} catch (TransformerConfigurationException e) {
} catch (TransformerException e) {
return null;

The string that I get is the Node Name the Attribute that confirms that I have the right node and then all the xml doc.
Miguel Capo

Joined: Oct 08, 2006
Posts: 14
I have change the approach and the code seems to work.

Instead of trying to use an extension to do the Java work I do all the transformation and I use a DOMResult object to store the result of the transformation.
Then I use XPath to get the node that I wanted to modify with the extension and I just modify it using the DOM. In this case the code does return the String for that node only. I am using the same XPath expression that I was using with the extension but now I get the right result.

There has to be something beyond my knowledge on how the extensions work.

Thanks anyway
It is sorta covered in the JavaRanch Style Guide.
subject: Problems converting a node to string