【问题标题】:JAXB Marshaller with specified schema具有指定模式的 JAXB Marshaller
【发布时间】:2010-12-17 18:00:23
【问题描述】:

我想在我编组的每个 xml 文件中定义架构。然后在解组过程中获取此模式(路径字符串)。仅限marshaller.setSchema()

允许调用者在编组时验证编组的 XML。

是的,我可以为此编写额外的 bean,但我想获得类似的 xml

<root
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation='bla-bla.xsd'>...

【问题讨论】:

    标签: java xsd jaxb


    【解决方案1】:

    要指定 noNamespaceSchemaLocation,您可以执行以下操作:

    JAXBContext jc = JAXBContext.newInstance(Root.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "bla-bla.xsd");
    

    【讨论】:

    • 这是我需要的。非常感谢。
    • 我在解组此属性时遇到了一些问题。请参阅stackoverflow.com/questions/4478666/…
    • @Stas Kurilin,我现在已经发布了一个利用 XMLStreamReader(StAX 解析器)获取 noNamespaceSchemaLocation 的问题的答案。一旦获得 noNamespaceSchemaLocation,JAXB 就可以使用 XMLStreamReader 来执行解组。
    • @BlaiseDoughan 在设置属性 noNamespaceSchemaLocation 我得到 javax.xml.bind.PropertyException: name: Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION value : "somexsd.xsd" 这个错误的可能原因是什么?跨度>
    • 你能做同样的事情来获取模式的版本号吗?即: 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 1970-01-01
    相关资源
    最近更新 更多