关于JAXB封送,我有几个简单的问题 . 我正在尝试封送包含以下字段的类:
@XmlElementRef(name = "AlternateVerificationKeys", namespace = "http://schemas.microsoft.com/Azure/MediaServices/KeyDelivery/TokenRestrictionTemplate/v1", type = JAXBElement.class, required = false)
protected JAXBElement<ArrayOfTokenVerificationKey> alternateVerificationKeys;
@XmlElement(name = "Audience", required = true, nillable = true)
@XmlSchemaType(name = "anyURI")
protected String audience;
@XmlElement(name = "Issuer", required = true, nillable = true)
@XmlSchemaType(name = "anyURI")
protected String issuer;
@XmlElement(name = "PrimaryVerificationKey", required = true, nillable = true)
protected TokenVerificationKey primaryVerificationKey;
@XmlElementRef(name = "RequiredClaims", namespace = "http://schemas.microsoft.com/Azure/MediaServices/KeyDelivery/TokenRestrictionTemplate/v1", type = JAXBElement.class, required = false)
protected JAXBElement<ArrayOfTokenClaim> requiredClaims;
@XmlElement(name = "TokenType", required = true)
@XmlSchemaType(name = "string")
protected TokenType tokenType;
并使用以下序列化代码:
public static String asString(JAXBContext pContext, Object pObject) throws JAXBException {
StringWriter sw = new StringWriter();
Marshaller marshaller = pContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.marshal(pObject, sw);
return sw.toString();
}
我得到的输出是:
<TokenRestrictionTemplate xmlns="http://schemas.microsoft.com/Azure/MediaServices/KeyDelivery/TokenRestrictionTemplate/v1">
<ArrayOfTokenVerificationKey>
<TokenVerificationKey
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="SymmetricVerificationKey">
<KeyValue></KeyValue>
</TokenVerificationKey>
</ArrayOfTokenVerificationKey>
<Audience>urn:test</Audience>
<Issuer>http://testacs.com/</Issuer>
<PrimaryVerificationKey
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="SymmetricVerificationKey">
<KeyValue></KeyValue>
</PrimaryVerificationKey>
<ArrayOfTokenClaim>
<TokenClaim>
<ClaimType>urn:microsoft:azure:mediaservices:contentkeyidentifier</ClaimType>
<ClaimValue
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</TokenClaim>
</ArrayOfTokenClaim>
<TokenType>SWT</TokenType>
</TokenRestrictionTemplate>
现在,我面临的问题是:
-
我希望命名空间 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 出现在根元素中,即 TokenRestrictionTemplate 而不是单个子元素 . 我怎样才能做到这一点?
-
我有一些元素,例如 JAXBElement 与 @XmlElementRef(name = "AlternateVerificationKeys" ...) 但当我编组子元素的名称时,似乎是 ArrayOfTokenVerificationKey 而不是 AlternateVerificationKeys . 我怎样才能解决这个问题?
1 回答
这是XML的核心功能 . 您可以通过使用抽象类和从第一个继承的多个具体类来实现此目的 . 您还应该使用
@XmlTransient
和@XmlSeeAlso
来注释抽象类,它们会收到具体类的列表 . 以下是一个例子 .1.使用这些注释创建名为
TokenVerificationKey
的抽象类 .您应该使用
@XmlElementWrapper
使用@XmlElementWrapper
来创建集合(包装器)元素,并使用@XmlElement
来设置子元素名称:此设置与前一个设置相结合,输出如下 .
使用此技术,您将获得此输出:
有条件的信息:
您可能希望在XML的开头定义命名空间一次 . 您应该定义
"com.sun.xml.bind.namespacePrefixMapper"
属性 . 您还可以为命名空间设置首选前缀 .btw:Azure SDK for .NET,使用“i”作为前缀 .