使用JAXB Marshaller转义字符(例如引号)

时间:2017-09-13 09:06:40

标签: java xml jaxb

我需要使用XmlAdapter,因为我想要没有空的XLM对象:

<TACHES>
    <TACHE>
        <NB_HEURE>7.75</NB_HEURE>
        <NOTES>foo</NOTES>
        <CODE_TACHE>1234</CODE_TACHE>
    </TACHE>
    <TACHE>                 <- I Want delete this part
        <NB_HEURE/>         <- I Want delete this part
        <NOTES/>            <- I Want delete this part 
        <CODE_TACHE/>       <- I Want delete this part 
    </TACHE>                <- I Want delete this part 
</TACHES>

我的POJO是:

@XmlRootElement(name = "TACHES")
public class Tasks {
    private List<Task> tasks;
    @XmlJavaTypeAdapter(TaskAdapter.class)
    @XmlElement(name = "TACHE")
    public List<Task> getTasks() {
        return tasks;
    }
    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }
}

我的XmlAdapter是:

public class TaskAdapter extends XmlAdapter<String, Task> {
    @Override
    public Task unmarshal(String v) throws Exception {
        return null;
    }
    @Override
    public String marshal(Task t) throws Exception {
        JAXBContext context = JAXBContext.newInstance(Task.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, true);
        m.setProperty("jaxb.encoding", "Unicode");
        StringWriter sw = new StringWriter();
        if ("".equals(t.getNotes()) && "".equals(t.getHourCount()) && "".equals(t.getTaskCode())) {
            return null;
        }
        m.marshal(t, sw);
        String s = sw.toString();
        s = s.substring(s.indexOf("<TACHE>") + 7, s.indexOf("</TACHE>"));
        System.out.println(s);
        return s;
    }
}

XmlAdapter打印的System.out.println:

<NB_HEURE>7.75</NB_HEURE>
<NOTES>foo</NOTES>
<CODE_TACHE>1234</CODE_TACHE>

但是,当我整理全球POJO时,我有:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
...
<TACHES>
    <TACHE>
        &lt;NB_HEURE&gt;7.75&lt;/NB_HEURE&gt;
        &lt;NOTES&gt;foo&lt;/NOTES&gt;
        &lt;CODE_TACHE&gt;1234&lt;/CODE_TACHE&gt;
    </TACHE>
</TACHES>
...

我的意思是:

JAXBContext context = JAXBContext.newInstance(MoReports.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter sw = new StringWriter();
m.marshal(moReports, sw);
xmlString = sw.toString();

1 个答案:

答案 0 :(得分:0)

第1步

我没有使用&#39; TaskAdapter扩展了XmlAdapter&#39;

第2步

我将[EnableCors(origins: "http://www.contoso.com,http://www.example.com", headers: "*", methods: "*")] 设置为JAXB_FORMATTED_OUTPUT

FALSE

第3步

我将此代码添加到marshal

m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);

第4步

我将此代码添加到marshal

xmlString = removeAllEmptyXmlTags(xmlString);

public String removeAllEmptyXmlTags(String xml) {
    final String regex1 = "<([a-zA-Z0-9-\\_]*)[^>]*/>";
    final String regex2 = "<([a-zA-Z0-9-\\_]*)[^>]*>\\s*</\\1>";
    final Pattern pattern1 = Pattern.compile(regex1);
    final Pattern pattern2 = Pattern.compile(regex2);

    String xmlString = xml;

    Matcher matcher1;
    Matcher matcher2;
    do {
        xmlString = xmlString.replaceAll(regex1, "").replaceAll(regex2, "");
        matcher1 = pattern1.matcher(xmlString);
        matcher2 = pattern2.matcher(xmlString);
    } while (matcher1.find() || matcher2.find());

    return xmlString;
}

结果还可以:

xmlString = xmlIndent(xmlString, false);

public String xmlIndent(String xml, Boolean ommitXmlDeclaration) {
    Writer outxml = new StringWriter();
    try {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(xml)));
        OutputFormat format = new OutputFormat(doc);
        format.setIndenting(true);
        format.setIndent(2);
        format.setOmitXMLDeclaration(ommitXmlDeclaration);
        format.setLineWidth(Integer.MAX_VALUE);
        XMLSerializer serializer = new XMLSerializer(outxml, format);
        serializer.serialize(doc);
    } catch (ParserConfigurationException | SAXException | IOException e) {
        return xml;
    }
    return outxml.toString();
}