Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. Preserving white space in attribute values. (using MSXML)

Preserving white space in attribute values. (using MSXML)

Scheduled Pinned Locked Moved XML / XSL
xmljsonhelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • M Maximilien

      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.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        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

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups