Результат выполнения
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<first>
<!--it's a comment-->
<second>some string</second>
<!--it's a comment-->
<second>![CDATA[need CDATA because of <second>]]</second>
<!--it's a comment-->
<second/>
<forth>
<!--it's a comment-->
<second>some string</second>
<!--it's a comment-->
<second>![CDATA[need CDATA because of <second>]]</second>
</forth>
<fifth>![CDATA[need CDATA because of "]]</fifth>
</first>
Process finished with exit code 0package 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 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.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
/*
Комментарий внутри xml
*/
public class Solution {
public static String toXmlWithComment(Object obj, String tagName, String comment) {
String res = null;
try {
// Create the JAXBContext
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
// Create the Document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
// Marshal the Object to a Document
Marshaller marshaller = jc.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(obj, document);
NodeList nodeList = document.getElementsByTagName("*");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equals(tagName)) {
Comment newComment = document.createComment(comment);
node.getParentNode().insertBefore(newComment, node);
}
Node textNode = node.getFirstChild();
if (textNode.getNodeType() == Node.TEXT_NODE) {
String content = textNode.getTextContent();
if (content.matches(".*[<>&'\"].*")) {
content = "![CDATA[" + content + "]]";
textNode.setTextContent(content);
}
}
}
}
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
StringWriter result = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(result));
String st1 = result.toString();
st1 = st1.replace(">", ">");
st1 = st1.replace("<", "<");
st1 = st1.replace("&", "&");
st1 = st1.replace(""", "\"");
st1 = st1.replace("'", "'");
return st1;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String st = toXmlWithComment(new First(), "second", "it's a comment");
System.out.println(st);
}
@XmlRootElement(name = "first")
public static class First {
@XmlElement(name = "second")
public String item1 = "some string";
@XmlElement(name = "second")
public String item2 = "need CDATA because of <second>";
@XmlElement(name = "second")
public String item3 = "";
@XmlElement(name = "third")
public String item4;
@XmlElement(name = "forth")
public Second[] third = new Second[]{new Second()};
@XmlElement(name = "fifth")
public String item5 = "need CDATA because of \"";
}
public static class Second {
@XmlElement(name = "second")
public String item1 = "some string";
@XmlElement(name = "second")
public String item2 = "need CDATA because of <second>";
}
}