Solved

Working with an XML document in VB6

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordlen challenge 3 49
groovy example issue 10 71
using web browser with BING 40 89
Macro Excel - Multiple If conditions 2 17
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.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now