问题描述
我正在使用JAXB将字符串xml数据转换为POJO,如下所示。
I am using JAXB to convert string xml data to POJO as follows.
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(temp);
Employee emp = (Employee) unmarshaller.unmarshal(reader);
一切正常,但它总是试图在unmarshal期间验证每个元素的文本,有时会失败。我不想要,因为在文本中有很多html标签,有时它们也是错误的。
It goes fine, but it's always trying to validate text of each element during unmarshal and sometimes got failed. That I don't want, because in text there are lot of html tags and sometimes they are erroneous too.
因此,我希望JAXB跳过整个文本并将其原样传递给POJO数据。有没有办法实现这一目标。任何帮助将不胜感激。
So, I want JAXB to skip the entire text and pass it as it is to form POJO data. Is there any way to achieve this. Any help will be appreciated.
推荐答案
这是一个例子,您需要使用@XmlAnyElement来获取内容,因为它没有使用 CDATA
。
Here is an example, you need to use @XmlAnyElement to get the content as it is without using CDATA
.
Employee.java:
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee {
private long id;
private String name;
private int age;
public long getId() {
return id;
}
@XmlAttribute
public void setId(long id) {
this.id = id;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public String getName() {
return name;
}
@XmlAnyElement(NameHandler.class)
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
}
NameHandler.java:
import java.io.StringReader;
import java.io.StringWriter;
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;
public class NameHandler implements DomHandler<String, StreamResult> {
private static final String NAME_START_TAG = "<name>";
private static final String NAME_END_TAG = "</name>";
private StringWriter xmlWriter = new StringWriter();
@Override
public StreamResult createUnmarshaller(ValidationEventHandler errorHandler) {
return new StreamResult(xmlWriter);
}
@Override
public String getElement(StreamResult rt) {
String xml = rt.getWriter().toString();
int beginIndex = xml.indexOf(NAME_START_TAG) + NAME_START_TAG.length();
int endIndex = xml.indexOf(NAME_END_TAG);
return xml.substring(beginIndex, endIndex);
}
@Override
public Source marshal(String n, ValidationEventHandler errorHandler) {
try {
String xml = NAME_START_TAG + n.trim() + NAME_END_TAG;
StringReader xmlReader = new StringReader(xml);
return new StreamSource(xmlReader);
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}
JAXB:
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class JAXBExample {
public static void main(String[] args) {
try {
String temp ="<employee id=\"1001\"><age>25</age><name>myemp<p>content inside tags</p></name></employee>";
JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
StringReader reader = new StringReader(temp);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Employee emp = (Employee) jaxbUnmarshaller.unmarshal(reader);
System.out.println(emp);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
打印内容原样:
Employee [id=1001, name=myemp<p>content inside tags</p>, age=25]
希望有所帮助。
这篇关于强制jaxb unmarshaller忽略html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!