Lauren,
Have you tried describing your XML in XML Schema?
It has a facility for this type of thing in the form of the <xsd:any>
element.
If you take an XML Schema and run it through the Castor Source Generator
it will produce the classes and the descriptiors (can be thought of as
compiled time mappings) for you.
Given your example, if for example the element content of <auto> is
variable then an XML Schema as follows will generate a class that uses
the Castor AnyNode class to describe your variable content.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="auto">
<xs:complexType>
<xs:sequence>
<xs:any/>
</xs:sequence>
<xs:attribute name="color" type="xs:string"/>
<xs:attribute name="make" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:schema>
If you then determine after an initial unmarshalling to further
unmarshall the variable content into a more specific class.
You can then pass the AnyNode content to another Castor Unmarshaller to
obtain the object you really want.
Cheers,
Andy.
-----Original Message-----
From: Lauren Bish [mailto:[EMAIL PROTECTED]
Sent: 07 December 2004 23:05
To: [EMAIL PROTECTED]
Subject: [castor-user] [XML] Somewhat arbitrary XML and Field Handlers??
We are using Castor-XML for marshalling/unmarshalling SOAP messages, and
we are using mapping files. Almost all of our SOAP messages have a known
structure - so so far so good.
However, some of our messages contain an XML structure which is somewhat
arbitrary. We have been mapping this to a HashMap, but there are two
problems:
1) It seems too complex - especially for me, as I have trouble getting
my head wrapped around this structure in order to move the data in and
out of the HashMap from a GUI (which is built on the fly to edit the
data).
2) The XML may contain multiple instances of an element within a given
node - for example:
<auto color="red" make="toyota" >
<notes>Notes 1: test, test, is this mike working?</notes>
<notes>Notes 2</notes>
<notes>Notes 3</notes>
<notes>Notes 4</notes>
<notes>Notes 5</notes>
<notes>Notes 6</notes>
</auto>
It may also contain no or empty instances of that particular element.
I do know ahead of time something of the structure to expect, because it
is defined in a class which has a list of what these elements may be
(for example, boolean, string, text (a long string), a class (containing
a structure of other elements), a date, etc.), but I do not know this
structure until runtime, and it can change during runtime - it can even
change between the time I unmarshall it and marshall it back out (the
user may add new elements, or remove elements).
A given element may have a minimum occurance of zero or more, a maximum
occurance of 1 or more (possibly unlimited), and may or may not be
required. A given element may or may not be represented as an an
attribute. These things I will also know at runtime, and these can also
change at runtime - even between unmarshalling and marshalling.
The problem is that although we can almost map this to a HashMap,
because it requires unique keys, and we are keying on the element name,
we can't have duplicate elements - which the structure says we have to
support. This is how I discovered we had a problem.
I am thinking that I want to just unmarshall this structure into a
simple List. I already have the Element structure (sans values) in a
list - so I am thinking I would like to extend the classes in that list
to not only have the strucutral and type info, but also contain the
value, then when I unmarshall the XML I can almost directly use the
List.
My question/problem:
I am thinking I would generate a mapping file on the fly, when I know
the general structure of this particular snippet of XML. That should not
be hard at all.
I am thinking that I also need to use a FieldHandler of some sort - but
I am not sure and I haven't been able to find enough
documentation/examples/hand-holding of FieldHandlers to make me feel
comfortable going down the road just yet.
Does this seem a reasonable way to attack this problem?
Are there some detailed/hand-holding examples somewhere?
I have read everything I could find on the Castor website and linked
articles therein.
Other suggestions? Tips? Traps? Tricks?
TIA
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-user
The information in this message is confidential and may be legally privileged.
It may not be disclosed to, or used by, anyone other than the addressee. If you
receive this message in error, please advise us immediately. Internet emails
are not necessarily secure. CODA does not accept responsibility for changes to
any email which occur after the email has been sent. Attachments to this email
may contain software viruses, which could damage your systems. CODA has checked
the attachments for viruses before sending, but you should virus-check them
before opening.
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-user