Re: [Simple-support] How to parse an xml element with a provided default value from xsd?
Brought to you by:
niallg
|
From: Robert M. <rob...@gm...> - 2012-01-12 21:40:01
|
SimplsXML does not read or follow your XML schema when parsing your XML ust
youe attributes. If it did then it would be getting closer in functionality
to JAXB and this is supposed to ba a simple library. However this is not a
problem as you should see no loss of functionality without using a schema.
To do what you want is simple however. From memory just add a default
clause to the value attribute annotation. Like this:
@Attribute(name = "value", required = false, default = "CCC")
Robert
(Sent from my ASUS Transformer tablet)
On Jan 13, 2012 8:29 AM, "Alexander Poulikakos" <
ale...@er...> wrote:
> Hi
>
> I have an xml-schema (my.xsd) and an xml document (my.xml), as shown
> below. I have made two classes (Element.java and Elements.java also shown
> below) to parse the xml-document.
>
> When doing the following, everything works as expected:
> ==========================================
> *public* *static* *void* main(String[] args) *throws* Throwable {
> Serializer serializer = *new* Persister();
> Elements elements = serializer.read(Elements.*class*, *new*File(
> "my.xml"));
> Element element = elements.getElementByName("propB");
> System.*out*.println(element.getValue());
> }
> ==========================================
>
> And “BBB” is printed on the console. However, if I remove the following
> from my.xml file “value=*"BBB"*” and run the same code I would expect the
> default value (as defined in my.xsd) to be returned. But instead I get
> “null”. What am I doing wrong? How do I return the default value (“*CCC*”
> in this case)?
>
> I’m quite new to XML, so it is possible that I have missed the concept of
> xml-schemas…
>
> Regards,
> Alex
>
> my.xml
> ==========================================
> <?xml version=*"1.0"* encoding=*"UTF-8"*?>
> <elements
> xmlns:xsi=*"http://www.w3.org/2001/XMLSchema-instance"*
> xsi:noNamespaceSchemaLocation=*"my.xsd"*>
>
> <element name=*"propA"* value=*"AAA"* />
> <element name=*"propB"* value=*"BBB"* />
> </elements>
> ==========================================
>
> my.xsd
> ==========================================
> <?xml version=*"1.0"*?>
> <xs:schema xmlns:xs=*"http://www.w3.org/2001/XMLSchema"*>
>
> <xs:complexType name=*"elementType"*>
> <xs:attribute name=*"name"* type=*"xs:string"* use=*"required"* />
> <xs:attribute name=*"value"* type=*"xs:string"* default=*"**CCC**"* />
> </xs:complexType>
>
> <xs:element name=*"elements"*>
> <xs:complexType>
> <xs:sequence>
> <xs:element name=*"element"* type=*"elementType"* maxOccurs=*
> "unbounded"*/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
>
> </xs:schema>
> ==========================================
>
>
> Elements.java
> ==========================================
> *package* simple;
>
> *import* org.simpleframework.xml.Attribute;
> *import* org.simpleframework.xml.ElementList;
> *import* org.simpleframework.xml.Root;
> *import* org.simpleframework.xml.util.Dictionary;
>
> @Root(name="elements")
> *public* *class* Elements {
> @Attribute(name="noNamespaceSchemaLocation") *private* String
> noNamespaceSchemaLocation;
>
> @ElementList(entry = "element", inline = *true*)
> *private* Dictionary<Element> elements = *new*Dictionary<Element>();
>
> *public* Elements(@ElementList(entry = "element", inline = *true*)
> Dictionary<Element> resources) {
> *this*.elements = resources;
> }
>
> *public* Element getElementByName(String name){
> *return* elements.get(name);
> }
> }
> ==========================================
>
> Element.java
> *package* simple;
>
> *import* org.simpleframework.xml.Attribute;
> *import* org.simpleframework.xml.Root;
> *import* org.simpleframework.xml.util.Entry;
>
> @Root
> *public* *class* Element *implements* Entry{
> @Attribute(name="name") *private* String name;
> @Attribute(name="value", required=*false*) *private* String value;
>
> *public* Element(@Attribute(name="name") String name, @Attribute(name=
> "value", required=*false*) String value) {
> *this*.name = name;
> *this*.value = value;
> }
>
> *public* String getName() {
> *return* name;
> }
>
> *public* String getValue() {
> *return* value;
> }
> }
> ==========================================
>
>
>
> ------------------------------------------------------------------------------
> RSA(R) Conference 2012
> Mar 27 - Feb 2
> Save $400 by Jan. 27
> Register now!
> http://p.sf.net/sfu/rsa-sfdev2dev2
> _______________________________________________
> Simple-support mailing list
> Sim...@li...
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
|