代码之家  ›  专栏  ›  技术社区  ›  Matt Ball

在XStream中用属性反序列化XML文本元素

  •  1
  • Matt Ball  · 技术社区  · 15 年前

    this question .

    我从微软的Bing批处理地理代码服务中获取XML,其中一些元素看起来像这样(从 here

    <DataflowJob>
        <Id>5bf10c37df944083b1879fbb0556e67e</Id>
        <Link role="self">https://spatial.virtualearth.net /REST/v1/dataflows/Geocode/5bf10c37df944083b1879fbb0556e67e</Link>
        <Link role="output" name="succeeded">https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/5bf10c37df944083b1879fbb0556e67e/output/succeeded</Link>
        <Link role="output" name="failed">https://spatial.virtualearth.net/REST/v1/dataflows/Geocode/5bf10c37df944083b1879fbb0556e67e/output/failed</Link>
        <Description>Xml</Description>
        <Status>Completed</Status>
        ...
    </DataflowJob>
    

    请注意 <Link> 元素具有属性和文本内容。以下是我试图反序列化的相关POJO类:

    class DataflowJob
    {
        String Id;
        @XStreamImplicit
        List<Link> Links;
        String Description;
        Status Status;
        ...
    }
    
    class Link
    {
        @XStreamAsAttribute
        Role role;
        @XStreamAsAttribute
        Name name;
        String url;
    }
    

    在我当前的配置中(类是别名,属性是自动检测的,所有这些),XStream正确地反序列化 Name Role 上的属性 < 元素,而不是实际的链接文本本身。

    如何让XStream将该文本反序列化为 String a中的字段 Link


    *e、 g.更换

    <Link role="self">
        https://long/url/here
    </Link>
    

    具有

    <Link role="self">
        <url>https://long/url/here</url>
    </Link>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   skaffman    15 年前