有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何反序列化XML列表?

我正在使用jaxb2对 the MPEG Dash schema 进行反序列化

Java类的生成非常有效,但是Dash清单中的部分信息丢失了。也就是说,包含在ContentProtection元素中的数据。看起来像这样:

  <ContentProtection schemeIdUri="urn:uuid:SomeIdHash" value="PlayReady">                                                                                
    <cenc:pssh>Base64EncodedBlob</cenc:pssh>          
    <mspr:pro>Base64EncodedBlob</mspr:pro>                                                        
  </ContentProtection> 

默认模式将内部字段抛出到带有@XmlAnyElement注释的DescriptorType类中,该类将生成如下所示的对象列表:

[[cenc:pssh: null], [mspr:pro: null]] 

为了解决这个问题,我为ContentProtection创建了自己的jxb绑定,如下所示:

<jxb:bindings node="//xs:complexType[@name='RepresentationBaseType']/xs:sequence/xs:element[@name='ContentProtection']">
    <jxb:class ref="com.jmeter.protocol.dash.sampler.ContentProtection"/>
</jxb:bindings>

然而,我并没有更接近于获得其中包含的Base64EncodedBlob信息。我不知道如何在自定义类中设置注释以正确构造列表。这就是我尝试过的

 //@XmlAnyElement(lax = true) //[[cenc:pssh: null], [mspr:pro: null]] 
  // @XmlJavaTypeAdapter(Pssh.class) //DOESNT WORK
  // @XmlValue //Empty List???
  // @XmlSchemaType(name = "pssh")
  // @XmlElementRef(name = "pssh") //Not matching annotations
  // @XmlElement(name = "enc:pssh") //Not matching annotations
  protected List<Pssh> pssh;

public List<Pssh> getPssh() {
    if (pssh == null) {
      pssh = new ArrayList<Pssh>();
    }
    return this.pssh;
  }

我的Pssh课程看起来像:

package com.jmeter.protocol.dash.sampler;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlType(name = "enc:pssh")
// @XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "cenc:pssh")

public class Pssh {

  // @XmlValue
  //@XmlElement
  private String psshValue;

  public String getPsshValue() {
    return psshValue;
  }

  public void setPsshValue(String psshValue) {
    this.psshValue = psshValue;
  }
}

我该怎么做才能使Pssh对象的列表用base64 blob而不是null构建


共 (1) 个答案

  1. # 1 楼答案

    我建议您在JAXB解组器上启用XML模式验证。它通常会告诉您XML输入中阻止JAXB正确解组的所有错误内容(比如不知道映射到哪个Java类的意外XML内容)。这将使调试这类东西变得更容易。例如,我在XML中没有看到前缀cencmspr的任何名称空间声明。如果未声明/未定义,JAXB不知道它们的XML类型定义。您还需要为它们提供模式,以进行完整的模式验证。虽然processContents="lax"不强制要求进行完整的模式验证,但它有助于及早发现潜在的反序列化问题。此外,一般来说,验证输入总是更安全的

    最后但并非最不重要的一点是,确保创建解组器的JAXBContext知道要将cenc:psshmsgpr:pro(它们的XML类型)绑定到的额外JAXB注释类。比如:

    JAXBContext.newInstance(Pssh.class,Pro.class,...);