-1

When I'm trying to use found solutions to convert from Node to String in javascript, starting from this line

var transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer();

it says

Message: (Desired evaluation could not be performed: JavaScript Error: Internal Error: ReferenceError: "javax" is not defined. (JavaScript Error: Internal Error: ReferenceError: "javax" is not defined.)

but classes from java* package work OK

this works:

var writer = new java.io.StringWriter();

Is it possible to convert Node to String without using classes from javax* package?

using org.w3c.* is ok though

flup
  • 26,937
  • 7
  • 52
  • 74
VextoR
  • 5,087
  • 22
  • 74
  • 109
  • What are you trying to do? You should write a helper class that does the XML handling for you and bind an instance of that to your scriptengine. – mwhs Dec 22 '13 at 19:56
  • @mwhs I'm working in system which allows to write java code in javascript (new in javascript, maybe it's common to write Java inside js). Some API method from the system returns DOM Node instance and I need to convert it to String using Java language in javascript. That javascript "engine" seems like doesn't support javax package and I don't have access to link package or custom written /*jar//*class files. I need to convert that Node to String using classes from java* package – VextoR Dec 22 '13 at 20:06
  • @VextoR `myNode.outerHTML` – Benjamin Gruenbaum Dec 22 '13 at 20:06
  • I think this is a misconception. You are actually writing javascript, but some of the java classes have been bound to your javascript engine with their full name. There is no keyword `var` in java. Maybe you should tell us the name of your system. – mwhs Dec 22 '13 at 20:21
  • @mwhs probably you're right. In any case I would love to convert that Node to String using "Java" or javascript. It doesn't matter which to use. System name is "Progress Sonic ESB 7.6" – VextoR Dec 22 '13 at 20:26
  • Are you sure that you cannot extend the binding context of your javascript execution environment? – mwhs Dec 22 '13 at 20:29
  • 1
    I am pretty sure that Progress Sonic ESB internally uses Rhino as its JavaScript engine. I've updated the question's tags accordingly. – flup Dec 28 '13 at 21:23

1 Answers1

1

Sonic ESB internally uses Rhino as its JavaScript engine.

When calling Java from JavaScript, you should reference the Java classes using Packages: Packages.javax.xml.transform.TransformerFactory The reason you can use the java packages without trouble is that Packages.java has been aliased in the global variable java.

When you do this, you can use the classes in the javax package just fine. In fact, you can add any jars you like to the classpath and use those classes too.

https://developer.mozilla.org/en-US/docs/Rhino/Scripting_Java

flup
  • 26,937
  • 7
  • 52
  • 74