Solved

Working with an XML document in VB6

Posted on 2013-11-02
2
346 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Automation VBA 19 88
Python 3.5.2 32 virtualenv problems 3 64
Problem to refer to value 8 62
Link failure 16 32
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

839 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