This is getting pretty convoluted, and I'm not sure the current code can handle it cleanly - I think you're actually going to get two object creations for the "player" object, which is why the name information is lost when you reference it again for the "id". It *may* work if you merge together the two layers of <structure> definitions on the first reference, though:
<structure field="pitcher">
<structure name="current-pitcher" usage="optional"
value-style="attribute" field="player">
<value name="first-name" field="firstName"/>
<value name="last-name" field="lastName"/>
</structure>
<structure name="current-pitcher" usage="optional"
value-style="attribute">
<structure field="player"> <value name="id" field="id"/>
</structure>
<value name="wins" field="wins"/>
<value name="losses" field="losses"/>
</structure>
</structure>
No guarantees, but it might be worth a try. Another alternative is to use a set-method on the "pitcher" object for the "id" value.
I'd appreciate it if you'd enter the existing binding and XML as a bug report, though, so that I can track it for beta 4. Thanks - and thanks too for your remarks about JiBX! I'm always glad to hear that it's making things easier for developers (and almost as glad to hear when it's not, just so I can think about how to improve it).
- Dennis
Gardner, Aaron wrote:
Is it possible with JiBX Beta 3a 1.0 to map data inside the attributes of 2 XML elements into 2 Java objects when the element boundaries and object boundaries don't "line up" exactly?
(I read all the docs, looked through the sample code, and searched through these mailing list archives and didn't find this topic discussed anywhere.)
Specifically:
I have some 3rd party XML (which they can't change without adding a transformation stage, which I'm trying to avoid) that contains element pairs like this:
<current-pitcher first-name="Kazuhisa" last-name="Ishii"/> <current-pitcher id="6852" wins="2" losses="0"/>
I first tried to map said XML into a mixin-style class hierarchy (as opposed to inheritance) of 2 classes, Pitcher and Player, with fields:
Pitcher.Player.firstName Pitcher.Player.lastName Pitcher.Player.id
Pitcher.wins Pitcher.losses
For some unknown reason, the XML provider put the 2 name attributes in the
first XML element and continued the logical entity into the 2nd XML elm by
putting the player id attribute there along with the pitcher-specific wins
and losses attributes. Seems convoluted to me but that's what I have to work
with.
I tried a binding.xml like so (excerpt):
<structure field="pitcher">
<structure name="current-pitcher" usage="optional"
value-style="attribute">
<structure field="player"> <!-- firstName and lastName don't get set,
maybe because player defined again below
-->
<value name="first-name" field="firstName"/>
<value name="last-name" field="lastName"/>
</structure>
</structure>
<structure name="current-pitcher" usage="optional"
value-style="attribute">
<structure field="player"> <value name="id" field="id"/>
</structure>
<value name="wins" field="wins"/>
<value name="losses" field="losses"/>
</structure>
</structure>
This almost worked, except that the second field="player" block (with id field) was used while the first field="player" block (with name fields) was ignored. I'm guessing it's not correct to refer to the same object field twice in the binding definition (the binding compiler doesn't complain about it), but I don't see a way to define the mapping I want without doing so. Would getter/setter methods help here in any way?
I successfully wrote a custom unmarshaller class that maps date-time attributes spread out over 2 XML elements into a single java.util.Date object, so I was thinking I could attempt a custom unmarshaller to tackle this problem, but I would like to avoid custom code if at all possible.
For a quick work-around, I adjusted my domain object model so that one Java class now contains these attributes that are spread out over multiple XML elements. I don't know if this is the object model I want in the long run, but it works for now.
Assuming I really do want to map attributes spread out over multiple elements into multiple objects, what's the best/correct way to handle this type of mapping situation?
Thanks,
Aaron Gardner Comcast Online
P.S. Since this is my first post, I must say that I love JiBX's Java-centric approach to XML binding and its flexibility. So far it's handled (almost) everything I've thrown at it, not to mention the docs are very clear and thorough. As someone who works with a lot of messy real-world XML, this is the only data binding library that I've been able to use to get real work done (I've tried a bunch of those other Schema-centric ones; they never cut it).
------------------------------------------------------- This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click _______________________________________________ jibx-users mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jibx-users
