Parsing xml with google-http-client-xml : parsing xml element content as well as attributes using XmlObjectParser

462 views Asked by At

I cannot seem to parse the value out of the the following xml. What would be the parser class for parsing xml of type if I was using google-http-java client ? Sample xml and parsers below

<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; 
}

To be clear I want the start attribute value, end attribute value and the text content. What seems to work are the following

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);

But I cannot get the content value.

If I change the feed class to the following

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

I can only get the content and not the attribute values !

Any sample source code is very difficult to find (on github, stackoverflow etc.)

1

There are 1 answers

0
raj On

Ok here is the answer after combing through 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; 
}