代码之家  ›  专栏  ›  技术社区  ›  Alan Silva

如何反序列化XML列表?

  •  0
  • Alan Silva  · 技术社区  · 7 年前

    我正在反序列化 the MPEG Dash schema 使用jaxb2。

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

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

    默认模式将内部字段抛出到 DescriptorType 使用类 @XmlAnyElement 产生如下对象列表的注释:

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

    为了解决这个问题,我为 内容保护 像这样:

    <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 回复  |  直到 6 年前
        1
  •  0
  •   cdan    7 年前

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

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

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