Re: [Simple-support] How to parse an xml element with a provided default value from xsd?
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2012-01-14 00:44:52
|
This is really a question for the underlying parser, Simple uses StAX, DOM, or KXML depending on the platform you are on. Simple does not deal directly with parsing.
________________________________
From: Alexander Poulikakos <ale...@er...>
To: "sim...@li..." <sim...@li...>
Sent: Friday, 13 January 2012 8:27 AM
Subject: [Simple-support] How to parse an xml element with a provided default value from xsd?
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:
==========================================
publicstaticvoidmain(String[] args) throwsThrowable {
Serializer serializer = newPersister();
Elements elements = serializer.read(Elements.class, newFile("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
==========================================
<?xmlversion="1.0"encoding="UTF-8"?>
<elements
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="my.xsd">
<elementname="propA"value="AAA"/>
<elementname="propB"value="BBB"/>
</elements>
==========================================
my.xsd
==========================================
<?xmlversion="1.0"?>
<xs:schemaxmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexTypename="elementType">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="value" type="xs:string" default="CCC" />
</xs:complexType>
<xs:elementname="elements">
<xs:complexType>
<xs:sequence>
<xs:element name="element" type="elementType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
==========================================
Elements.java
==========================================
packagesimple;
importorg.simpleframework.xml.Attribute;
importorg.simpleframework.xml.ElementList;
importorg.simpleframework.xml.Root;
importorg.simpleframework.xml.util.Dictionary;
@Root(name="elements")
publicclassElements {
@Attribute(name="noNamespaceSchemaLocation") privateString noNamespaceSchemaLocation;
@ElementList(entry = "element", inline = true)
privateDictionary<Element> elements= newDictionary<Element>();
publicElements(@ElementList(entry = "element", inline = true) Dictionary<Element> resources) {
this.elements= resources;
}
publicElement getElementByName(String name){
returnelements.get(name);
}
}
==========================================
Element.java
packagesimple;
importorg.simpleframework.xml.Attribute;
importorg.simpleframework.xml.Root;
importorg.simpleframework.xml.util.Entry;
@Root
publicclassElement implementsEntry{
@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 |