Skip to content

Instantly share code, notes, and snippets.

@Shtaba09
Created September 30, 2019 21:16
Show Gist options
  • Save Shtaba09/c39933b7dc7e52da6da3f4dd68897b35 to your computer and use it in GitHub Desktop.
Save Shtaba09/c39933b7dc7e52da6da3f4dd68897b35 to your computer and use it in GitHub Desktop.
JAXB сериализация с СDATA и Коментами
package com.javarush.task.task33.task3309;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.io.StringWriter;
/*
Комментарий внутри xml
*/
public class Solution {
public static String toXmlWithComment(Object obj, String tagName, String comment) throws JAXBException, ParserConfigurationException, IOException, SAXException, TransformerException {
// creating document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setCoalescing(true); // convert exist CDATA to text
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
// marsh object to document
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(obj, document);
// searching tagName
// creating NodeList
NodeList nodes = document.getDocumentElement().getElementsByTagName("*");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
// insert comments
if (node.getNodeName().equalsIgnoreCase(tagName)) {
Comment com = document.createComment(comment);
node.getParentNode().insertBefore(com, node);
}
//replace CDATA
if (node.getTextContent().matches(".*[<>&\'\"].*")) {
String text = node.getTextContent();
Node cdata = document.createCDATASection(text);
node.setTextContent(null);
node.appendChild(cdata);
}
}
//transform doc to xml to writer
StringWriter writer = new StringWriter();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
transformer.transform(new DOMSource(document), new StreamResult(writer));
String result = writer.toString();
return result;
}
public static void main(String[] args) throws JAXBException, ParserConfigurationException, IOException, SAXException, TransformerException {
First first = new First();
System.out.println(toXmlWithComment(first, "second", ""));
}
@XmlRootElement(name = "first")
public static class First {
@XmlElement(name = "second")
public String item1 = "has comment";
@XmlElement(name = "second")
public String item2 = "has comment + CDATA because of <second>";
@XmlElement(name = "second")
public String item3 = "has comment";
@XmlElement(name = "q")
public String item4 = "no comment";
@XmlElement(name = "q")
public Second[] item5 = new Second[]{new Second()};
@XmlElement(name = "q")
public String item6 = "no comment + CDATA because of \"";
}
// @XmlRootElement(name = "first")
// public static class First {
// @XmlElement(name = "q")
// public String item1 = "has comment";
// @XmlElement(name = "q")
// public String item2 = "has comment + CDATA because of <second>";
// @XmlElement(name = "q")
// public String item3 = "has comment";
// @XmlElement(name = "q")
// public String item4 = "no comment";
// @XmlElement(name = "q")
// public Second[] item5 = new Second[]{new Second()};
// @XmlElement(name = "q")
// public String item6 = "no comment + CDATA because of \"";
// }
public static class Second {
@XmlElement(name = "q")
public String item1 = "has comment";
@XmlElement(name = "q")
public String item2 = "has comment and need CDATA because of <second>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment