[betwixt] saving/reading more than one bean in the same xml file

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[betwixt] saving/reading more than one bean in the same xml file

Thomas Kraft
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

I stuck trying to read more then one bean out of one single file using
betwixt, i think that this have to be really simple but i still can't
get it working, maybe you can help figuring out my mistake:

I have a xml file with my bean stored in which looks like that:

<?xml version='1.0' ?>
  <configuration>
    <description>TestDescription</description>
  </configuration>

Out of this file i can read my bean without problems:

beanReader.registerBeanClass("configuration", TestBean.class);
TestBean testConfig = (TestBean) beanReader.parse(fileContent);

But now i want to store additional beans into the same xml file, and get
them out of this file also.

My first try was something like that:

<?xml version='1.0' ?>
  <configuration>
    <description>TestDescription</description>
  </configuration>
  <additional>
    <description>TestDescription</description>
  </additional>

and then read the data out:

beanReader.registerBeanClass("additional", AdditionalBean.class);
AdditionalBean additionalConfig = (AdditionalBeanBean)
beanReader.parse(fileContent);

But then the xml parser throws a exception because the xml is not
well-formed.

I can't find an example to do that in the correct way, but i think that
hasnt to be really complex, i just can't figure out the correct technique.

Can somebody give me a hint or can tell me how to get this working?

Any help would be highly appreciated,

Thanks in advance,

Thomas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFChIlDnPtETagUFaYRAnSvAJ4swOiQsU7uMI8+JUMb7JoxaEI1NQCfRb4e
TQfFgLuSBoux8FoqXSLTRW4=
=kTLQ
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [betwixt] saving/reading more than one bean in the same xml file

Thomas Dudziak
On 5/13/05, Thomas Kraft <[hidden email]> wrote:
 
> <?xml version='1.0' ?>
>   <configuration>
>     <description>TestDescription</description>
>   </configuration>
>   <additional>
>     <description>TestDescription</description>
>   </additional>

