Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

constructing XML properties for host object

0 views
Skip to first unread message

and...@tchijov.com

unread,
Dec 30, 2004, 12:52:31 PM12/30/04
to
I am trying to create host object which will "expose" XML object as one
of its properties. My starting point is Node object (DOM). Using
xmlbeans v2 I was able to convert it to XmlObject

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

and...@tchijov.com

unread,
Dec 30, 2004, 1:54:22 PM12/30/04
to
It looks like this code does what I need.

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

and...@tchijov.com

unread,
Dec 30, 2004, 2:46:37 PM12/30/04
to
On a similar note. To get Node object out of XML property

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

Igor Bukanov

unread,
Dec 30, 2004, 7:31:24 PM12/30/04
to and...@tchijov.com

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

Andrei Tchijov

unread,
Dec 30, 2004, 11:33:05 PM12/30/04
to
Thanks for quick replay. The fact that you did not comment about
"getting Node object out of XML property", does it mean that that code
snippet is OK?

Igor Bukanov

unread,
Dec 31, 2004, 9:12:46 AM12/31/04
to Andrei Tchijov
Andrei Tchijov wrote:
> The fact that you did not comment about
> "getting Node object out of XML property", does it mean that that code
> snippet is OK?

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

0 new messages