Solved

Working with an XML document in VB6

Posted on 2013-11-02
2
344 Views
Last Modified: 2013-11-02
Experts, i need to get some information from an xml file. From the sample below i need to get the <Text> of all field types of "Text" where the name of the message is "Default".

I have used the code below to get the names of the messages in the file. but i dont know where to go from here.

    Dim xdoc As MSXML2.DOMDocument
    Set xdoc = New MSXML2.DOMDocument
    xdoc.validateOnParse = False
   
    Dim message As IXMLDOMElement
   
    If xdoc.Load("C:\test.xml") Then
       ' The document loaded successfully.
       ' Now do something intersting.
       'DisplayNode xdoc.childNodes, 0
        For Each message In xdoc.selectNodes("/Messages/Message/Name")
       
            Debug.Print message.Text
       
        Next
       
    End If

<Messages>
  <Message>
    <Language>English</Language>
    <Name>DEFAULT</Name>
    <Code>Undefined</Code>
    <FontSize>7</FontSize>
    <Delay>5000</Delay>
    <Bold>0</Bold>
    <Width>200</Width>
    <Gap>0</Gap>
    <Height>200</Height>
    <Pixel>16</Pixel>
    <Orientation>Upside Up</Orientation>
    <Alternate0>No</Alternate0>
    <Alternate1>No</Alternate1>
    <Alternate7>No</Alternate7>
    <RepeatPitch>0</RepeatPitch>
    <RepeatPrint>0</RepeatPrint>
    <AutoPrintPitch>0</AutoPrintPitch>
    <LanguageShifts>English</LanguageShifts>
    <ExpirationRolldown>Off</ExpirationRolldown>
    <ExpirationDate1>
      <Day>30</Day>
      <Week>0</Week>
      <Month>0</Month>
      <Year>0</Year>
    </ExpirationDate1>
    <ExpirationDate2>
      <Day>60</Day>
      <Week>0</Week>
      <Month>0</Month>
      <Year>0</Year>
    </ExpirationDate2>
    <TimeCreated>
      <Date>
        <Year>2013</Year>
        <Month>1</Month>
        <Day>24</Day>
      </Date>
      <Time>
        <Hour>11</Hour>
        <Minute>19</Minute>
      </Time>
    </TimeCreated>
    <CounterDate>
      <Date>
        <Year>2013</Year>
        <Month>1</Month>
        <Day>24</Day>
      </Date>
    </CounterDate>
    <Shift>
      <Text>A</Text>
      <Time>
        <Hour>8</Hour>
        <Minute>0</Minute>
      </Time>
    </Shift>
    <Counter1>
      <Start>0</Start>
      <Upper>100000</Upper>
      <Lower>0</Lower>
      <Increment>1</Increment>
      <Repeat>0</Repeat>
      <RepeatStart>0</RepeatStart>
      <Digits>6</Digits>
      <LeadZero>Yes</LeadZero>
      <Value>0</Value>
      <WrapAround>Yes</WrapAround>
    </Counter1>
    <Counter2>
      <Start>0</Start>
      <Upper>100000</Upper>
      <Lower>0</Lower>
      <Increment>1</Increment>
      <Repeat>0</Repeat>
      <RepeatStart>0</RepeatStart>
      <Digits>6</Digits>
      <LeadZero>Yes</LeadZero>
      <Value>0</Value>
      <WrapAround>Yes</WrapAround>
    </Counter2>
    <Field>
      <Type>Text</Type>
      <Language>English</Language>
      <Text>CITRONIX CIJ</Text>
      <FontSize>16</FontSize>
      <FontName>30021600</FontName>
      <Bold>0</Bold>
      <Gap>1</Gap>
      <X>0</X>
      <Y>0</Y>
      <Orientation>Upside Up</Orientation>
    </Field>
    <Field>
      <Type>Text</Type>
      <Language>English</Language>
      <Text>OCTOBER</Text>
      <FontSize>16</FontSize>
      <FontName>30021600</FontName>
      <Bold>0</Bold>
      <Gap>1</Gap>
      <X>0</X>
      <Y>0</Y>
      <Orientation>Upside Up</Orientation>
    </Field>
    <Field>
      <Type>AutoCode</Type>
      <Language>English</Language>
      <Format>MMDDYY</Format>
      <DateDelimiter>/</DateDelimiter>
      <FontSize>7</FontSize>
      <FontName>30020700</FontName>
      <Bold>0</Bold>
      <Gap>1</Gap>
      <X>160</X>
      <Y>0</Y>
      <Orientation>Upside Up</Orientation>
    </Field>
    <Field>
      <Type>AutoCode</Type>
      <Language>English</Language>
      <Format>HHMMSS</Format>
      <TimeDelimiter>:</TimeDelimiter>
      <FontSize>7</FontSize>
      <FontName>30020700</FontName>
      <Bold>0</Bold>
      <Gap>1</Gap>
      <X>160</X>
      <Y>9</Y>
      <Orientation>Upside Up</Orientation>
    </Field>
  </Message>
</Messages>
0
Comment
Question by:Basicfarmer
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39619209
Two ways to add that (tested in VBA only but should be the same):
        For Each message In xdoc.selectNodes("/Messages/Message/Name")
            Debug.Print message.Text
            ' added below to get fields in a separate loop
            If message.Text = "DEFAULT" Then
                Dim FieldText As IXMLDOMElement
                For Each FieldText In message.parentNode.selectNodes("Field[Type='Text']/Text")
                    Debug.Print "* " & FieldText.Text
                Next
            End If
        Next
        ' OR, do it in one go
        Dim messageFieldText As IXMLDOMElement
        For Each messageFieldText In xdoc.selectNodes("/Messages/Message[Name='DEFAULT']/Field[Type='Text']/Text")
            Debug.Print "** " & messageFieldText.Text
        Next

Open in new window

0
 

Author Closing Comment

by:Basicfarmer
ID: 39619277
Perfect!!!

Thanks...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video teaches viewers about errors in exception handling.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question