Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Tue Sep 18, 2018 6:37 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: [Solved] Trying to understand advanced XML loading
PostPosted: Sat Feb 09, 2013 12:49 pm 
Offline

Joined: Wed Sep 15, 2010 1:48 pm
Posts: 84
I finally got XML load and Save working 100% on a little program that converted about 1500 palm notes from an exported palm db file.

Now I am working on a little database program that to loads and saves sqllite/realsqldatabase table information. I got the program to create the save xml output, but I am having problems understanding how to load the parsed XML output.

I think I figured it out... here is my code for the XML load. It iterates through the records, but the odd thing to me is that child2 contains the xml name, but nestled node child3 contains the value of child2. Am I doing this right, is this normal? This is my debug out, followed by the xml load code.

Anyway, I got the code to work. Here is the code. I do not know if this is spaghetti code, but it is a long loop. Here it is for anyone who wants an xml working example, you will have to tweak it to your code base, but I did learn how to make xml load and save work with any amount of records. Something that I needed to know how to do.

Here is the xml load that loads the xml sample W_debug is a window with the run as a method that takes a string and outputs it to a textarea in W_debug.

dim x as new XmlDocument(xmltext)

// some node you have

// check first node of document:
dim n as XmlNode = x.FirstChild// some node you have


// walk over all children
dim child as XmlNode = n.FirstChild
while child<>Nil
W_debug.run(child.Name) // database record
W_debug.run(child.tostring)

// check if it's the node you need
if child.LocalName = "mytag" then
W_debug.run "Found tag."
end if

// some node you have
dim n1 as XmlNode

// walk over all children
dim child1 as XmlNode = child.FirstChild
while child1<>Nil

W_debug.run("child ---->" +child1.Name)// table record
W_debug.run(child1.tostring)




// walk over all children
dim child2 as XmlNode = child1.FirstChild
while child2<>Nil

W_debug.run("child 2 ---->" +child2.Name)// table record
W_debug.run(child2.tostring)
W_debug.run("child 2 Value---->" +child2.value)// table record


// Database_Name


// check if it's the node you need
if child2.LocalName = "Database_Name" then
W_debug.run "Found tag. child2.localname: " +child2.LocalName
W_debug.run "Found tag. child2.name: " + child2.name
W_debug.run "Found tag. child2.value: " + child2.value

// walk over all children
dim child3 as XmlNode = child2.FirstChild
while child3<>Nil

// check if it's the node you need
if child3.name = "#text" then
W_debug.run "Found tag."
W_debug.run("child 3 value of #text Value is---->" +child3.value)// database table name

end if




child3 = child3.NextSibling
wend


end if

if child2.LocalName = "Database_Heading" then
W_debug.run "Found tag. child2.localname: " +child2.LocalName
W_debug.run "Found tag. child2.name: " + child2.name
W_debug.run "Found tag. child2.value: " + child2.value

// walk over all children
dim child3 as XmlNode = child2.FirstChild
while child3<>Nil

// check if it's the node you need
if child3.name = "#text" then
W_debug.run "Found tag."
W_debug.run("child 3 value of #text Value is---->" +child3.value)// database table name

end if




child3 = child3.NextSibling
wend


end if


if child2.LocalName = "Table_Fields" then
W_debug.run "Found tag tablefields. child2.localname: " +child2.LocalName
W_debug.run "Found tag. child2.name: " + child2.name
W_debug.run "Found tag. child2.value: " + child2.value

// walk over all children
dim child3 as XmlNode = child2.FirstChild
while child3<>Nil

W_debug.run("child 3.name is---->" +child3.name)// name should be tablefield
W_debug.run("child 3 value of #text Value is---->" +child3.value)// database table name


// dim a single adbfield

dim AsingleAdbfield as NEW adbfield

// check if it's the node you need
if child3.name = "Table_field" then
W_debug.run "Found tag. TableField"

W_debug.run("child 3 value of Tablefield Value is---->" +child3.value)// database table name

// walk over all children node 4 start here
dim child4 as XmlNode = child3.FirstChild
while child4<>Nil

W_debug.run("child 4 should be table field properties ---->" + child4.Name)// iterates out the fields of the records
W_debug.run(child4.tostring)
W_debug.run("child 4 Value---->" +child4.value)// table record



// check if it's the node you need list all node tags here
if child4.Name = "Fieldname" then
W_debug.run "Found tag. Fieldname"

