Assign DTD to XML Document
-
Hi Gurus! I'm a XML beginner. I try to save my program settings in XML file using Microsoft Visual C++ 6.0. Here is the sample from my code: -------------------------------------------------------------- IXMLDOMDocumentPtr XmlDocPtr; IXMLDOMNodePtr pNode; IXMLDOMElementPtr XmlRootPtr; b=XmlDocPtr->loadXML(_bstr_t("")); if (!b) return; XmlDocPtr->get_documentElement(&XmlRootPtr); if (XmlRootPtr) { VARIANT vtTemp2; vtTemp2.vt=VT_I2; vtTemp2.iVal = NODE_ELEMENT; pNode = XmlDocPtr->createNode(vtTemp2, "Program", ""); pNode->put_dataType(L"NODE_TEXT"); pNode->put_text(L"MYCOOLER"); XmlRootPtr->appendChild(pNode); pNode = XmlDocPtr->createNode(vtTemp2, "Distributor", ""); pNode->put_dataType(L"NODE_TEXT"); pNode->put_text(L"IBM Corp."); XmlRootPtr->appendChild(pNode); pNode = XmlDocPtr->createNode(vtTemp2, "Version", ""); pNode->put_dataType(L"NODE_TEXT"); pNode->put_text(L"6.0"); XmlRootPtr->appendChild(pNode); }; variant_t vDest(L"d:\\settings.xml"); XmlDocPtr->save(vDest); -------------------------------------------------------------- File d:\system.dtd contains: -------------------------------------------------------------- -------------------------------------------------------------- But resulted file "settings.xml" is not formatted according system.dtd specification. What's wrong? Help me! Yours sincerely, Alex Bash
-
Hi Gurus! I'm a XML beginner. I try to save my program settings in XML file using Microsoft Visual C++ 6.0. Here is the sample from my code: -------------------------------------------------------------- IXMLDOMDocumentPtr XmlDocPtr; IXMLDOMNodePtr pNode; IXMLDOMElementPtr XmlRootPtr; b=XmlDocPtr->loadXML(_bstr_t("")); if (!b) return; XmlDocPtr->get_documentElement(&XmlRootPtr); if (XmlRootPtr) { VARIANT vtTemp2; vtTemp2.vt=VT_I2; vtTemp2.iVal = NODE_ELEMENT; pNode = XmlDocPtr->createNode(vtTemp2, "Program", ""); pNode->put_dataType(L"NODE_TEXT"); pNode->put_text(L"MYCOOLER"); XmlRootPtr->appendChild(pNode); pNode = XmlDocPtr->createNode(vtTemp2, "Distributor", ""); pNode->put_dataType(L"NODE_TEXT"); pNode->put_text(L"IBM Corp."); XmlRootPtr->appendChild(pNode); pNode = XmlDocPtr->createNode(vtTemp2, "Version", ""); pNode->put_dataType(L"NODE_TEXT"); pNode->put_text(L"6.0"); XmlRootPtr->appendChild(pNode); }; variant_t vDest(L"d:\\settings.xml"); XmlDocPtr->save(vDest); -------------------------------------------------------------- File d:\system.dtd contains: -------------------------------------------------------------- -------------------------------------------------------------- But resulted file "settings.xml" is not formatted according system.dtd specification. What's wrong? Help me! Yours sincerely, Alex Bash
Bash wrote: But resulted file "settings.xml" is not formatted according system.dtd specification. The DTD is not a formatter. A DTD is just here to verify the well-formedness and/or validness of an Xml stream. The formatting is the result of your own code (appendChild, ...). On top of that, when you open an Xml file in MSIE, MSIE applies a default formatting (using a XSLT stylesheet) : for instance it may show you <k name="kkk"/> although there is <k name="kkk"></k> in the file.
Back to real work : D-27.
-
Bash wrote: But resulted file "settings.xml" is not formatted according system.dtd specification. The DTD is not a formatter. A DTD is just here to verify the well-formedness and/or validness of an Xml stream. The formatting is the result of your own code (appendChild, ...). On top of that, when you open an Xml file in MSIE, MSIE applies a default formatting (using a XSLT stylesheet) : for instance it may show you <k name="kkk"/> although there is <k name="kkk"></k> in the file.
Back to real work : D-27.
Hi, Thank you for quick reply to the beginner. ;) And another question - how to insert CR/LF symbol after appending a new child. At present, I have "settings.xml" with single line like as: MYCOOLERIBM Corp.6.0 I want to get this xml (i.e. "formatted") as follows: MYCOOLER IBM Corp. 6.0 How to do "formatted" xml? Yours sincerely, Alex Bash
-
Hi, Thank you for quick reply to the beginner. ;) And another question - how to insert CR/LF symbol after appending a new child. At present, I have "settings.xml" with single line like as: MYCOOLERIBM Corp.6.0 I want to get this xml (i.e. "formatted") as follows: MYCOOLER IBM Corp. 6.0 How to do "formatted" xml? Yours sincerely, Alex Bash
Unfortunately, there is no way to do so using MSXML. That's probably why this SDK is now legacy and replaced by a much more powerful Xml stuff along with .NET. You can achieve indentation by creating your own fake text nodes, with code like this :
var dom = new ActiveXObject("Microsoft.XMLDOM");
var root = dom.createElement("toto");
for (i = 0; i < 10; i++) {
var formatting = dom.createTextNode("\n\t");
root.appendChild(formatting);
var child = dom.createElement(child);
root.appendChild(child);
}
dom.save("output.xml");
Back to real work : D-27.
-
Unfortunately, there is no way to do so using MSXML. That's probably why this SDK is now legacy and replaced by a much more powerful Xml stuff along with .NET. You can achieve indentation by creating your own fake text nodes, with code like this :
var dom = new ActiveXObject("Microsoft.XMLDOM");
var root = dom.createElement("toto");
for (i = 0; i < 10; i++) {
var formatting = dom.createTextNode("\n\t");
root.appendChild(formatting);
var child = dom.createElement(child);
root.appendChild(child);
}
dom.save("output.xml");
Back to real work : D-27.