【问题标题】:Parsing xml with google-http-client-xml : parsing xml element content as well as attributes using XmlObjectParser使用 google-http-client-xml 解析 xml:使用 XmlObjectParser 解析 xml 元素内容和属性
【发布时间】:2017-08-14 07:29:05
【问题描述】:

我似乎无法解析以下 xml 中的值。如果我使用 google-http-java 客户端,解析 xml 类型的解析器类是什么?下面的示例 xml 和解析器

<feed>
  <text start="1" end="10"> Text 1 </text> 
  <text start="2" end="20"> Text 2 </text> 
  <text start="3" end="30"> Text 3 </text> 
  <text start="4" end="40"> Text 4 </text> 
  <text start="5" end="50"> Text 5 </text> 
</feed>

Class Feed {
 @Key("text")
 public List<Text> textList;
}

Class Text {
  @Key("@start")
  public String startTime;


  @Key("@end")
  public String endTime; 
}

要清楚,我想要开始属性值、结束属性值和文本内容。似乎有效的是以下

HttpRequestFactory httpRequestFactory =
        HTTP_TRANSPORT.createRequestFactory(
            new HttpRequestInitializer() {
              @Override
              public void initialize(HttpRequest request) throws IOException {
                request.setParser(new XmlObjectParser(new XmlNamespaceDictionary().set("", "")));
              }
            });    
Feed feedObject = httpResponse.parseAs(Feed.class);

但我无法获取内容值。

如果我将提要类更改为以下

Class Feed {
     @Key("text")
     public List<String> textList;
}

我只能获取内容,不能获取属性值!

任何示例源代码都很难找到(在 github、stackoverflow 等上)

【问题讨论】:

    标签: java xml xml-parsing google-api-java-client google-http-client


    【解决方案1】:

    好的,这里是通过github梳理后的答案!!!

    <feed>
      <text start="1" end="10"> Text 1 </text> 
      <text start="2" end="20"> Text 2 </text> 
      <text start="3" end="30"> Text 3 </text> 
      <text start="4" end="40"> Text 4 </text> 
      <text start="5" end="50"> Text 5 </text> 
    </feed>
    
    Class Feed {
     @Key("text")
     public List<Text> textList;
    }
    
    Class Text {
      @Key("@start")
      public String startTime;
    
      @Key("@end")
      public String endTime; 
    
      @Key("text()")
      public String payload; 
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-07
      • 1970-01-01
      • 2013-09-10
      • 2021-09-16
      • 2021-09-19
      • 2011-09-25
      相关资源
      最近更新 更多