// walk over all children
dim child5 as XmlNode = child4.FirstChild
while child5<>Nil

W_debug.run("child5.name should be individual values of table field properties ---->" + child5.Name)// iterates out the fields of the records
W_debug.run(child4.tostring)
W_debug.run("child 5.Value---->" +child5.value)// fieldname


AsingleAdbfield.Fieldname = child5.value

child5 = child5.NextSibling
wend


end if

if child4.Name = "TheType" then
W_debug.run "Found tag. TheType"

// walk over all children
dim child5 as XmlNode = child4.FirstChild
while child5<>Nil

W_debug.run("child5.name should be individual values of table field properties ---->" + child5.Name)// iterates out the fields of the records
W_debug.run(child4.tostring)
W_debug.run("child 5.Value---->" +child5.value)// fieldname


AsingleAdbfield.Type = child5.value

child5 = child5.NextSibling
wend


end if




child4 = child4.NextSibling
wend


// put AsingleAdbfield into mydbfields

mydbfields.Append(AsingleAdbfield)

end if






child3 = child3.NextSibling
wend


end if


child2 = child2.NextSibling
wend




child1 = child1.NextSibling
wend




child = child.NextSibling
wend



The language reference code examples are not very clear when parsing more complex XML.

<?xml version="1.0" encoding="UTF-8" ?>
- <root>
- <Database_Table>
- <Table_record>
<Database_Name>Woofy_Woof_7</Database_Name>
<Database_Heading>Heading Goes Here</Database_Heading>
- <Table_Fields>
- <Table_field>
<Table_Field_number>1</Table_Field_number>
<Fieldname>Dog_Biscuit</Fieldname>
<TheType>VarChar</TheType>
</Table_field>
- <Table_field>
<Table_Field_number>2</Table_Field_number>
<Fieldname>Amount</Fieldname>
<TheType>Integer</TheType>
</Table_field>
- <Table_field>
<Table_Field_number>3</Table_Field_number>
<Fieldname>Free_Samples</Fieldname>
<TheType>Integer</TheType>
</Table_field>
- <Table_field>
<Table_Field_number>4</Table_Field_number>
<Fieldname>Comment</Fieldname>
<TheType>Text</TheType>
</Table_field>
</Table_Fields>
</Table_record>
</Database_Table>
</root>


Root
-Database table
--Table record
--Table name, other fields
---TableFields
----tablefield
loop until all fields are loaded

This is the XML save code:

xml = New XmlDocument
// <?xml version="1.0" encoding="UTF-8"?>
root = xml.AppendChild(xml.CreateElement("root"))

// <?xml version="1.0" encoding="UTF-8"?><root/>
Database_Table = root.AppendChild(xml.CreateElement("Database_Table"))


Table_record = Database_Table.AppendChild(xml.CreateElement("Table_record"))

Database_Name = Table_record.AppendChild(xml.CreateElement("Database_Name"))
Database_Name.AppendChild(xml.CreateTextNode(TF_TableName.text))
'
Database_Heading = Table_record.AppendChild(xml.CreateElement("Database_Heading"))
Database_Heading.AppendChild(xml.CreateTextNode("Heading Goes Here"))

// loop through the database fields

Table_fields = Table_record.AppendChild(xml.CreateElement("Table_Fields"))


for x = 0 to mydbfields.Ubound

Table_field = Table_fields.AppendChild(xml.CreateElement("Table_field"))

Table_field_number = Table_field.AppendChild(xml.CreateElement("Table_Field_number")) // nil object exception // fixed
Table_field_number.AppendChild(xml.CreateTextNode(str(x+1)))

Fieldname = Table_field.AppendChild(xml.CreateElement("Fieldname"))
fieldname.AppendChild(xml.CreateTextNode(mydbfields(x).Fieldname))

TheType = Table_field.AppendChild(xml.CreateElement("TheType"))
TheType.AppendChild(xml.CreateTextNode(mydbfields(x).Type))


// <?xml version="1.0" encoding="UTF-8"?><root><person id="55"
//active="true"/></root>
next x


I have noticed that dot notation sometimes does not work when your nestled in. I hope the new IDE has this fixed.

Since this code does work, it might help others. Is it normal that the value of what your finding is in the next nestled node?

_________________
RS2012r2 Pro Win


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group