Preserving white space in attribute values. (using MSXML)
-
Howdy' In one of our data XML files, we need to have attribute values preserve white space at the end. For example :
<!-- without space -->
<MyTag MyAttribute="a value" /><!-- with space -->
<MyTag MyAttribute="a value " />Are two different attribute values. I tried using the preserve white space attribute
pXMLDoc->preserveWhiteSpace = VARIANT_TRUE;
but that completely craps out the parsing of the file, for example, sometimes (well, always) I get the wrong number of items in a node list (
IXMLDOMNodeListPtr
) for example, in the following snippet, when preserveWhiteSpace is FALSE, the list count is 2 and when preserveWhiteSpace is TRUE, the list count if 5 (!)<MESSAGE_LIST>
<MESSAGE Id="10" Type="Info">
<FRENCH>Message 10</FRENCH>
</MESSAGE>
<MESSAGE Id="11" Type="Info">
<FRENCH>Message 11</FRENCH>
</MESSAGE>
</MESSAGE_LIST>Is there something I can do to MSXML to have the parsing behaviour that I want ? i.e. read significant spaces from an attribute value. Thanks for any help, tips or hints. Max.
This signature was proudly tested on animals.
-
Howdy' In one of our data XML files, we need to have attribute values preserve white space at the end. For example :
<!-- without space -->
<MyTag MyAttribute="a value" /><!-- with space -->
<MyTag MyAttribute="a value " />Are two different attribute values. I tried using the preserve white space attribute
pXMLDoc->preserveWhiteSpace = VARIANT_TRUE;
but that completely craps out the parsing of the file, for example, sometimes (well, always) I get the wrong number of items in a node list (
IXMLDOMNodeListPtr
) for example, in the following snippet, when preserveWhiteSpace is FALSE, the list count is 2 and when preserveWhiteSpace is TRUE, the list count if 5 (!)<MESSAGE_LIST>
<MESSAGE Id="10" Type="Info">
<FRENCH>Message 10</FRENCH>
</MESSAGE>
<MESSAGE Id="11" Type="Info">
<FRENCH>Message 11</FRENCH>
</MESSAGE>
</MESSAGE_LIST>Is there something I can do to MSXML to have the parsing behaviour that I want ? i.e. read significant spaces from an attribute value. Thanks for any help, tips or hints. Max.
This signature was proudly tested on animals.
Maximilien wrote:
for example, in the following snippet, when preserveWhiteSpace is FALSE, the list count is 2 and when preserveWhiteSpace is TRUE, the list count if 5 (!)
That's because there are text nodes in the list - they're the manifestation of the whitespace that's been preserved from the source XML. I'm not sure that the preserve whitespace option affects attribute values anyway... Here's some quick test code I wrote that demonstrates correct whitespace handling in attribute values - the value of the arp attribute of the test element is correctly read with a trailing space:
#include "stdafx.h"
#import progid:Msxml2.DOMDocument.6.0 named_guids
#include <iostream>int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(0);
MSXML2::IXMLDOMDocument3Ptr doc;
doc.CreateInstance(MSXML2::CLSID_DOMDocument60);
if (VARIANT_FALSE != doc->loadXML(_bstr_t(L"<test arp=\"test \">\n Hello!\n</test>")))
{
MSXML2::IXMLDOMNodeListPtr nodes = doc->childNodes;
MSXML2::IXMLDOMNodePtr thisNode = nodes->nextNode();if (thisNode) do { std::cout << thisNode->nodeTypeString << " - " << thisNode->text << std::endl; MSXML2::IXMLDOMNamedNodeMapPtr attributes = thisNode->attributes; { MSXML2::IXMLDOMNodePtr thisAttribute = attributes->nextNode(); if (thisAttribute) do { std::cout << " " << thisAttribute->nodeName << " - \\"" << \_bstr\_t(thisAttribute->nodeValue) << "\\"" << std::endl; } while (thisAttribute = attributes->nextNode()); } } while (thisNode = nodes->nextNode());
}
return 0;
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Maximilien wrote:
for example, in the following snippet, when preserveWhiteSpace is FALSE, the list count is 2 and when preserveWhiteSpace is TRUE, the list count if 5 (!)
That's because there are text nodes in the list - they're the manifestation of the whitespace that's been preserved from the source XML. I'm not sure that the preserve whitespace option affects attribute values anyway... Here's some quick test code I wrote that demonstrates correct whitespace handling in attribute values - the value of the arp attribute of the test element is correctly read with a trailing space:
#include "stdafx.h"
#import progid:Msxml2.DOMDocument.6.0 named_guids
#include <iostream>int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(0);
MSXML2::IXMLDOMDocument3Ptr doc;
doc.CreateInstance(MSXML2::CLSID_DOMDocument60);
if (VARIANT_FALSE != doc->loadXML(_bstr_t(L"<test arp=\"test \">\n Hello!\n</test>")))
{
MSXML2::IXMLDOMNodeListPtr nodes = doc->childNodes;
MSXML2::IXMLDOMNodePtr thisNode = nodes->nextNode();if (thisNode) do { std::cout << thisNode->nodeTypeString << " - " << thisNode->text << std::endl; MSXML2::IXMLDOMNamedNodeMapPtr attributes = thisNode->attributes; { MSXML2::IXMLDOMNodePtr thisAttribute = attributes->nextNode(); if (thisAttribute) do { std::cout << " " << thisAttribute->nodeName << " - \\"" << \_bstr\_t(thisAttribute->nodeValue) << "\\"" << std::endl; } while (thisAttribute = attributes->nextNode()); } } while (thisNode = nodes->nextNode());
}
return 0;
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Thanks, I will look at your sample and report back. :) ---edit---- Yep, that works. Thanks.
This signature was proudly tested on animals.
modified on Wednesday, June 10, 2009 9:52 AM