1

First of all I am new to Jaxb and most defiantly have not wrapped my head completely around it.

I would like to use annotation to pares Xml but instead of generating binding classes I would just like to extract a portion of the xml and return it as a Java.lang.String. No marshalling or unmarshalling need. Just want to extract from xml.

Let's say my XML is structured as followed

 <root>
   <topMovies>
     <string>The Godfather (1972)</string>
     <string>The Shawshank Redemption (1994)</string>
     <string>The Godfather: Part II (1974)</string>
     <string>The Lord of the Rings: The Return of the King (2003)</string>
  <topMovies>
 <root>

And I would like to extract the above XML snippet (topMovies).

This won't work of course but might give an idea of what I am trying to say.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "root")
public class GetTopMovies {
@XmlElement(name = "topMovies")
    protected java.lang.String getTopMovies;
}

In the end I want a java String with the extracted xml (not taking escaping into account).

String topMovies =
 "<topMovies>
   <string>The Godfather (1972)</string>
   <string>The Shawshank Redemption (1994)</string>
   <string>The Godfather: Part II (1974)</string>
   <string>The Lord of the Rings: The Return of the King (2003)</string>
   <topMovies>
 ";

Can this be done with JAXBElement or and XmlAdapter or creating a special Object Factory?

RobS
  • 11
  • 2
  • possible duplicate of [JAXB take all content of element (both tags and text)](http://stackoverflow.com/questions/7736678/jaxb-take-all-content-of-element-both-tags-and-text) – bdoughan Jan 14 '12 at 20:21

1 Answers1

0

You can leverage the @XmlAnyElement annotation with a DOMHandler to accomplish this use case:

Below are links to answers I gave, to similar questions that contain additional details:

For More Information

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400