1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| public void createXml(File xmlDestFile, CategoryBean category) {
try {// création d'une fabrique de documents
DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
// création d'un constructeur de documents
DocumentBuilder constructeur;
constructeur = fabrique.newDocumentBuilder();
constructeur.setErrorHandler(new ErrorsManager());
// lecture d'un document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document doc = parser.newDocument();
// category = new XmlParser().parse(new File("F:\\Android\\test_parsing.xml"));
Element root = browseCategory(category, doc, doc.getDocumentElement());
doc.appendChild(root);
File file = XmlParser.createXml(doc, xmlDestFile, "utf-8");
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private Element browseCategory(CategoryBean category, Document doc, Element parentCategoryElement) {
Element categoryNode = null;
// On vérifie si on est à la racine
if (category.getParentCategory() == null) {
parentCategoryElement = doc.createElement(MAIN_CATEGORY);
parentCategoryElement.setAttribute(ACTIVATED, String.valueOf(category.getActivated()));
parentCategoryElement.setAttribute(STATE, String.valueOf(category.getState()));
parentCategoryElement.setAttribute(TITLE, category.getTitle());
categoryNode = parentCategoryElement;
} else {
if (category instanceof QuestionsBean) {
categoryNode = doc.createElement(QUESTIONS);
if (((QuestionsBean) category).getComment() != null) {
categoryNode.setAttribute(COMMENT, ((QuestionsBean) category).getComment());
}
} else {
categoryNode = doc.createElement(CATEGORY);
}
categoryNode.setAttribute(ACTIVATED, String.valueOf(category.getActivated()));
categoryNode.setAttribute(STATE, String.valueOf(category.getState()));
categoryNode.setAttribute(TITLE, category.getTitle());
parentCategoryElement.appendChild(categoryNode);
}
if (category.getCategoryList() != null && !category.getCategoryList().isEmpty()) {
for (CategoryBean childCategory : category.getCategoryList()) {
if (category.getParentCategory() != null) {
Element categoryElement = browseCategory(childCategory, doc, categoryNode);
parentCategoryElement.appendChild(categoryElement);
} else {
parentCategoryElement = browseCategory(childCategory, doc, categoryNode);
}
}
}
if (category instanceof QuestionsBean && ((QuestionsBean) category).getQuestionList() != null
&& !((QuestionsBean) category).getQuestionList().isEmpty()) {
Element questionsElement = browseQuestions((QuestionsBean) category, doc, categoryNode);
// Element questionsElement = doc.createElement(QUESTION);
parentCategoryElement.appendChild(questionsElement);
}
return parentCategoryElement;
}
private Element browseQuestions(QuestionsBean questions, Document doc, Element categoryElement) {
for (QuestionBean question : questions.getQuestionList()) {
Element questionElement = doc.createElement(QUESTION);
if (question.getComment() != null) {
questionElement.setAttribute(COMMENT, question.getComment());
}
questionElement.setAttribute(HAS_INT_VALUE, question.hasIntValue() ? "1" : "0");
if (question.hasIntValue()) {
questionElement.setAttribute(INT_VALUE, String.valueOf(question.getIntValue()));
}
questionElement.setAttribute(VALUE, String.valueOf(question.getValue()));
questionElement.setAttribute(PRIORITY, String.valueOf(question.getPriority()));
questionElement.setAttribute(ACTIVATED, String.valueOf(question.getActivated()));
questionElement.appendChild(doc.createTextNode(question.getQuestion()));
categoryElement.appendChild(questionElement);
break;
}
return categoryElement;
}
public static File createXml(Document document, File xmlDestFile, String encodage) throws Exception {
FileOutputStream fos = null;
Writer writer = null;
try {
fos = new FileOutputStream(xmlDestFile);
writer = new OutputStreamWriter(fos, encodage);
emitDocument(document, writer, encodage);
} catch (Exception e) {
throw e;
} finally {
if (writer != null) {
writer.flush();
writer.close();
}
if (fos != null) {
fos.flush();
fos.close();
}
}
return xmlDestFile;
}
private static final void emitDocument(Document doc, Writer writer, String encoding) throws IOException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = null;
try {
t = tf.newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.setOutputProperty(OutputKeys.ENCODING, encoding);
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
} catch (TransformerConfigurationException tce) {
assert (false);
}
DOMSource doms = new DOMSource(doc);
StreamResult sr = new StreamResult(writer);
try {
t.transform(doms, sr);
} catch (TransformerException te) {
IOException ioe = new IOException();
ioe.initCause(te);
throw ioe;
}
} |
Partager