This is not a betwixt problem, but rather this XML is not valid at
all. In XML files you can only have a single top-level element (the
root), but in your example you got two top-level elements. The easiest
solution would probably to define a root element which you can specify
for betwixt too, though you need a .betwixt file (or a multi-mapping
if you're using 0.61-dev from SVN) for this.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [betwixt] saving/reading more than one bean in the same xml file

Thomas Kraft
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Thomas,

thanks for the quick reply

> This is not a betwixt problem, but rather this XML is not valid at
> all.

Yes, i know that this can't be valid.

> In XML files you can only have a single top-level element (the
> root), but in your example you got two top-level elements. The easiest
> solution would probably to define a root element which you can specify
> for betwixt too, though you need a .betwixt file (or a multi-mapping
> if you're using 0.61-dev from SVN) for this.

Ok, then i missing the .betwixt file. I tried to add a

<rootConfig>
[...]
</rootConfig>

around my two bean contents, but then betwixt throws follwing exception:

java.lang.IllegalArgumentException: Cannot get class for null object.
      at
org.apache.commons.betwixt.strategy.ClassNormalizer.getNormalizedClass(ClassNormalizer.java:56)

That was the reason i write to that list, because i stuck there and
didnt know how to get further.

But it seems that i have to re-read the dokumentaion and search for a
apropriate syntax for my .betwixt file.

Maybe you can give me a quick hint or something like that, i am a
betwixt-noob and i have difficulties in understanding that .betwixt file
thing

Regards,

Thomas

>
> Tom
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


- --
/**
 * Thomas Kraft
 * Synyx oHG
 * Augustastr. 13
 * 76137 Karlsruhe
 * phone  +49(0)721 66 48 79 33
 * fax    +49(0)721 66 48 877
 * eMail  [hidden email]
 * www    http://www.synyx.de
 * irc    irc.synyx.de
 */
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFChI8LnPtETagUFaYRAgI7AJoC4fSZ6tex6DARAXmbtO1ZFmBhsACgj0KW
+S1St9d/o62JgROSVnykmpg=
=Epcq
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [betwixt] saving/reading more than one bean in the same xml file

Thomas Dudziak
> Maybe you can give me a quick hint or something like that, i am a
> betwixt-noob and i have difficulties in understanding that .betwixt file
> thing

I'd suggest you upgrade to the SVN Head (0.61-dev) because there you
get the Multimapping stuff which makes defining the mapping far easier
(in short you can define all your mappings in one XML file which you
can manually add). Info about accessing Jakarta SVN is here:
http://jakarta.apache.org/site/cvsindex.html (scroll down a bit)

Then you should check the TestMultimapping test case that comes with
betwixt (view in SVN here:
http://svn.apache.org/viewcvs.cgi/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestMultiMapping.java?view=markup)
This should help get you started.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [betwixt] saving/reading more than one bean in the same xml file

Thomas Kraft
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> Then you should check the TestMultimapping test case that comes with
> betwixt (view in SVN here:
> http://svn.apache.org/viewcvs.cgi/jakarta/commons/proper/betwixt/trunk/src/test/org/apache/commons/betwixt/TestMultiMapping.java?view=markup)
> This should help get you started.

I tried the checkout and built the latest svn-version. I also tried to
look into the mentioned test-case, but that did not really helped me.

In the test case there is one bean holding another bean of different
type, which is written to and read from the xml file. That already
worked for me.

What i try to do is, i have several beans of the same type (e.g.
TestBean) and i would like to store all of them in the same xml file.
This will not work because of not-wellformed xml:

<?xml version='1.0' ?>
  <testBean1>
    <description>TestDescription</description>
  </testBean1>
  <testBean2>
    <description>TestDescription</description>
  </testBean2>
  <testBean3>
    <description>TestDescription</description>
  </testBean3>

What i want to do is having several beans of the same (or different)
type in one xml file and tell betwixt with a construct like:
beanReader.registerBeanClass("testBean" + i, AdditionalBean.class);
that it should take the named bean out of that file.

Isn't there any type of "dummy-container" where i can put all my bean
data into and getting it out by telling betwixt the name i like to have?
(simply to get wellformed xml file)

e.g.:
<?xml version='1.0' ?>
<dummyContainer>
  <testBean1>
[...]
  </testBean3>
</dummyContainer>

That would be very easy and i wonder if this issue is so hard for me to
solve :(

Still trying to get a solution,

Thomas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFChMhNnPtETagUFaYRAvmbAJ0Rq8/gXlr3bskq+b85lrEErNy3jQCcCfLu
47+yTPMihwFPsrfk8NWFejU=
=gavm
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [betwixt] saving/reading more than one bean in the same xml file

Thomas Dudziak
> I tried the checkout and built the latest svn-version. I also tried to
> look into the mentioned test-case, but that did not really helped me.
>
> In the test case there is one bean holding another bean of different
> type, which is written to and read from the xml file. That already
> worked for me.
>
> What i try to do is, i have several beans of the same type (e.g.
> TestBean) and i would like to store all of them in the same xml file.
> This will not work because of not-wellformed xml:
>
> <?xml version='1.0' ?>
>   <testBean1>
>     <description>TestDescription</description>
>   </testBean1>
>   <testBean2>
>     <description>TestDescription</description>
>   </testBean2>
>   <testBean3>
>     <description>TestDescription</description>
>   </testBean3>
>
> What i want to do is having several beans of the same (or different)
> type in one xml file and tell betwixt with a construct like:
> beanReader.registerBeanClass("testBean" + i, AdditionalBean.class);
> that it should take the named bean out of that file.
>
> Isn't there any type of "dummy-container" where i can put all my bean
> data into and getting it out by telling betwixt the name i like to have?
> (simply to get wellformed xml file)
>
> e.g.:
> <?xml version='1.0' ?>
> <dummyContainer>
>   <testBean1>
> [...]
>   </testBean3>
> </dummyContainer>
>
> That would be very easy and i wonder if this issue is so hard for me to
> solve :(
>
> Still trying to get a solution,

Might be easiest if you use a simple container class that holds the instances.
However I wonder how you differenciate the TestBean instances ? If
they are contained in a collection, then I think you should rather use
Digester directly, this gives you more freedom though also requires a
bit more work.
If there is a fixed number of TestBean instances to persiste, you
should define a simple container that has the corresponding number of
properties. You could even use a DynaBean for the purpose of the
container if you are unwilling to introduce a class solely for the
purpose of XML reading/writing of your TestBeans.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]