- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个生成 jaxb 类的 xsd。为此,我为通用 map 创建了一个 XML 适配器。该适配器通过单元测试,并且生成的变量的类型适本地是预期的映射。但问题是,当尝试验证从此场景创建的对象时,我得到“无法将‘hashMap’类型解析为元素‘autoExecuteArguments’的定义。我已经摆弄了很多,但总是以上述内容的一些变体结束错误。这两个绑定(bind)没有一起使用,但举例说明了达成某种解决方案的尝试。
<xjc:javaType name="java.util.Map" xmlType="mp:MapTypeEntry"
adapter="mil.dod.th.ose.core.impl.mp.UtilMapConverter" />
<jaxb:bindings schemaLocation="resources/missionProgramSchema/MissionProgram.xsd">
<jaxb:bindings node="//xs:element[@name='MissionProgramInstance']">
<jaxb:bindings node="//xs:element[@name='autoExecuteArguments']">
<jaxb:property>
<jaxb:baseType name="java.util.HashMap">
<xjc:javaType name="java.util.Map"
adapter="//core.impl.mp.UtilMapConverter" />
</jaxb:baseType>
</jaxb:property>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
我确信最后一个是对绑定(bind)的 mock ,因此我无法使其在大量调整下工作,我得到“[错误]编译器无法支持此属性自定义。它附加到错误的位置,或者与其他绑定(bind)不一致。
接下来是转换器:
@XmlSeeAlso(UtilMapConverter.MapType.class)
public class UtilMapConverter<K, V> extends XmlAdapter<UtilMapConverter.MapType<K,V>, Map<K, V>> {
@Override
public UtilMapConverter.MapType<K, V> marshal(final Map<K, V> map)
{
final MapType<K, V> mapType = new MapType<K, V>();
for (Map.Entry<K, V> entry : map.entrySet())
{
final MapTypeEntry<K, V> mapEntryType = new MapTypeEntry<K, V>();
mapEntryType.setKey(entry.getKey());
mapEntryType.setValue(entry.getValue());
mapType.getEntry().add(mapEntryType);
}
return mapType;
}
@Override
public Map<K, V> unmarshal(final UtilMapConverter.MapType<K, V> genericMap)
{
final Map<K, V> map = new HashMap<K, V>();
for (MapTypeEntry<K, V> mapEntryType : genericMap.getEntry())
{
map.put(mapEntryType.getKey(), mapEntryType.getValue());
}
return map;
}
@XmlAccessorType(XmlAccessType.PROPERTY)
public static class MapType<K, V>
{
private List<MapTypeEntry<K, V>> m_Entry = new ArrayList<MapTypeEntry<K, V>>();
public MapType()
{
}
public MapType(final Map<K, V> map)
{
for (Map.Entry<K, V> e : map.entrySet())
{
m_Entry.add(new MapTypeEntry<K, V>(e));
}
}
@XmlElement
@XmlSchemaType(name = "mapType")
public List<MapTypeEntry<K, V>> getEntry()
{
return m_Entry;
}
public void setEntry(final List<MapTypeEntry<K, V>> entry)
{
this.m_Entry = entry;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public static class MapTypeEntry<K, V>
{
@XmlElement(required = true)
@XmlSchemaType(name = "key")
protected K m_Key;
@XmlElement(required = true)
@XmlSchemaType(name = "value")
protected V m_Value;
public MapTypeEntry()
{
}
public MapTypeEntry(final Map.Entry<K, V> entry)
{
m_Key = entry.getKey();
m_Value = entry.getValue();
}
@XmlElement
public K getKey()
{
return m_Key;
}
@XmlElement
public V getValue()
{
return m_Value;
}
}
我知道我的情况一团糟。我只是在疯狂地试图弄清楚什么是有帮助的,什么是有害的,什么是愚蠢的。最后是 xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns="http://core/mp/model" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" targetNamespace="http://core/mp/model"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" elementFormDefault="qualified"
id="MissionProgramInstance" jaxb:version="2.1"
jaxb:extensionBindingPrefixes="xjc">
<xs:include schemaLocation="MissionProgram.xsd" />
<xs:element name="MissionProgramInstance">
<xs:complexType>
<xs:sequence>
<xs:element name="program" type="MissionProgram"
minOccurs="1" />
<xs:element name="autoExecuteArguments" minOccurs="0">
<xs:annotation>
<xs:appinfo>
<jaxb:property>
<jaxb:baseType name="java.util.Map" />
</jaxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
<xs:attribute name="autoExecuteEnabled" type="xs:boolean"
use="required" />
<xs:attribute name="assetDep" use="optional">
<xs:simpleType>
<xs:list itemType="xs:string" />
</xs:simpleType>
</xs:attribute>
<xs:attribute name="physicalLinkDep" use="optional">
<xs:simpleType>
<xs:list itemType="xs:string" />
</xs:simpleType>
</xs:attribute>
<xs:attribute name="linkLayerDep" use="optional">
<xs:simpleType>
<xs:list itemType="xs:string" />
</xs:simpleType>
</xs:attribute>
<xs:attribute name="transportLayerDep" use="optional">
<xs:simpleType>
<xs:list itemType="xs:string" />
</xs:simpleType>
</xs:attribute>
<xs:attribute name="imageStreamDep" use="optional">
<xs:simpleType>
<xs:list itemType="xs:string" />
</xs:simpleType>
</xs:attribute>
<xs:attribute name="programDep" use="optional">
<xs:simpleType>
<xs:list itemType="xs:string" />
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:complexType name="MissionProgram">
<xs:sequence>
<!--This field holds source data for a mission program -->
<xs:element name="source" type="xs:string" minOccurs="1" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
<xs:complexType name="MapType">
<xs:sequence>
<xs:element name="autoExecuteArgument" type="MapTypeEntry">
<xs:annotation>
<xs:appinfo>
<jaxb:property>
<jaxb:baseType name="//core.mp.impl.UtilMapConverter.MapType" />
</jaxb:property>
</xs:appinfo>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="MapTypeEntry">
<xs:list itemType="xs:anySimpleType" />
</xs:simpleType>
</xs:schema>
当前演示中有一些冗余。我是 xsd 和 jaxb 的新手。我试图理解这一切,但尽管尝试进行研究,我认为我找错了地方,因为我似乎找到了一些碎片,但没有找到所有这些共同作用的解释。这是我尝试将至少部分内容放在一起的主要资源之一:http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html 。还有 xml 适配器的 javadocs,以及到处浏览的内容。谢谢您至少花时间思考“您会做什么?”
最佳答案
我会做的(并且已经完成的)是编写一个干净的 xsd 并使用 xjc 生成 java 类。如果您需要更“编码器友好”的数据结构,请编写自己的代码来转换反序列化后的 jaxb 对象。试图强制 xml 符合您所希望的数据结构外观通常是一种令人沮丧的练习,并且随着您的项目随着时间的推移而发生变化,这会让您的生活变得困难。
关于java - 根据 xsd 验证 java.util.Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11925639/
我知道我以前在某个地方看到过这个,但我再也找不到它了。我需要一个从另一个 xsd 中调用 xsd 文件的示例。这在生成大量 xml 文件的情况下非常有用,但在这些 xml 文件之间存在大量公共(pub
有人知道供应商中立的 XSD 来描述关系数据库模式吗?我们的系统需要获取有关数据库结构的信息: 表 列和类型 主键和外键约束 索引 等 以独立于供应商的方式并将其存储在 XML 文件中以供以后处理。
我在 XSD 中使用 gml (3.1.1) XSD 作为我的应用程序。我想下载版本 3.1.1 中的所有 gml XSD,例如 zip 文件。换句话说:基本 xsd 是 here我想用 zip 文件
我想要一个 XSD 来验证包含具有许多别名但每个别名具有不同值的文件元素的 XML。 这是我的 XML: Document1
我看到一个 xml 架构 ( EPP ) 将 xsd:choice 与一个元素一起使用,即使我们可以使用 xsd:enumeration 代替:
我目前工作的公司将架构或契约(Contract)版本编入根节点。例如, ... 我正在寻找人们对这种设计方法的意见,因为我不相信它是合理的。例如,它要求所有使用此模式作为消息传递契约的服务都能够发
我在处理 Web 服务响应时遇到了 Apache CXF 解析错误。归结为一个空元素被返回: 元素定义如下: 现在我在 CXF 邮件列表上看到了 empty value is not allowe
XSD 可以为比较两个元素添加约束吗? 假设我在 DataRangeType 下有 Begin End 我想添加一个约束说 Begin 非常
我想声明一个要包含在复杂类型声明中的元素,并且该元素有一个强制属性:“option=MyOption”,但是“选项”属性的值可以是任何值,具体取决于上下文。 也就是说:在使用包含该元素的复杂类型的任何
我需要能够将简单元素类型设置为整数,但也可以将其设置为空。如果此示例为空且空白字段不是整数,则此示例将发送错误。我该如何解决? 最佳答案 您要做的是在同一元素上分配限制,并对其进行合并,例如以下示例
对于这个 xml: 我有这个模式,它似乎可以根据 w3 schema validation service 进行验证,并且该架构可以很好地验证上述 XML。遗憾的是,xsd.exe
我有两个 XSD 文件(源文件和目标文件)...我应该在什么基础上映射这两个文件以获得 XSLT?我知道 MapForce 如何帮助映射,但我使用示例项目..现在我想知道我应该在什么基础上映射我的客户
我有一个 .cs 文件,其中包含 XTypedElement 和 IXMetaData 的子类。微软有一个 tool that generates XSD files automatically来自托
这个问题在这里已经有了答案: XML Schema to validate XML Schemas? (3 个答案) 关闭 9 年前。 是否存在可验证其他 XML 架构的 Xml 架构? 我想做的是
假设我正在处理一个 xsd:simpleType,它是一个字符串,需要具有特定的字符集和特定的最大长度,类似于以下代码: 所以我的 xsd 类型将是一个只
JAXB 同时映射 xsd:base64Binary和 xsd:hexBinary类型为 byte[] . 鉴于我有一个架构/DOM 元素代表这些类型中的每一个,例如: ABCD对于 xsd:hexB
我非常确定我在这里遗漏了一些简单的东西。 我正在使用 netbeans 在两个单独的项目中创建一个 web jax-ws web 服务和一个客户端,并且我有一些自定义绑定(bind)已使用 net b
将字节数组表示为 XSD 架构的最佳方式是什么?我有一个字节输入,我需要解析它并将其提供给 JAXB 从 XSD 模式生成的 Java 对象以供将来验证。我输入中的每条信息都由偏移量和长度定义。我想将
我的架构的这一部分给我带来了麻烦:
我需要定义元素“MyData”的名为“DataValue”的属性。但要求是“DataValue”的类型可以动态更改,即数据值在一个实例中可以是字符串,而在其他实例中它可以是 int 或 bool。它可
我是一名优秀的程序员,十分优秀!