java - JAXB @XmlElementWrapper ArrayList size zero -
i have xml load jaxb. load correctly, except geteffectsettings() size 0 when should show 2 because have 2 'effect-setting' in 'settings-list' element. ideas? i'm able load , print effect's target attribute , effects shown should except variable.
the xml:
<item:item xmlns:item="http://www.swordsandsorcery.com/item" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.swordsandsorcery.com/item ../schema.xsd"> <item:id>1</item:id> <item:name>laxi's epic</item:name> <item:type>one_handed_axes</item:type> <item:usable>true</item:usable> <item:use-effect-list> <item:effect target="com.xyz.healeffect"> <item:settings-list> <item:effect-setting> <item:setting-name>amount</item:setting-name> <item:setting-value>10</item:setting-value> </item:effect-setting> <item:effect-setting> <item:setting-name>xyz</item:setting-name> <item:setting-value>10</item:setting-value> </item:effect-setting> </item:settings-list> </item:effect> </item:use-effect-list> <item:weight>5</item:weight> <item:combat-modifiers> <item:modifier> <item:attribute>attack</item:attribute> <item:amount>1</item:amount> </item:modifier> <item:modifier> <item:attribute>defense</item:attribute> <item:amount>1</item:amount> </item:modifier> <item:modifier> <item:attribute>life</item:attribute> <item:amount>3</item:amount> </item:modifier> </item:combat-modifiers> <item:general-modifiers> <item:modifier> <item:attribute>strength</item:attribute> <item:amount>1</item:amount> </item:modifier> </item:general-modifiers> <item:skill-modifiers> <item:modifier> <item:attribute>two_handed_crushing_weapons</item:attribute> <item:amount>2</item:amount> </item:modifier> </item:skill-modifiers> <item:basic-modifiers> <item:modifier> <item:attribute>movement</item:attribute> <item:amount>1</item:amount> </item:modifier> </item:basic-modifiers> <item:general-requirements> <item:requirement> <item:attribute>dexterity</item:attribute> <item:amount>100</item:amount> </item:requirement> </item:general-requirements> </item:item>
the nodes in path:
rawitemdefinition.java (root node): package com.morethanheroic.swords.item.service.domain;
import com.morethanheroic.swords.effect.domain.effect; import com.morethanheroic.swords.item.domain.itemtype; import javax.xml.bind.annotation.*; import java.util.arraylist; import java.util.collections; import java.util.list; @xmlrootelement(name = "item") @xmlaccessortype(xmlaccesstype.field) public class rawitemdefinition { private int id; private string name; private itemtype type; private boolean usable; private int weight; @xmlelementwrapper(name = "use-effect-list") @xmlelement(name = "effect") private arraylist<effect> effectlist; @xmlelementwrapper(name = "basic-modifiers") @xmlelement(name = "modifier") private arraylist<basicattributemodifierdefinition> basicmodifiers; @xmlelementwrapper(name = "combat-modifiers") @xmlelement(name = "modifier") private arraylist<combatattributemodifierdefinition> combatmodifiers; @xmlelementwrapper(name = "general-modifiers") @xmlelement(name = "modifier") private arraylist<generalattributemodifierdefinition> generalmodifiers; @xmlelementwrapper(name = "skill-modifiers") @xmlelement(name = "modifier") private arraylist<skillattributemodifierdefinition> skillmodifiers; @xmlelementwrapper(name = "basic-requirements") @xmlelement(name = "requirement") private arraylist<basicattributerequirementdefinition> basicrequirements; @xmlelementwrapper(name = "combat-requirements") @xmlelement(name = "requirement") private arraylist<combatattributerequirementdefinition> combatrequirements; @xmlelementwrapper(name = "general-requirements") @xmlelement(name = "requirement") private arraylist<generalattributerequirementdefinition> generalrequirements; @xmlelementwrapper(name = "skill-requirements") @xmlelement(name = "requirement") private arraylist<skillattributerequirementdefinition> skillrequirements; public int getid() { return id; } public string getname() { return name; } public itemtype gettype() { return type; } public int getweight() { return weight; } public boolean isusable() { return usable; } public string tostring() { return "rawitemdefinition -> [id: " + id + " name: " + name + "]"; } public list<basicattributemodifierdefinition> getbasicmodifiers() { return basicmodifiers; } public list<combatattributemodifierdefinition> getcombatmodifiers() { return combatmodifiers; } public list<generalattributemodifierdefinition> getgeneralmodifiers() { return generalmodifiers; } public list<skillattributemodifierdefinition> getskillmodifiers() { return skillmodifiers; } public list<skillattributerequirementdefinition> getskillrequirements() { return skillrequirements; } public list<basicattributerequirementdefinition> getbasicrequirements() { return basicrequirements; } public list<combatattributerequirementdefinition> getcombatrequirements() { return combatrequirements; } public list<generalattributerequirementdefinition> getgeneralrequirements() { return generalrequirements; } public list<attributerequirementdefinition> getallrequirements() { list<attributerequirementdefinition> list = new arraylist<>(); if (basicrequirements != null) { list.addall(basicrequirements); } if (combatrequirements != null) { list.addall(combatrequirements); } if (generalrequirements != null) { list.addall(generalrequirements); } if (skillrequirements != null) { list.addall(skillrequirements); } return collections.unmodifiablelist(list); } public arraylist<effect> geteffectlist() { return effectlist; } }
effect.java: package com.morethanheroic.swords.effect.domain;
import javax.xml.bind.annotation.*; import java.util.arraylist; @xmlaccessortype(xmlaccesstype.field) public class effect { @xmlattribute private string target; @xmlelementwrapper(name = "settings-list") @xmlelement(name = "effect-setting") private arraylist<effectsetting> effectsettings; public string gettarget() { return target; } public arraylist<effectsetting> geteffectsettings() { return effectsettings; } }
effectsetting.java:
package com.morethanheroic.swords.effect.domain; import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; import javax.xml.bind.annotation.xmlelement; @xmlaccessortype(xmlaccesstype.field) public class effectsetting { @xmlelement(name = "setting-name") private string name; @xmlelement(name = "setting-value") private string value; public string getname() { return name; } public string getvalue() { return value; } }
the unmarshaller use this:
package com.morethanheroic.swords.definition.service; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.applicationcontext; import org.springframework.stereotype.service; import org.xml.sax.saxexception; import javax.xml.xmlconstants; import javax.xml.bind.jaxbcontext; import javax.xml.bind.jaxbexception; import javax.xml.bind.unmarshaller; import javax.xml.validation.schema; import javax.xml.validation.schemafactory; import java.io.file; import java.io.ioexception; import java.util.arraylist; import java.util.list; @service public class xmldefinitionloader { private static final schemafactory schemafactory = schemafactory.newinstance(xmlconstants.w3c_xml_schema_ns_uri); @autowired private applicationcontext applicationcontext; public list loaddefinitions(class clazz, string resourcepath, string schemapath) throws jaxbexception, ioexception, saxexception { return unmarshalltargetfiles(buildunmarshaller(clazz, schemapath), gettargetfiles(resourcepath)); } private arraylist unmarshalltargetfiles(unmarshaller unmarshaller, file[] files) throws jaxbexception { arraylist list = new arraylist<>(); (file file : files) { list.add(unmarshaller.unmarshal(file)); } return list; } private file[] gettargetfiles(string resourcepath) throws ioexception { return applicationcontext.getresource(resourcepath).getfile().listfiles(); } private unmarshaller buildunmarshaller(class clazz, string schemapath) throws ioexception, saxexception, jaxbexception { unmarshaller unmarshaller = jaxbcontext.newinstance(clazz).createunmarshaller(); unmarshaller.setschema(buildschema(schemapath)); return unmarshaller; } private schema buildschema(string schemapath) throws ioexception, saxexception { return schemafactory.newschema(applicationcontext.getresource(schemapath).getfile()); } }
and how try access effects:
@postconstruct public void init() throws exception { list<rawitemdefinition> rawitemdefinitionlist = xmldefinitionloader.loaddefinitions(rawitemdefinition.class, "classpath:data/item/definition/", "classpath:data/item/schema.xsd"); (rawitemdefinition rawitemdefinition : rawitemdefinitionlist) { if(rawitemdefinition.geteffectlist() != null) { system.out.println("effect: "+rawitemdefinition.geteffectlist().get(0).gettarget()); system.out.println("effect: "+rawitemdefinition.geteffectlist().get(0).geteffectsettings().size()); } itemdefinitionmap.put(rawitemdefinition.getid(), new itemdefinition(rawitemdefinition)); } }
could provide code call geteffectsettings()
, how unmarshalling done (by hand or framework ?).
update
have tried adding @xmlrootelement
effectsetting
class ?
update 2
as found yourself, check packages of classes :
the
effect
,effectsetting
in other packagerawitemdata
, addedpackage-info.java
rawitemdata
's package. after addingeffect
's package it's started working.
Comments
Post a Comment