List Info

Thread: Re: instantiate given a schema type




Re: instantiate given a schema type
user name
2007-10-22 14:37:34
There are a couple of different ways you can do this within XmlObject.
I prefer the xmlcursor methods, but you can also use .newDomNode() and work with the DOM api and call append child like
parent.newDomNode()...appendChild(...)
-jacobd

On 10/22/07, Cory Virok < cvirokvmware.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">cvirokvmware.com> wrote:

That looks like it's exactly what I need… However, I need to get the new instance into a working XML document. Trying to do this via the DOM API is giving me a "Child to add is from another document" error.

 

So, now the question is, if I have 2 XmlObjects and I *know* the first one is a parent element to the second one, how can I do the equivalent of DOM's "appendChild()&quot; method but with the XmlObject interface?

 

Something like:

 

<code&gt;

 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; XmlObject parent = getParentObject();

SchemaTypeLoader loader = XmlBeans.getContextTypeLoader();

XmlObject child = loader.newInstance(getSomeSchemaType(), null);

 

 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; //Here';s what I *want* to do

 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; parent.appendChild(child);

assert(parent.validate());   ; //Should always be OK since I know getSomeSchemaType() will always return an XmlObject that is a child of parent

</code&gt;

 

Thanks,

cory

 

From: Jacob Danner [mailto: jacob.dannergmail.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">jacob.dannergmail.com]
Sent: Monday, October 22, 2007 11:41 AM
To: userxmlbeans.apache.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">userxmlbeans.apache.org
Subject: Re: instantiate given a schema type

&nbsp;

Have you looked at the SchemaTypeLoader APIs
http://xmlbeans.apache.org/docs/2.2.0/reference/org/apache/xmlbeans/SchemaTypeLoader.html#newInstance(org.apache.xmlbeans.SchemaType,%20org.apache.xmlbeans.XmlOptions)

-jacobd

On 10/22/07, Cory Virok < cvirokvmware.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">cvirokvmware.com> wrote:

Given only a SchemaType object, is there a way to instantiate a new XmlObject
that the SchemaType represents?
I have it working but my solution is clunky and uses reflection... There's
gotta be a better way!

I want to do something like this:

SchemaType someType = getFooSchemaType();
Foo foo = (Foo) newInstanceFromSchemaType(someType);

Currently, I have this:

<code&gt;
   ; &nbsp; &nbsp; //children[i] is an SchemaParticle

&nbsp; &nbsp; &nbsp; &nbsp; //objClass is now the Class obj that represents the *interface* to
the object I really want...
  ; &nbsp; &nbsp; &nbsp;Class objClass = children[i].getType().getJavaClass();
  ; &nbsp; &nbsp; &nbsp;// Make the assumption that XMLbeans will always
 &nbsp;   ; &nbsp; // create an inner &quot;Factory&quot; class!
&nbsp;   ; &nbsp; &nbsp;Class innerFactory = objClass.getClasses()[0];
&nbsp;   ; &nbsp; &nbsp;Method newInstanceMeth =
innerFactory.getDeclaredMethod("newInstance&quot;,

new Class[0]);

&nbsp; &nbsp; &nbsp; &nbsp; XmlObject childObj = (XmlObject) newInstanceMeth.invoke(null, null);
&lt;/code>;

Thanks,
cory

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribexmlbeans.apache.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">user-unsubscribexmlbeans.apache.org
For additional commands, e-mail: user-helpxmlbeans.apache.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">user-helpxmlbeans.apache.org

&nbsp;


RE: instantiate given a schema type
user name
2007-10-22 14:59:27

Unless I do a DOM node import, I get the “Child to add is from another document&#8221; error when using the DOM API. I tried using the copyXml() method with XmlCursor but it gave me an error saying that it could not copy and entire document.

 

Since we can do this in DOM and in Cursor modes̷0; is this a feature that the XmlObject API is lacking?

 

Thanks very much for your help,

cory

 

From: Jacob Danner [mailto:jacob.dannergmail.com]
Sent: Monday, October 22, 2007 12:38 PM
To: userxmlbeans.apache.org
Subject: Re: instantiate given a schema type

 

There are a couple of different ways you can do this within XmlObject.
I prefer the xmlcursor methods, but you can also use .newDomNode() and work with the DOM api and call append child like
parent.newDomNode()...appendChild(...)
-jacobd

On 10/22/07, Cory Virok <vmware.com" target="_blank">cvirokvmware.com> wrote:

That looks like it's exactly what I need…; However, I need to get the new instance into a working XML document. Trying to do this via the DOM API is giving me a "Child to add is from another document" error.

 

So, now the question is, if I have 2 XmlObjects and I *know* the first one is a parent element to the second one, how can I do the equivalent of DOM's "appendChild()&quot; method but with the XmlObject interface?

 

Something like:

 

<code&gt;

 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; XmlObject parent = getParentObject();

SchemaTypeLoader loader = XmlBeans.getContextTypeLoader();

XmlObject child = loader.newInstance(getSomeSchemaType(), null);

 

 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; //Here's what I *want* to do

 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; parent.appendChild(child);

assert(parent.validate());   ; //Should always be OK since I know getSomeSchemaType() will always return an XmlObject that is a child of parent

</code&gt;

 

Thanks,

cory

 

From: Jacob Danner [mailto: jacob.dannergmail.com" target="_blank">jacob.dannergmail.com]
Sent: Monday, October 22, 2007 11:41 AM
To: userxmlbeans.apache.org" target="_blank">userxmlbeans.apache.org
Subject: Re: instantiate given a schema type

 

Have you looked at the SchemaTypeLoader APIs
http://xmlbeans.apache.org/docs/2.2.0/reference/org/apache/xmlbeans/SchemaTypeLoader.html#newInstance(org.apache.xmlbeans.SchemaType,%20org.apache.xmlbeans.XmlOptions)

-jacobd

On 10/22/07, Cory Virok < cvirokvmware.com" target="_blank">cvirokvmware.com> wrote:

Given only a SchemaType object, is there a way to instantiate a new XmlObject
that the SchemaType represents?
I have it working but my solution is clunky and uses reflection... There's
gotta be a better way!

I want to do something like this:

SchemaType someType = getFooSchemaType();
Foo foo = (Foo) newInstanceFromSchemaType(someType);

Currently, I have this:

<code&gt;
   ; &nbsp; &nbsp; //children[i] is an SchemaParticle

&nbsp; &nbsp; &nbsp; &nbsp; //objClass is now the Class obj that represents the *interface* to
the object I really want...
  ; &nbsp; &nbsp; &nbsp;Class objClass = children[i].getType().getJavaClass();
  ; &nbsp; &nbsp; &nbsp;// Make the assumption that XMLbeans will always
 &nbsp;   ; &nbsp; // create an inner &quot;Factory&quot; class!
&nbsp;   ; &nbsp; &nbsp;Class innerFactory = objClass.getClasses()[0];
&nbsp;   ; &nbsp; &nbsp;Method newInstanceMeth =
innerFactory.getDeclaredMethod("newInstance&quot;,

new Class[0]);

&nbsp; &nbsp; &nbsp; &nbsp; XmlObject childObj = (XmlObject) newInstanceMeth.invoke(null, null);
&lt;/code>;

Thanks,
cory

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribexmlbeans.apache.org" target="_blank">user-unsubscribexmlbeans.apache.org
For additional commands, e-mail: user-helpxmlbeans.apache.org" target="_blank">user-helpxmlbeans.apache.org

 

 

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )