- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 XML 要使用 JAXB 加载。它加载正确,除了 getEffectSettings() 大小为 0,而它应该显示 2,因为我在“settings-list”元素中有两个“effect-setting”。有任何想法吗?我能够加载并打印效果的目标属性,并且效果按其应有的方式显示(除了该变量之外)。
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>
路径中的节点:
RawItemDefinition.java(根节点): 包 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;
}
}
效果.java: 包 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;
}
}
我使用的解码器是这样的:
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<>();
for (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());
}
}
这就是我尝试访问效果的方式:
@PostConstruct
public void init() throws Exception {
List<RawItemDefinition> rawItemDefinitionList = xmlDefinitionLoader.loadDefinitions(RawItemDefinition.class, "classpath:data/item/definition/", "classpath:data/item/schema.xsd");
for (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));
}
}
最佳答案
您能否提供调用 getEffectSettings()
的代码以及如何完成解码(手动或任何框架?)。
更新
您是否尝试过将 @XmlRootElement
添加到 EffectSetting
类中?
更新2
正如您发现的那样,检查您的类的包:
The
Effect
andEffectSetting
was in an other package thanRawItemData
and I added thepackage-info.java
to theRawItemData
's package. After adding it to theEffect
's package it's started working.
关于java - JAXB @XmlElementWrapper ArrayList 大小为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32874726/
我正在尝试创建一个类似于下面提到的 xml 文件。 value1 value2 能否请您告诉我,如何定义类层次结构。
在我的代码中我有这个 bean: @XmlRootElement @XmlAccessorType(XmlAccessType.NONE) public class Data implements S
我在编码具有双向关系的类时遇到问题。以下示例将通过编码 PhoneNumber 实例来生成无效的 xml。 yvonne 12345
文档说明 @XmlElementWrapper 注释可用于“未包装”或“包装”集合。 http://docs.oracle.com/javaee/5/api/javax/xml/bind/annota
下面有这段代码 @XmlRootElement(name = "FNOL") @XmlAccessorType(XmlAccessType.FIELD) public class Conversati
我正在使用 jaxb 创建以下 xml 结构: @XmlElementWrapper @XmlElement(name = "picture") private List pictures; 创建:
我有一个 XML 要使用 JAXB 加载。它加载正确,除了 getEffectSettings() 大小为 0,而它应该显示 2,因为我在“settings-list”元素中有两个“effect-se
我正在尝试使用 JAXB/MOXy 来映射由一组具有独特特征的接口(interface)表示的域:所有多值元素都用 Iterables 封装。与数组或 Collection 相反s。下面是一个演示我的
如何告诉 JAXB 对包装在 @XmlElementWrapper 集合中的数据使用 @XmlRootElement 注释?我知道您可以使用 @XmlElement 来命名每个单独的项目,但我很好奇是
我正在使用 Swagger 为基于 jax-rs 的 API 生成文档。在我的一个模型中,我具有以下属性: @XmlElementWrapper(name = "clip_list") @XmlEle
我想生成如下所示的 XML: 这就是我在代码中生成 mainNode1 、 mainNode2 和 node1 的方式: @Xml
我们使用 Jackson 2.9.4 和 JaxB Annotations 序列化为 JSON。 @XMLElementWrapper 注释没有按预期添加额外的包装级别。相反,它只是更改元素的名称。
我有一个如下所示的 XML 文档,并希望对其执行解码 Lon
我遇到了一个字段的 JAXB 注释问题,该字段是一个泛型类型为接口(interface)的列表。当我声明它时: @XmlAnyElement private List animals; 每件事都正常工
我有一个类,其中有一些我想通过 Jackson 进行编码的字符串列表。为了更好地使用,我想在每个列表中列出相同的元素名称。所以我这样注释: import java.util.List; import
我正在尝试根据 Jersey/Jaxb aliasing a List of beans 上接受的答案在 Artcile 的评论列表周围添加一个包装类 public class Article imp
我想为 xml 根元素和列表元素指定自定义名称。但注释不起作用。 @XmlRootElement(name = "test") @XmlAccessorType(XmlAccessType.FIELD
我正在使用 jackson-databind:2.9.3 和 jackson-module-jaxb-annotations:2.9.3 我有以下格式的 JSON。 { "response":
我有一个带有 XmlElementWrapper 注释的类,例如: ... @XmlElementWrapper(name="myList") @XmlElements({ @Xm
如何将属性添加到XmlElementWrapper?我觉得很难做到这一点。我找不到讨论它的文章或教程和问题。 我希望我的 XML 像 ff:
我是一名优秀的程序员,十分优秀!