不幸的是,这是不可能的
jackson-dataformat-xml
但是,使用JAXB可以通过使用
DomHandler
@XmlRootElement(name = "content")
@XmlAccessorType(XmlAccessType.FIELD)
public class Content {
@XmlAnyElement(InnerXmlHandler.class)
private String p;
}
域处理程序
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.annotation.DomHandler;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
public class InnerXmlHandler implements DomHandler {
private static final String START_TAG = "
";
private static final String END_TAG = "
";private StringWriter xmlWriter = new StringWriter();
public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
return new StreamResult(xmlWriter);
}
public String getElement(StreamResult rt) {
String xml = rt.getWriter().toString();
int beginIndex = xml.indexOf(START_TAG) + START_TAG.length();
int endIndex = xml.lastIndexOf(END_TAG);
return xml.substring(beginIndex, endIndex);
}
public Source marshal(String n, ValidationEventHandler errorHandler) {
try {
String xml = START_TAG + n.trim() + END_TAG;
StringReader xmlReader = new StringReader(xml);
return new StreamSource(xmlReader);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
标签如下:
This is some
- list
nested paragraph
但是,这只在内部HTML/XML有效时才起作用。以下操作将不起作用,并引发如下异常
The element type "ul" must be terminated by the matching end-tag ""
.
This is some
- invalid xml
这是因为尽管提供了dom处理程序,JAXBs内部仍然遍历所有内部元素。