?
Solved

XML reader using class structure

Posted on 2003-02-28
7
Medium Priority
?
234 Views
Last Modified: 2013-11-23
hi

i am quite new to vb and am hoping to build an application in vb6 that reads some xml content and then inserts it to a database.

i have partially written 2 classes:
CReport
CCustomer

the xml structure i have mirrors these classes:

<Report>
<ReportNumber>1</ReportNumber>
     <Customer>
     <Name>CocoSteel</Name>
     </Customer>
<ReportDate>21/11/2002</ReportDate>
</Report>


the CReport class does the following

1. loads the xml file into a DOM
2. sends the document element to the following LoadXML method:

Public Sub LoadXML(node As MSXML2.IXMLDOMNode)

Set node = node.childNodes(0)

Do While Not node Is Nothing

    Select Case LCase(node.NodeName)
   
    Case Is = "reportnumber"
    ReportDate = node.text  ' report date is a property

    Case Is = "customer"
    customer.Loadxml node   ' customer is an instanciated
                            'object of the Ccustomer                         ' class
     
    Case Is = "reportdate"
    ReportOrganisation = node.text  ' report organisation is a property

    End Select
   
    Set node = node.nextSibling
   
   Loop

in the customer class there would also be a loadxml method which would loop through the child elements of the customer node.

The problem this created was that when the loop in the Report class reached the "customer" element it jumps into the customer class and destroys the node object in the Report class.  Accordingly when the code finishes in the customer class and jumps back to the Report class i cannot use the node object for .next sibling and therfore cannot finish the loop in the Report class.

I wondered if i actually created a DOM tree within the classes and put the nodes into this. and then put the values into these and then read them from this tree.  BUT it seems like this will be replicating work.  
i already have the data in a DOM so why should i create another?

So my question is:
Is there any way of maintaing the Report node object whilst i move into the Customer class object.  If not can you recommend any approach to achieving the same result without excessive complexity.
things to consider are:

1. i would like to use a class structure rather than doing the whole thing in one form procedure.
2. i have already read the "XML Wrapper template; Transform XML documents into vb classes" article on MSDN http://msdn.microsoft.com/msdnmag/issues/01/01/XMLWrap/default.aspx  - this is very good but i dont think i want to create another DOM structure when i already have the data in one at the start of the Report class.
3. i have not added in the inserts to the db yet but will do this later once i have exposed the content.

thankyou very much.







0
Comment
Question by:cocosteel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 8044915
>>it jumps into the customer class and destroys the node object in the Report class


Explain "destroys".  If you debug your project and set a breakpoint at:

customer.Loadxml node

And when the code hits that point do a Shift + F8 to execute that line, then when it steps out of that function call, in the Immediate window do a:


?node Is Nothing

If that returns False, do a:

?node.Text

and see what it returns
0
 

Author Comment

by:cocosteel
ID: 8056798
AzraSound  thanks for responding

i followed your instructions and got a true.  

so as this meant the Reportnode was getting set to nothing. i  decided to continue to pass that Reportnode in to the CCustomer class loadnode method.  but then to declare a new node in CCustomer and and set this new node:

CustomerNode = ReportNode.childNodes(0)

then use the new node to loop round the second element.  and this has allowed me to continue using the reportnode when i went back to the Report class.  

re: >>it jumps into the customer class and destroys the node object in the Report class - i was confused about this because i thought that when scope went to another object all nodes were automatically set to nothing?  having now fixed this i dont think this is correct??

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 8057364
Well, I never saw your function in the CCustomer class, but I assume it had a similar loop...looping until the passed in Node was Nothing.  Thus, that is why when it returned back to the CReport class function, the node was nothing.  Your function is passing the node ByRef, that is, it is passing a pointer to the node so that in CCustomer, you are working with the same node object in memory.  If you pass it ByVal to CCustomer, you would not suffer from this problem, and it sounds like maybe that is the way you may wish to approach this.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cocosteel
ID: 8058174
so i can just declare and use one node? do you think that is a better way of doing it than creating another node in the CCustomer?
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 300 total points
ID: 8058255
Actually, when object's are passed to a function, whether it be ByVal or ByRef, you are only passing a pointer to the object, so the ByVal option really shouldn't make any difference.

The reason you're method worked is because you then grabbed a reference to the passed in node's child, and manipulated it, leaving the original passed in node parameter alone.  Therefore, I would continue using that method if it works for you.
0
 

Expert Comment

by:Tuy
ID: 8070960
an interesting program for u is Visual code generator, u can find it at http://www.liquid-technologies.com/

The program generates the classes for u, saves a lot of time

U can download a trail version there, u can use it only for 15 classes and 30 attributes, but i guess that is sufficient ?
If u go over that split the scheme and then manual add the together.
I've use the program and was positively surprised :)

Hope it helps
0
 

Author Comment

by:cocosteel
ID: 8071370
thanks, you got me thinking about what was actually happening!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

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