все работает, однако валюшку не устраивает... вопрос на ярд: что не так?
package com.javarush.task.task33.task3309;
import org.w3c.dom.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
/*
Комментарий внутри xml
*/
public class Solution {
public static String toXmlWithComment(Object obj, String tagName, String comment) throws Exception {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = context.createMarshaller();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
marshaller.marshal(obj, baos);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(bais);
NodeList nodes = document.getElementsByTagName(tagName);
if (nodes.getLength() > 0) {
process(nodes, tagName, comment);
}
else{Node node = document.getDocumentElement();
if (node.hasChildNodes()){process(node.getChildNodes(), null, null);}}
return transformerPrint(document);
}
public static void process(NodeList nodeList, String tagName, String comment) {
for (int i = 0; i < nodeList.getLength(); i++) {
if (nodeList.item(i).getNodeType() == Node.TEXT_NODE){
if (nodeList.item(i).getTextContent().matches((".*[<>&'\"].*"))){
Node current = nodeList.item(i);
String content = current.getTextContent();
CDATASection section = nodeList.item(i).getOwnerDocument().createCDATASection(content);
Node parent = current.getParentNode();
parent.insertBefore(section, current);
parent.removeChild(current);
}
continue;
}
Node currentNode = nodeList.item(i);
if (currentNode.getNodeType() == Node.ELEMENT_NODE && currentNode.getNodeName().equals(tagName)) {
nodeList.item(i).getParentNode().insertBefore(nodeList.item(i).getOwnerDocument().createComment(comment), nodeList.item(i));
}
if (nodeList.item(i).hasChildNodes()&&nodeList.item(i).getNodeType() != Node.TEXT_NODE){
NodeList childList = nodeList.item(i).getChildNodes();
process(childList, tagName, comment);
}
}
}
public static final String transformerPrint(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
System.out.println(out.toString());
return out.toString();
}
public static void main(String[] args) throws Exception {
Store store = new Store();
Book book1 = new Book();
Book book2 = new Book();
Book book3 = new Book();
book1.name = "Java";
book1.id = "1";
book1.description = "about Java<>";
book2.name = "Script";
book2.id = "2";
book2.description = "about Script";
book3.name = "Fuuuuc";
book3.id = "3";
book3.description = "about FUUUC";
store.books = new Book[3];
store.books[0] = book1;
store.books[1] = book2;
store.books[2] = book3;
toXmlWithComment(store, "casual", "myComment!!");
}
}