I saw a similar question being posted here, yet it did not help me solve the problem so I am posting my question here to see if someone can modify my code to make it work.
Question: How to access mixed content String value and save it in setPhrase(String value) method?
After using JAXB unmarshall and marshall I am able to get everything converted into and object and saved accorderling, except for the actual phrase "Hello World.". I know I must use some sort of @XmlMixed for this complex element but I cannot figure it out.
Thanks in advance to any help I may get to access this value and save it in setPhrase(String value) method.
解决方案
I'll try to answer your question with an example:
input.xml
We will use the following XML document for this example. The root element has mixed content. Having mixed conent means that text nodes can appear mixed in with the elements. Since more than one text node can appear a unary property isn't a good fit.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<root/>
Hello
<root/>
World
<root/>
</root>
Demo
The following code will be used in to read in the XML to object form and then write it back to XML.
package forum10940267;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum10940267/input.xml");
Root root = (Root) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
USE CASE #1 - One List to Hold Mixed Content
@XmlMixed is most often used to with another annotation, so that the resulting List contains both element and text content. One advantage of this is that order is maintained so that the document can be round tripped.
package forum10940267;
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Root {
private List<Object> mixedContent = new ArrayList<Object>();
@XmlElementRef(name="root", type=Root.class)
@XmlMixed
public List<Object> getMixedContent() {
return mixedContent;
}
public void setMixedContent(List<Object> mixedContent) {
this.mixedContent = mixedContent;
}
}
Since text nodes can occur multiple times in mixed content, a non-List property isn't a good fit and it appears as though the @XmlMixed annotation is being ignored.
package forum10940267;
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Root {
private List<Object> mixedContent = new ArrayList<Object>();
private String text;
@XmlElementRef(name="root", type=Root.class)
public List<Object> getMixedContent() {
return mixedContent;
}
public void setMixedContent(List<Object> mixedContent) {
this.mixedContent = mixedContent;
}
@XmlMixed
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}