- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在 XSD/XML 到 Java 的方向上使用 JAXB。我的 XSD 包含派生类型,我必须解码的 XML 包含 xsi:type 属性。查看代码 JAXB(默认的 Sun 实现)已经生成(用于解码)似乎没有方法来获取这些属性。
是不是因为我总能对解码的 Java 对象执行 getClass()
并找到实际的类?
不过,根据我提供给 JAXBContext.newInstance
的包或类,是否可以实例化一些基类? (一个是对应于 xsi:type 属性的实际值的类的父类(super class))。在这种情况下,可能需要能够读取 XML 实例中出现的实际属性的值。
最佳答案
JAXB (JSR-222)实现将为您处理一切。 JAXB 认为每个类对应一个复杂类型。它有一个计算类型名称的算法,但您可以使用 @XmlType
注释覆盖它。当一个元素被解码时,如果它包含一个 xsi:type
属性,那么 JAXB 将查看是否有与该类型关联的类。如果存在,它将实例化该类型的类,如果不存在,它将根据通过注释提供的映射元数据实例化与该元素对应的类型。
了解更多信息
更新
下面是一个可能有帮助的例子:
schema.xsd
在复杂类型 canadianAddress
下的 XML 架构中扩展了复杂类型 address
。
<?xml version="1.0" encoding="UTF-8"?>
<schema
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/customer"
xmlns:tns="http://www.example.org/customer"
elementFormDefault="qualified">
<element name="customer">
<complexType>
<sequence>
<element name="address" type="tns:address"/>
</sequence>
</complexType>
</element>
<complexType name="address">
<sequence>
<element name="street" type="string"/>
</sequence>
</complexType>
<complexType name="canadianAddress">
<complexContent>
<extension base="tns:address">
<sequence>
<element name="postalCode" type="string"/>
</sequence>
</extension>
</complexContent>
</complexType>
</schema>
演示
在下面的演示代码中,XML 将转换为从上述 XML 模式生成的 JAXB 模型,然后再转换回 XML。
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("org.example.customer");
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/org/example/customer/input.xml");
Customer customer = (Customer) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(customer, System.out);
}
}
input.xml/输出
下面是 XML。 address
元素使用 xsi:type
进行限定,表明它拥有 canadianAddress
的实例,而不仅仅是一个 address
.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer xmlns="http://www.example.org/customer">
<address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="canadianAddress">
<street>1 A Street</street>
<postalCode>Ontario</postalCode>
</address>
</customer>
关于java - JAXB : how to read the xsi:type value when unmarshalling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16903618/
对于以下 XML 片段: xmlns、xmlns:xsi 和 xsi:schemaLocation 属性的确切含义是什么?它们有什么关系? : 的作用是什么? xsi:schemaLocation=
据我所知,按照惯例,默认命名空间在 XML 文档的根元素中命名为 xmlns 属性。其他 namespace 被命名为其他名称。现在,我想知道为什么 XML 方案实例命名空间总是命名为 xmlns:x
我无法根据 XSD 验证我的 XML。验证器抛出 The prefix "xsi" for attribute "xsi:schemaLocation" associated with an elem
我正在尝试针对 JMeter 中的 xsd 模式验证 xml 响应,但断言总是失败并显示错误 Cannot resolve 'job' to a type definition for element
当我尝试从部署到 WebSphere 7.0 的类调用 WebService(HP-Service Manager,如果它有帮助的话)时出现错误。当我调用它时,相同的代码起作用从 JUnit 代码..
我有一组 JAXB 生成的类,其中一些类有 setter 方法接受“对象”作为参数。例如: @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name="C
我使用 JAXB 创建文件夹和文件层次结构 我的模型: @XmlRootElement public class Root { @XmlAttribute private String
为什么会出现这个错误 [Fatal Error]: The prefix "xsi" for attribute "xsi:schemaLocation" associated with an ele
启动 tomcat 时出现错误: javax.servlet.UnavailableException: org.xml.sax.SAXParseException: The prefix "xsi"
在使用 JAXB 时,我想在使用泛型时从我的 XML 元素中删除多余的命名空间/类型。我该怎么做或者我做错了什么?我想使用泛型,这样我只需编写一次代码块。 示例代码: public static vo
请帮助我摆脱这个错误,我没有得到我该如何解决这个问题? 这是我的 web.xml 和 Dispatcher-servlet.xml 文件: AngSpring index
我需要复制一个 xml header : 用我的代码: 'Export the object to XML Dim writer As New XmlSerializ
将 WCF Restful 服务与 XmlSerializer 结合使用,我得到以下响应。 0010327457 false
如何更改 Castor 映射
我正在使用 asp.net MVC4 Web Api。 我已经设置: Dim xml = GlobalConfiguration.Configuration.Formatters.XmlFormatt
这是我的问题:我需要在 XML 中显示 by 对象的名称标签,但没有可空属性:简而言之,如 。这是该对象的代码。如果 name 为空,我会得到标签 ,但是附加的属性可能会给客户端带来问题。 正如我
我正在使用 xjc 创建 JAXB 类。我正在使用以下命令 xjc -d src -p com.abc.proj the-types.xsd 我收到以下错误 parsing a schema... [
下面的代码生成下面给出的 xml,但没有 xsi:nil=true,为什么它没有生成该属性? 来自以下演示代码的 XML:
这是我所反对的一个普遍的例子。 我已经在我的模式中创建了派生类型,并且想要创建一个元素,它是一个无界列表(序列),它有一个限制,只允许三个派生类型中的两个。 从顶层的角度来说,“我有一些事件,在一种情
我发现我们有多个 url 作为此属性的值,就像在 spring 中一样: 为什么需要它以及它的用途是什么? Spring 是否会访问 url 并进行验证?xmlns 和 xsi:schemaLoca
我是一名优秀的程序员,十分优秀!