22 #include <boost/foreach.hpp>
40 QByteArray byte_array;
41 QXmlStreamWriter xmlStream(&byte_array);
42 QDomDocument docToSave;
43 QDomElement elementToSave;
44 QDomElement rootElement;
50 if (nodeToSave.isDocument())
52 docToSave = nodeToSave.toDocument();
56 docToSave.importNode(nodeToSave,
true);
59 xmlStream.setAutoFormatting(
true);
60 xmlStream.setAutoFormattingIndent(1);
62 xmlStream.writeStartDocument(
"1.0",
false);
63 xmlStream.writeDTD(
"<!DOCTYPE Tympan SYSTEM 'Tympan.dtd'>");
66 rootElement = docToSave.documentElement();
68 ret = writeElementToStream(rootElement, xmlStream);
70 xmlStream.writeEndDocument();
76 if (!file.open(QIODevice::WriteOnly))
80 file.write(byte_array);
96 QString tagName = pElement.tagName();
97 pStream.writeStartElement(tagName);
99 QVector<QString> attribute_names;
100 const QDomNamedNodeMap attributes = pElement.attributes();
101 for (
int i = 0; i < attributes.length(); ++i)
103 attribute_names << attributes.item(i).toAttr().name();
106 std::stable_sort(attribute_names.begin(), attribute_names.end());
108 BOOST_FOREACH (QString attr_name, attribute_names)
110 const QDomAttr attr = attributes.namedItem(attr_name).toAttr();
111 QString attrName = attr.name();
112 QString attrValue = attr.value();
113 pStream.writeAttribute(attrName, attrValue);
117 const QDomNodeList nodeList = pElement.childNodes();
118 int nodecount = nodeList.length();
120 for (
int i = 0; i < nodecount; i++)
122 QDomNode nodeTosave = nodeList.item(i);
123 if (nodeTosave.isElement())
125 QDomElement elementToSave = nodeTosave.toElement();
126 ret = ret && writeElementToStream(elementToSave, pStream);
128 else if (nodeTosave.nodeType() == QDomNode::TextNode)
130 QDomText textToWrite = nodeTosave.toText();
131 pStream.writeCharacters(textToWrite.data());
136 nodeTosave.nodeName(), nodeTosave.nodeType());
140 pStream.writeEndElement();
146 QString outputString;
147 QTextStream textSream(&outputString);
151 textSream <<
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>\n";
156 if (nodeToSave.isDocument())
158 textSream << nodeToSave.toDocument().toString();
163 doc.importNode(nodeToSave,
true);
164 textSream << doc.toString();
static QString saveNodeToString(DOM_Node &nodeToSave)
Methode pour la sauvegarde d'un noeud DOM dans une string XML.
static bool writeElementToStream(const QDomElement &pElement, QXmlStreamWriter &pStream)
Methode qui ecrit recursivement les noeuds d'un document xml dans un flux de type QXmlStreamWriter.
static int saveNodeToFile(DOM_Node &nodeToSave, const char *fileName)
La methode principale pour la sauvegarde d'un noeud DOM dans un fichier XML.
static OMessageManager * get()
virtual void info(const char *message,...)