XML parser don't know whitespace length

501 views Asked by At

I have the following XML sample.

    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:f="http://kbcfp.com/BoTech/Inferno/Fault"
    xmlns:base="http://kbcfp.com/BoTech/Inferno/Base" xmlns:h="http://kbcfp.com/BoTech/Inferno/Header" xmlns:ep="http://torstonetech.com/Inferno/FDE">
    <SOAP-ENV:Header>
    <h:optLockSeqNumbers>
    <h:values><h:editSequenceNumber>1</h:editSequenceNumber><h:modifiedDateTime>2015-01-21T15:33:10+06:30</h:modifiedDateTime><h:modifiedUserId>1005</h:modifiedUserId><h:objectId>100100049</h:objectId><h:optimisticLockObjTypeId>11</h:optimisticLockObjTypeId></h:values>
    </h:optLockSeqNumbers>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
    <ep:AccountAttrs><ep:accountId>100100049</ep:accountId><ep:attrId>100001896</ep:attrId>
    <ep:AccountAttrs><ep:accountId> </ep:accountId><ep:attrId>100001896</ep:attrId></ep:AccountAttrs>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

I have to read this XML and expected output as below.

1st  ep:accountId 100100049
     ep:attrId 100001896
2nd  ep:accountId  <!-- want to write 1 space in these place -->
     ep:attrId 100001896

This is my XML parser code.

Dim xmlDoc As MSXML2.DOMDocument
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.LoadXML aboveXMLStringVale
Call readNode(xmlDoc.ChildNodes)

public Function readNode(ByRef nodes AS MSXML2.IXMLDOMNodeList)
Dim xmlNode As MSXML2.IXMLDOMNode
For Each xml Node In nodes
  If xmlNode.nodeType = NODE_TEXT Then
    ThisWorkbook.Worksheets("xxx").Cell(row,column) = xmlNode.ParentNode.ParentNode.nodeName
    ThisWorkbook.Worksheets("xxx").Cell(row,column) = xmlNode.ParentNode.nodeName
    ThisWorkbook.Worksheets("xxx").Cell(row,column) = "'" & xmlNode.NodeValue
  End If

  If xmlNode.HasChildNodes Then 
   Call readNode(xmlNode.ChildNodes)
  End If
Next
End Function

Code is fine and show the output if XML childnode text have no space value. My code condition is check XML element tag have value or not by using "If xmlNode.HasChildNodes Then" condition.
For white space case, there is no childnode and no length for xmlNode object.
So I can't check the condition and can't write whitespace element tag value and them coding will skip to next XML element tag.

I am really appreciate for any help and suggestion.

Addition:
This "If xmlNode.HasChildNodes Then" condition is check xmlNode childnode length.
eg.
if childnode length > 0 return true, if not return false.
As my whitespace case, condition will always return false because childnode lenght is 0 for whitespace value.

1

There are 1 answers

1
ThW On BEST ANSWER

You XML parser ignores the whitespace node. Try changing the DOMDocument.preserveWhiteSpace property before loading the XML.

Dim xmlDoc As MSXML2.DOMDocument
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.preserveWhiteSpace = true
xmlDoc.LoadXML aboveXMLStringVale

HINT: You're using the XML parser to parse SOAP. Do you don't have SOAP parser/library available?