users@jaxb.java.net

Re: DTD generation of (#PCDATA | field)*

From: M.Ismail <mohammed.hany_at_gmail.com>
Date: Fri, 25 Jun 2010 00:52:04 -0700 (PDT)

Finally, got it to work. For anyone who may struggle with DTD to java
generation using jaxb in the future:
1- convert it to xmlshema first (if you want to customize the binding it
will be much better to use xmlschema).
2- generate the code and edit it afterward.

I used the same steps as Wolfgang Laun has suggested (thanks alot for your
help) and added the following function in ObjectFactory.java:

// the following is generated
@XmlElementDecl(namespace = "", name = "field")
    public JAXBElement<Field> createField(Field value) {
        return new JAXBElement<Field>(_Field_QNAME, Field.class, null,
value);
    }
    // This one I've added
    @XmlElementDecl(namespace = "", name = "")
    public JAXBElement<String> createField3(String value) {
        return new JAXBElement<String>(new
QName(null,""),String.class,value);
    }

so now I can generate mixed XML. Text and elements just like HL7. The
following page is also very informative:

http://wiki.glassfish.java.net/Wiki.jsp?page=JAXBMixed

Thanks


Wolfgang Laun-2 wrote:
>
> Generated from a DTD, I suppose? (Try to avoid that.)
>
> Anyway, here is the class with annotations as it should be for s.th.
> similar
> to your XML snippet (as generated from an XML schema).
>
> @XmlAccessorType(XmlAccessType.FIELD)
> @XmlType(name = "FooType", propOrder = { "content"})
> public class FooType {
> @XmlElementRef(name = "field", type = JAXBElement.class)
> @XmlMixed
> protected List<Serializable> content;
> @XmlAttribute
> protected String type;
> // getters, setters...
> }
>
> A <foo> element might look like this
>
> <foo type="example">
> text part 1
> <field>turnips</field>
> text part 2
> <field>wheat</field>
> text part 3
> </foo>
>
> Notice that the content list contains the text chunks intermingled with
> the
> <field> elements.
>
> Don't complain - mixed content must be presented in document order; that's
> why there is a single
> catch-all list for text() and child() nodes subordinate to //foo.
>
> -W
>
> On 23 June 2010 19:16, M.Ismail <mohammed.hany_at_gmail.com> wrote:
>
>>
>> No its automatically generated.
>>
>> However, I even tried to edit the generated code manually :
>>
>> @XmlAccessorType(XmlAccessType.FIELD)
>> @XmlType(name = "", propOrder = {
>> "value","field"
>> })
>> @XmlRootElement(name = "request")
>> public class Request {
>>
>> @XmlValue
>> protected String value;
>>
>> @XmlElement(required = true)
>> protected List<Field> field;
>>
>> public List<Field> getField()
>> {
>> return field;
>> }
>>
>> public void setFields(List<Field> fields)
>> {
>> this.field = fields;
>> }
>>
>> public String getvalue() {
>> return value;
>> }
>>
>>
>> public void setvalue(String value) {
>> this.value = value;
>> }
>>
>> }
>>
>>
>> But I got the following runtime exception:
>>
>> com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of
>> IllegalAnnotationExceptions
>> If a class has @XmlElement property, it cannot have @XmlValue property.
>> this problem is related to the following location:
>> at protected java.lang.String com.Ji.Request.value
>> at com.Ji.Request
>> at public com.Ji.Request
>> com.Ji.ObjectFactory.createRequest()
>> at com.Ji.ObjectFactory
>> this problem is related to the following location:
>> at protected java.util.List com.Ji.Request.field
>> at com.Ji.Request
>> at public com.Ji.Request
>> com.Ji.ObjectFactory.createRequest()
>> at com.Ji.ObjectFactory
>>
>>
>>
>> M.Ismail wrote:
>> >
>> > Sorry, I missed the closing tag. Yes you're right.
>> >
>> > Thanks
>> >
>> >
>> > Wolfgang Laun-2 wrote:
>> >>
>> >> Would you please confirm the XML?
>> >> <application>
>> >> <request>cmd -i 1 -p 1 -s -a
>> >> <field name="phone">255685082753</field>
>> >> * </request>*
>> >> *</application>*
>> >>
>> >> On 23 June 2010 14:55, M.Ismail <mohammed.hany_at_gmail.com> wrote:
>> >>
>> >>>
>> >>> Hi,
>> >>>
>> >>> I just want to try custom binding but I don't know what to search for
>> to
>> >>> do
>> >>> the following:
>> >>>
>> >>> <application>
>> >>> <request>cmd -i 1 -p 1 -s -a
>> >>> <field name="phone">255685082753</field>
>> >>> <application>
>> >>>
>> >>> notice that the request element has both a #PCDATA and a request
>> element
>> >>> value. How to define this in custom jaxb binding? I've tried
>> customizing
>> >>> the
>> >>> type using javaType to Object but it didn't work.
>> >>>
>> >>> thanks
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>> M.Ismail wrote:
>> >>> >
>> >>> > Hi,
>> >>> >
>> >>> > I have a DTD schema that is defined as:
>> >>> >
>> >>> > <?xml version="1.0" encoding="UTF-8"?>
>> >>> > <!ELEMENT application (request | response)>
>> >>> > <!ELEMENT request (#PCDATA | field)*>
>> >>> > <!ELEMENT field (#PCDATA)>
>> >>> > <!ATTLIST field
>> >>> > name CDATA #REQUIRED
>> >>> >>
>> >>> > <!ELEMENT response (number? | record* | error?)>
>> >>> > <!ELEMENT number (#PCDATA)>
>> >>> > <!ELEMENT record (field)+>
>> >>> > <!ELEMENT error (#PCDATA)>
>> >>> >
>> >>> >
>> >>> > I want to generate the following xml for example:
>> >>> > <application>
>> >>> > <request>cmd -i 1 -p 1 -s -a
>> >>> > <field name="phone">255685082753</field>
>> >>> > <field name="username">user</field>
>> >>> >
>> >>> > </request>
>> >>> > </application>
>> >>> >
>> >>> > However I'm having trouble dealing with the element: <!ELEMENT
>> request
>> >>> > (#PCDATA | field)*>
>> >>> >
>> >>> > because the signature of the request.setValue is:
>> >>> > public void setvalue(String value) {
>> >>> > this.value = value;
>> >>> > }
>> >>> >
>> >>> > How can I set its value to both a PCDATA and multiple fields?
>> >>> >
>> >>> > Thanks,
>> >>> >
>> >>>
>> >>> --
>> >>> View this message in context:
>> >>>
>> http://old.nabble.com/DTD-generation-of-%28-PCDATA-%7C-field%29*-tp28916598p28971440.html
>> >>> Sent from the java.net - jaxb users mailing list archive at
>> Nabble.com.
>> >>>
>> >>>
>> >>> ---------------------------------------------------------------------
>> >>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> >>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>> >>>
>> >>>
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://old.nabble.com/DTD-generation-of-%28-PCDATA-%7C-field%29*-tp28916598p28974555.html
>> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>
>

-- 
View this message in context: http://old.nabble.com/DTD-generation-of-%28-PCDATA-%7C-field%29*-tp28916598p28989791.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.