创建具有XSI的XML元素:零和在.net / JAXB属性 [英] Creating an XML element with xsi:nil and attributes in .Net/Jaxb

查看:133
本文介绍了创建具有XSI的XML元素:零和在.net / JAXB属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML Schema,上面写着:

I have an XML Schema that says:

<xs:element name="employerOrganization" nillable="true" minOccurs="1" maxOccurs="1">
  <xs:complexType>
    <xs:sequence>
      ...
    </xs:sequence>
    <xs:attribute name="classCode" type="EntityClassOrganization" use="required"/>
    <xs:attribute name="determinerCode" type="EntityDeterminerSpecific" use="required"/>
  </xs:complexType>
</xs:element>

这意味着我必须能够创建一个看起来像这样的实例:

That means I must be able to create an instance that looks like this:

<employerOrganization classCode="ORG" determinerCode="INSTANCE" xsi:nil="true"/>

根据XML模式规范我的可以 HTTP ://www.w3.org/TR/xmlschema-0/#Nils )。根据微软的.Net我的不能 http://msdn.microsoft.com/en-us/library/ybce7f69(V = VS.100)的.aspx ),并尽可能别人告诉我,JAXB可以没有。

According to the XML Schema spec I can (http://www.w3.org/TR/xmlschema-0/#Nils). According to Microsoft .Net I cannot (http://msdn.microsoft.com/en-us/library/ybce7f69(v=vs.100).aspx) and as far as others tell me Jaxb cannot either.

是.NET和JAXB uncompliant?我可以覆盖以某种方式获得所需的输出?

Are both .Net and Jaxb uncompliant? Can I override somehow to get the desired output?

推荐答案

在JAXB可以利用一个的JAXBElement 这一点。该的JAXBElement 可以容纳一个值,有田/属性映射为XML属性和一个标志,跟踪元素是否为零。

In JAXB you can leverage a JAXBElement for this. The JAXBElement can hold a value which has fields/properties mapped to XML attributes and a flag that tracks whether the element was nil.

而不是有类型的字段/属性酒吧指定的JAXBElement&LT;酒吧&GT;

Instead of having a field/property of type Bar you specify JAXBElement<Bar>.

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    @XmlElementRef(name="bar")
    private JAXBElement<Bar> bar;

}

酒吧

酒吧有田/属性映射为XML属性。

Bar has fields/properties mapped to XML attributes.

import javax.xml.bind.annotation.XmlAttribute;

public class Bar {

    @XmlAttribute
    private String baz;

}

的ObjectFactory

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    @XmlElementDecl(name="bar")
    public JAXBElement<Bar> createBar(Bar bar) {
        return new JAXBElement<Bar>(new QName("bar"), Bar.class, bar);
    }

}

演示code

演示

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class, ObjectFactory.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum19797412/input.xml");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

input.xml中/输出

<?xml version="1.0" encoding="UTF-8"?>
<foo>
    <bar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" baz="Hello World" xsi:nil="true"/>
</foo>

这篇关于创建具有XSI的XML元素:零和在.net / JAXB属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