By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,378 Members | 3,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,378 IT Pros & Developers. It's quick & easy.

XML and VB6

P: 1
hello. please help me to read an XML file like:
<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
<PERSONS>
<PERSON NOM="IARU" PRENOM="IOANA" >
<CONTACT>
<TEL>
<TEL1>111111</TEL1>
<TEL2>222222</TEL2>
</TEL>

<EMAIL>
<EMAIL1>EMAIL1@E.COM</EMAIL1>
</EMAIL>

<FAX>
<FAX1>373474747</FAX1>
</FAX>

<ADRESSE>
<RUE>RUE1</RUE>
<NO>NO1</NO>
<VILLE>VILLE1</VILLE>
</ADRESSE>
</CONTACT>
</PERSON>

<PERSON NOM="PALL" PRENOM="EVA" >
<CONTACT>
<TEL>
<TEL1>333333</TEL1>
<TEL2>444444</TEL2>
</TEL>

<EMAIL>
<EMAIL1>ema@yahoo.com</EMAIL1>
</EMAIL>

<FAX>
<FAX1>373474747</FAX1>
</FAX>

<ADRESSE>
<RUE>rue2</RUE>
<NO>no2</NO>
<VILLE>ville2</VILLE>
</ADRESSE>
</CONTACT>
</PERSON>
</PERSONS>
</ROOT>
in VisualBasic6.0.
Sep 4 '07 #1
Share this Question
Share on Google+
1 Reply


Robbie
100+
P: 180
Hi, I made a module which allows you to extract the text which is between tags in an XML or HTML file.
Have a look, play around with it a bit, I think you'll find it's what you need.
To use it you'll need to load the XML file into a string, then give that string to this function.
Expand|Select|Wrap|Line Numbers
  1. Public Function XMLTagText(XMLFileText As String, TagToUse As String, NumberToGive As Long) As String
  2. 'XML tag text extraction module by Robbi-985
  3. 'XMLFileText    -   The entire text of the XML (or HTML) file
  4. 'TagToUse       -   Will return text within this tag
  5. 'NumberToGive   -   Gives back the text in this number'th occurence
  6. '                   of the tag (i.e. if you have:
  7. '<a>Apple</a>
  8. '<a>Banana</a>
  9. '<a>Orange</a>
  10. '                   if NumberToGive is 3 then it gives back Orange.
  11. '                   Must be > 0!
  12.  
  13.  
  14.     Dim TagStartPos As Long
  15.     TagStartPos = 0
  16.     Dim TagEndPos As Long
  17.  
  18.     Dim CurrentNumber As Long
  19.     CurrentNumber = 0
  20.  
  21.     While CurrentNumber < NumberToGive
  22.  
  23.         TagStartPos = InStr(TagStartPos + 1, XMLFileText, "<" + TagToUse + ">")
  24.         If TagStartPos < 1 Then
  25.         'There's no open tag with the text of TagToUse
  26.             'Just give back nothing
  27.             XMLTagText = ""
  28.             Exit Function
  29.         End If
  30.  
  31.         CurrentNumber = CurrentNumber + 1
  32.     Wend
  33.  
  34.     TagEndPos = InStr(TagStartPos, XMLFileText, "</" + TagToUse + ">")
  35.     If TagEndPos < 1 Then
  36.     'There's an error in the formatting of the XML or HTML text!
  37.         'Help out explaining why this funciton's going to give
  38.         'back blankness by saying that there IS a format error
  39.         MsgBox "There's an open tag for '" + TagToUse + "'" + _
  40.         ", but no close tag!", vbExclamation, _
  41.         "XML or HTML format error!"
  42.         '...Then give back nothing
  43.         XMLTagText = ""
  44.         Exit Function
  45.     End If
  46.  
  47.     'Now we have the positions of the first character of the
  48.     'start and end tags
  49.  
  50.     'Add on the length of the entire tag (including <>)
  51.     '(So that we're left with the position which the
  52.     'text within the tag starts at)
  53.     TagStartPos = TagStartPos + 2 + Len(TagToUse)
  54.     'Similar thing with the ending position
  55.     TagEndPos = TagEndPos - 1
  56.  
  57.  
  58.  
  59.  
  60.     XMLTagText = Mid(XMLFileText, TagStartPos, (TagEndPos - TagStartPos) + 1)
  61.  
  62.  
  63. End Function
  64.  
  65.  
Sep 4 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.