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();
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();
128 else if (nodeTosave.nodeType() == QDomNode::TextNode)
130 QDomText textToWrite = nodeTosave.toText();
131 pStream.writeCharacters(textToWrite.data());
135 std::string nodeName = nodeTosave.nodeName().toStdString();
137 nodeName.c_str(), nodeTosave.nodeType());
141 pStream.writeEndElement();
147 QString outputString;
148 QTextStream textSream(&outputString);
152 textSream <<
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>\n";
157 if (nodeToSave.isDocument())
159 textSream << nodeToSave.toDocument().toString();
164 doc.importNode(nodeToSave,
true);
165 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,...)