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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp to xml

Status
Not open for further replies.

dionisox

Technical User
May 3, 2002
10
IT
i need to make a xml file like this

- <people>
- <person>
<name>A</name>
- <people>
- <person>
<name>A1</name>
</person>
- <person>
<name>A2</name>
- <people>
- <person>
<name>A2.1</name>
</person>
- <person>
<name>A2.2</name>
</person>
</people>
</person>
</people>
</person>
</people>

I must use asp and a database table.
How must i organize the database and how must i recreate the xml structure?
Can anyone suggest me how can i do?
Thanks
 
DB: The guidelines for the database are not to important to generate the XML, as you can always get it out in a fashion you need, but the best design I could think of would be:
Table: People
person_id - auto-incrememnting id
person_name - text, varchar, char, etc

SQL to retrieve data:
&quot;SELECT person_name FROM People&quot;

XML: Now perhaps the easiest way to create the xml is to just create a large string. This is both more efficient than creatnig an XML DOM document on the fly and easier.
So say we execute out SQL statement and get a recordset back with an unknown number of records, to create the XML we could do the following:
Code:
'pretend rsPeople is our recordset
Code:
Dim xmlString
'insert the root element
Code:
xmlString = &quot;<people>&quot;
'make sure we are at the first record
Code:
rsPeople.MoveFirst
'start our loop through the recordset
Code:
Do Until rsPeople.EOF
'each record should be encapsulated by a person element
Code:
   xmlString = xmlString & &quot;<person>&quot;
'add the name element with record info
Code:
   xmlString = xmlString & &quot;<name>&quot; & rsPeople(&quot;name&quot;) & &quot;</name>&quot;
'finish the person element before moving on
Code:
   xmlString = xmlString & &quot;</person>&quot;
'incrememnt recordset to next record
Code:
   rsPeople.MoveNext

Loop
'Finish the root element
Code:
xmlString = xmlString & &quot;</people>&quot;

Now at this point if you need to save the xml to a file there are a couple ways to handle it. You can either create a File object using the FileSystemObject to get a reference to the file and a TextStream object to write to the file, you could use ADO to get a reference to the file and open a TextStream for writing to the file, or you can use the XML DOM that I didn't use above.

I prefer using the XML Dom in this case because I am lazy:
Code:
Dim xmlObj
'instantiate the object
Code:
Set xmlObj = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)
'load our xml string into the object
Code:
xmlObj.LoadXML xmlString
'save it out to a file in same directory
Code:
xmlObj.Save Server.MapPath(&quot;people.xml&quot;)
'Dereference object
Code:
Set xmlObj = Nothing

And there we have it, data successfully pulled from a db and saved out as a valid xml document.

Hope this helps,
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
Just a quick question, how do you append a new person ?

eg. say for some bizzare reason you wanted to add another person to 'people.xml' after you've created it and put the info from the database into it ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top