XML Schema

View: New views
3 Messages — Rating Filter:   Alert me  

XML Schema

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i would like to know if i could create a xml schema which a node contain both attribute and value? below is what i seen from w3cschool example, but after i create the schema and using xjc to convert to java object, there is no method which i could use to set the value. please advise, thanks!

<lastname lang="EN">Smith</lastname>

laurence
[Message sent by forum member 'pillboy' (llcy101@...)]

http://forums.java.net/jive/thread.jspa?messageID=370295

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: XML Schema

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

  Compare below XSD with yours which should solve the problem I think

Siva


   I generated java classes using xjc and got the value field. Your XSD should look like this .


<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:simpleType name="ST_lastname">
                <xs:restriction base="xs:string">
                        <xs:enumeration value="Smith"/>
                </xs:restriction>
        </xs:simpleType>
        <xs:element name="names">
                <xs:complexType>
                        <xs:sequence>
                                <xs:element ref="lastname"/>
                        </xs:sequence>
                </xs:complexType>
        </xs:element>
        <xs:element name="lastname">
                <xs:complexType>
                        <xs:simpleContent>
                                <xs:extension base="ST_lastname">
                                        <xs:attribute name="lang" use="required">
                                                <xs:simpleType>
                                                        <xs:restriction base="xs:string">
                                                                <xs:enumeration value="EN"/>
                                                        </xs:restriction>
                                                </xs:simpleType>
                                        </xs:attribute>
                                </xs:extension>
                        </xs:simpleContent>
                </xs:complexType>
        </xs:element>
</xs:schema>


and running xjc the lastname java class should be the following (along with other classes)

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "value"
})
@XmlRootElement(name = "lastname")
public class Lastname {

    @XmlValue
    protected STLastname value;
    @XmlAttribute(required = true)
    protected String lang;

    /**
     * Gets the value of the value property.
     *
     * @return
     *     possible object is
     *     {@link STLastname }
     *    
     */
    public STLastname getValue() {
        return value;
    }

    /**
     * Sets the value of the value property.
     *
     * @param value
     *     allowed object is
     *     {@link STLastname }
     *    
     */
    public void setValue(STLastname value) {
        this.value = value;
    }

    /**
     * Gets the value of the lang property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *    
     */
    public String getLang() {
        return lang;
    }

    /**
     * Sets the value of the lang property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     *    
     */
    public void setLang(String value) {
        this.lang = value;
    }

}

and the test program i did to print xml

public class NamesTest {

        /**
         * @param args
         */
        public static void main(String[] args) {
               
                Names names = new ObjectFactory().createNames();
                Lastname lName = new ObjectFactory().createLastname();
                lName.setLang("EN");
                lName.setValue(STLastname.SMITH);
                names.setLastname(lName);
                JAXB.marshal(names, System.out);

        }

}

OUTPUT:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<names>
    <lastname lang="EN">Smith</lastname>
</names>
[Message sent by forum member 'shammu' (samuthira@...)]

http://forums.java.net/jive/thread.jspa?messageID=370495

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: XML Schema

by metro-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thx shammu, i had modified my source and it works now. :)
[Message sent by forum member 'pillboy' (llcy101@...)]

http://forums.java.net/jive/thread.jspa?messageID=370579

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...