强制jaxb unmarshaller忽略html标签

时间:2014-09-12 15:06:37

标签: java xml parsing jaxb unmarshalling

我正在使用JAXB将字符串xml数据转换为POJO,如下所示。

JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(temp);
Employee emp = (Employee) unmarshaller.unmarshal(reader); 

一切正常,但它总是试图在unmarshal期间验证每个元素的文本,有时会失败。我不想要,因为在文本中有很多html标签,有时它们也是错误的。

所以,我希望JAXB跳过整个文本并将其原样传递给POJO数据。有没有办法实现这一目标。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

以下是一个示例,您需要使用@XmlAnyElement来获取内容,而不使用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]

希望它有所帮助。