[Simple-support] How to parse an xml element with a provided default value from xsd?
Brought to you by:
niallg
|
From: Alexander P. <ale...@er...> - 2012-01-12 21:27:47
|
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;
}
}
==========================================
|