Node node = ...;
XmlObject xmlObject = XmlObject.Factory.parse( node );
It is my understanding that I need eventually convert XmlObject into
org.mozilla.javascript.xml.XMLObject. Is it true? And what is the
best/correct way of doing it?
Andrei
Node node = ...;
XmlObject xmlObject = XmlObject.Factory.parse( node );
Object[] args = new Object[1];
args[0] = xmlObject;
result = ScriptRuntime.newObject( globalScope.get( "XML", globalScope
), cx, this, args );
Is it the "right way"?
Andrei
ScriptableObject xml = (ScriptableObject)this.get( "xml", this );
Function getXmlObject = (Function)xml.getPrototype().get(
"getXmlObject", xml );
Object xmlObject = getXmlObject.call( cx, this, xml, new Object[0]);
org.w3c.dom.Node node =
((XmlObject)((NativeJavaObject)xmlObject).unwrap()).getDomNode();
Is it right?
Andrei
No: you should not use ScriptRuntime since its is NOT a part of public
API and its methods are public purely for technical reasons.
Use the following code instead:
Node node = ...;
XmlObject xmlObject = XmlObject.Factory.parse( node );
Object[] args = new Object[1];
args[0] = Context.javaToJS(xmlObject, globalScope);
result = cx.newObject(globalScope, "XML", args);
Note that it important to call Context.javaToJS to convert xmlObject to
the proper boxed object that XML constructor can handle properly.
Regards, Igor
No, it does not mean that. It just means that just did not comment on
that ;)!
To get XmlObject from E4X JS object "x" use:
Wrapper wrap = (Wrapper)ScriptableObject.callMethod(x, "getXmlObject",
new Object[0]);
XmlObject xmlObject = (XmlObject)wrap.unwrap();
which uses the fact that E4X objects in Rhino provides for scripts
"getXmlObject()" method. so in JS the above fragment reduces to :
Then you can call any method on the resulting xmlObject that XMLBeans
provides.
Note also that this machinery is intended to be used from scripts since
in JS the above code reduces to:
var x = <xml>TEST</xml>;
...
var xmlObject = x.getXmlObject();
And do not use NativeJavaObject in application code: it is not a part of
public API either, use Wrapper interface instead.
Regards, Igor