Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Basic Question 1

Status
Not open for further replies.

jray2003

Programmer
Aug 12, 2003
253
US
I have read all the threads on working with XML and I have a couple of questions and need some examples.

Do you handle opening and reading an XML file any different the regular files? Here is what I want to do.

1. Open a XML file.
2. Read through it and check for some special that I am looking for.
3. When found, I will write those lines of information to another file.

Now I have loop through files before so that is no big thing, but I have never read in a XML file before. I need to be able to read it line by line till the end of the file.

Is there something special I need to understand about doing this?

I am running VB6.

Thanks
 
Thanks for the information. This will be helpful.
 
Are you familiar with recordsets? If so, xml can be converted to a recordset. then you can use all sorts of simple recordset functions to look for data. If you're interested, I can provide code samples.



ciao for niao!

AMACycle

American Motorcyclist Association
 
AMACycleLoony

Yes, I am interest and thank you very much.
 
strData" is a string that contains your xml.

Do While Not (strData = "")
xmlData = Mid(strData, 1, InStr(1, strData, "</xml>") + 5)
' remove part placed in xmlData var from strData var
If (InStr(1, strData, "</xml>") + 6) > 0 Then
strData = Mid(strData, InStr(1, strData, "</xml>") + 6)
Else
strData = ""
End If
' convert xml data to recordset
Set objStream = New ADODB.Stream
objStream.Open
objStream.WriteText xmlData
objStream.Position = 0
Set rsXML = New ADODB.Recordset
rsXML.Open objStream
objStream.Close
Set objStream = Nothing
' save data to database
rsXML.Filter = "rsXMLField='" & Trim(txtField.Text) & "'"
if not rsXML.eof Then
' do something
End if
' VERY important to clear out Filter before continuing.
rsXML.Filter = ""
Loop


ciao for niao!

AMACycle

American Motorcyclist Association
 
The ADO Recordset's ability to persist itself as XML and load it back is useful, but not a substitute for a generalized or specialized XML parser.
 
Thanks,

This is a start and hope I can get it to work. This looks more simple then the others things I can seen.
 
dilettante,

Understandable, but what am I doing is not the normal parser, at least I think seeing how this is my first stab at it.

I am doing something simple by just reading it, checking for a certain string and then writing it out.

Do you have a simple example of how this is done your way? I am willing to listen and learn.

Thanks again,
 
Alas, I don't have a simple example. I'd probably fall back on MSXML or a similar generalized tool myself. You might try starting at:


Then perhaps follow up Microsoft's or somebody else's parser via various web sites:


The Recordset only supports its own document type.
 
What exactly are you looking for in the file?
Specific Tags or Text in the tags?

You can use DOM to do either very easily...

Next, do you want the target file to be in XML format, or just Text...?

Give me an example of what you are trying to do, and I can give you an example of what you could do ;-)

For example...
If you want to Open one XML file and find specific Tags to Copy To A second file, you could do this:
Code:
Sub CopyTags(sourceFile As String, targetFile As String, TagToCopy As String)
  [b]'*** Setup Source & Target DOM's[/b]
  Dim DOM1 As DOMDocument
  Dim DOM2 As DOMDocument
  Set DOM1 = New DOMDocument
  Set DOM2 = New DOMDocument
  DOM1.async = False
  DOM2.async = False
  
  [b]'*** Load XML File Into Source DOM[/b]
  DOM1.Load sourceFile
  
  [b]'*** Add root node <Results> to new Target DOM[/b]
  DOM2.appendChild DOM2.createElement("Results")
  Dim E As IXMLDOMElement
  
  [b]'*** Loop through all <TagToCopy> tags in xml file[/b]
  For Each E In DOM1.selectNodes("//" & TagToCopy)
    [b]'*** Copy each Tag to target DOM[/b]
    DOM2.documentElement.appendChild E.cloneNode(True)
  Next
  
  [b]'*** Indent the XML to make it easy to read through... <<Optional>>[/b]
  DOM2.loadXML Replace(DOM2.xml, "><", ">" & vbCrLf & "<")
  
  [b]'*** Finally, save the new DOM to a file...[/b]
  DOM2.save targetFile
End Sub

Then call it like this...
Code:
CopyTags "SomeXMLFile.xml", "NewXMLFile.xml", "Name"

Which will find ALL <Name> tags in "SomeXMLFile.xml" and copy them to "NewXMLFile.xml"

You can also use XSL to select and format the output, which can get a little more complicated, be can also prove useful, and you can exucute it through VB, via DOM as well...

Let me know if you are interested...

Hope This Helps,
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Here is what I am trying to do. I need to open a XML file, read through it and search for this TAG:

"<text:span>1D76500</text:span>"
"<text:span>3.2<text:tab/>Limited</text:span>"

An extract the data out and save it to a text file.

Hope this is helpful.
 
Try the Sub I posted above like this:
Code:
CopyTags "Source.xml", "Target.xml", "text:span"

If you don't want the <result> tags around the result, do this:
Code:
Sub WriteTags(sourceFile As String, targetFile As String, TagToCopy As String)
  '*** Setup Source DOM
  Dim DOM1 As DOMDocument
  Set DOM1 = New DOMDocument
  DOM1.async = False
  DOM1.Load sourceFile
  
  '*** Loop through all <TagToCopy> tags in xml file and write results to string
  Dim E As IXMLDOMElement
  Dim TextOut As String
  For Each E In DOM1.selectNodes("//" & TagToCopy)
    TextOut = TextOut & E.xml & vbCrLf
  Next
  
  '*** Save the string to the target file...
  Open targetFile For Output As #1
    Print #1, TextOut
  Close
End Sub

With this call...
Code:
WriteTags "Source.xml", "Target.xml", "text:span"

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Cool,

This is looking better and better and thanks for all the help.

 
One last stupid question the line "DOM1.Load sourceFile" is the line that opens the file correct? Open an XML is different then opening a text file using the OPEN command, right?

This has been a great learning excersise.

Thanks again
 
Yes, it is different...

Opening a text file, such as...
Code:
Open sourceFile For Input As #1
...
Close
Simply gives you access to the string contents of the file.

On the other hand...
Code:
Dom.Load sourceFile
Opens the file, parses the contents and creates an object structure reprensentation of the file and immediately closes the file.

The alternative to this would be to use Dom.loadXML which loads a string, and parses into the object structure.
Such as:
Code:
Dom.LoadXML "<test><x>hello</x><y>world</y></test>"

So...
Dom.Load basically does this:
Code:
Open sourceFile For Input As #1
  Dom.loadXML Input(LOF(1), #1)
Close

Hope This Helps,
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Yes this does help and once again this place has been kind and gentle in helping an old novice guy like me understanding things. Other boards would have slammed me, but you all patiently worked with me and this has been so helpful.

Still I have much to learn and have orded a book that I hope will help me out.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top