1

Is there a way to parse part of an XML message into a String keeping it in XML format?

Example...

<person>
  <name>John Doe</name>
  <age>24</age>

  <address>
    <street>bailey</street>
    <city>boston</city>
  </address>
</person>

public class Person {

  @XmlElement
  private NameClass name;

  @XmlElement
  private int age;

  @XmlElement
  private String address; 

  ...
}

public class NameClass {
  ...
}

Where I want the entire address element to be kept in XML format and parsed as a String.

  • Duplicate of [JAXB take all content of element (both tags and text)](http://stackoverflow.com/questions/7736678), [How to read and write XML elements “as is” with JAXB](http://stackoverflow.com/questions/8298433), [Getting a XML data present in a child node as string using JAXB](http://stackoverflow.com/questions/5930406), [Using JAXB to extract inner text of XML element](http://stackoverflow.com/questions/5537416), [JAXB mapping elements with “unknown” name](http://stackoverflow.com/questions/4278546). Respect for **@BlaiseDoughan**. – dma_k Jan 28 '12 at 13:04
  • 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 31 '12 at 17:30

1 Answers1

2

You can use @XmlAnyElement with a DomHandler:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • I'm getting errors trying to marshall the object. [com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation] – silvino.barreiros Jan 31 '12 at 16:39
  • @silvino.barreiros - As I mentioned in the blog post the JAXB RI appears to have a bug here. This use case will work with EclipseLink JAXB (MOXy). – bdoughan Jan 31 '12 at 16:46