vbMarkO Posted August 5, 2006 Posted August 5, 2006 I am needing to convert contents of a Text file to xml The text file contents look like this as this is a part of the actual file THE FIRST BOOK OF MOSES, CALLED GENESIS CHAPTER 1 1 In the beginning God created the heaven and the earth. 2 And the earth was without form, and void; and darkness [was] upon the face of the deep. And the Spirit of God moved upon the face of the waters. 3 And God said, Let there be light: and there was light. 4 And God saw the light, that [it was] good: and God divided the light from the darkness. 5 And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day. 6 ¶ And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters. CHAPTER 2 1 Thus the heavens and the earth were finished, and all the host of them. 2 And on the seventh day God ended his work which he had made; and he rested on the seventh day from all his work which he had made. 3 And God blessed the seventh day, and sanctified it: because that in it he had rested from all his work which God created and made. 4 ¶ These [are] the generations of the heavens and of the earth when they were created, in the day that the LORD God made the earth and the heavens, 5 And every plant of the field before it was in the earth, and every herb of the field before it grew: for the LORD God had not caused it to rain upon the earth, and [there was] not a man to till the ground. I need to convert the above to the xml structure below <?xml version="1.0" standalone="yes"?> <Genesis Title="THE FIRST BOOK OF MOSES, CALLED GENESIS"> <Chapter> <Chap>CHAPTER 1</Chap> <Verse>1 In the beginning God created the heaven and the earth.</Verse> <Verse>2 And the earth was without form, and void; and darkness [was] upon the face of the deep. And the Spirit of God moved upon the face of the waters.</Verse> <Verse>3 And God said, Let there be light: and there was light.</Verse> <Verse>4 And God saw the light, that [it was] good: and God divided the light from the darkness.</Verse> <Verse>5 And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day.</Verse> <Verse>6 ¶ And God said, Let there be a firmament in the midst of the waters, and let it divide the waters from the waters.</Verse> <Verse>7 And God made the firmament, and divided the waters which [were] under the firmament from the waters which [were] above the firmament: and it was so.</Verse> <Verse>8 And God called the firmament Heaven. And the evening and the morning were the second day.</Verse> <Verse>9 ¶ And God said, Let the waters under the heaven be gathered together unto one place, and let the dry [land] appear: and it was so.</Verse> <Verse>10 And God called the dry [land] Earth; and the gathering together of the waters called he Seas: and God saw that [it was] good.</Verse> <Verse>11 And God said, Let the earth bring forth grass, the herb yielding seed, [and] the fruit tree yielding fruit after his kind, whose seed [is] in itself, upon the earth: and it was so.</Verse> </Chapter> <Chapter> <Chap>CHAPTER 2</Chap> <Verse>1 Thus the heavens and the earth were finished, and all the host of them.</Verse> <Verse>2 And on the seventh day God ended his work which he had made; and he rested on the seventh day from all his work which he had made.</Verse> <Verse>3 And God blessed the seventh day, and sanctified it: because that in it he had rested from all his work which God created and made.</Verse> <Verse>4 ¶ These [are] the generations of the heavens and of the earth when they were created, in the day that the LORD God made the earth and the heavens,</Verse> <Verse>5 And every plant of the field before it was in the earth, and every herb of the field before it grew: for the LORD God had not caused it to rain upon the earth, and [there was] not a man to till the ground.</Verse> </Chapter> </Genesis> This structure works real well for what I am wanting to do However... I have considered in changing one thing below with the verses. I have considered adding an attribute and putting the verse number in them... like this <Verse num="1">First verse Text goes here</Verse> I would like opinions about doing this if I should... Here is one possible reason When display the text in a RichTextbox I would like to Make the numbers Appear in the color Blue while all other text remains normal I thought it might make this easier if I placed the numbers of the verses as an attribute for this reason. The above structure at the moment does allow me to iterate through it very easy though I am not sure how I could change the numbers to blue in an rtb very easy this way Thoughts appreciated and any code direction in making the convertion... I know how to read a text file into a string or even an array just not sure how I would go about putting this directly into an xml file with the above structure or with the attribute sugestion as well... ANythoughts? vbMarkO Quote Visual Basic 2008 Express Edition!
vbMarkO Posted August 5, 2006 Author Posted August 5, 2006 Here is some code I am using at present to iterate through the xmlFile Private Sub btnReadXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadXML.Click Dim xmlDoc As New XmlDocument xmlDoc.Load(xmlFile) Dim nod As XmlNode = xmlDoc.SelectSingleNode("Genesis") For Each myNode As XmlNode In xmlDoc.DocumentElement.ChildNodes If myNode.FirstChild.InnerText = "CHAPTER 1" Then ' "CHAPTER 1" will be changed perhaps a selection from a listbox containing the chapters For i As Integer = 0 To myNode.ChildNodes.Count - 1 Dim strScrip As String = myNode.ChildNodes(i).InnerText rtb1.Text &= strScrip & vbCrLf Next Dim strChap As String = myNode.ChildNodes(0).InnerText Me.Text = strChap & " of " & nod.Attributes("Title").InnerText & " " & myNode.ChildNodes.Count - 1 & " Verses in this chapter!" lstChap.Items.Add(strChap) End If Next If I do convert the textfile so that the <Verse></Verse> Tags have an added attribute containing the verse number( <Verse num="1"></Verse> I am not quite sure how I would change the above code to put the verse number and verse text back together. Which ever I do... it will need to be done during the conversion vbMarkO Quote Visual Basic 2008 Express Edition!
vbMarkO Posted August 5, 2006 Author Posted August 5, 2006 Anyone? Ok guys I am making a real mess of this.... I am trying to convert contents of the text file into the xml... I was failing at that so I descided to read the text file contents into a Listbox... Then have create the Node elements as I scrolled through Yeah I know very CRUDE but Hey desperation LOL is the mother of inventions I hear... Anyway this isnt working for obvious reasons... but I thought I would post what I have.... and you may be able to show me how to create this xml file directly while reading the text file with streamReader Here is my code mess.... PLease give me some direction :) Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click Dim objReader As New StreamReader(myFile) Dim sLine As String = "" Dim arrText As New ArrayList() Do sLine = objReader.ReadLine() If Not sLine Is Nothing Then 'arrText.Add(sLine) ListBox1.Items.Add(sLine) End If Loop Until sLine Is Nothing objReader.Close() 'For Each itm As String In arrText ' rtb1.Text &= itm & vbCrLf 'Next End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Dim itm2 As String = ListBox1.SelectedItem Dim xmlDoc As New XmlDocument xmlDoc.Load(xmlFile) Dim chapNode As XmlNode Dim verseNode As XmlNode Dim chapterNode As XmlNode = xmlDoc.CreateElement("CHAPTER") Dim i As Integer Dim x As Integer = 2 For i = 1 To x For Each itm As String In ListBox1.Items If itm.StartsWith("CHAPTER " & i) Then ' create the child nodes chapNode = xmlDoc.CreateElement("Chap") chapNode.InnerText = itm verseNode = xmlDoc.CreateElement("Verse") verseNode.InnerText = itm2 End If Next ' add the child nodes to the new node chapterNode.AppendChild(chapNode) chapterNode.AppendChild(verseNode) 'memberNode.AppendChild(addressNode) ' add the new node to the file and save xmlDoc.DocumentElement.AppendChild(chapterNode) xmlDoc.Save(xmlFile) Next End Sub vbMarkO Quote Visual Basic 2008 Express Edition!
MrPaul Posted August 5, 2006 Posted August 5, 2006 (edited) And laziness is the father of invention... Okay, you're doing some strange things here. Firstly, the use of the outer For loop means that the selected verse is added as being both chapter 1 and chapter 2, and if the loop were allowed to continue, it would be added as chapters 3, 4, 5... This is somewhat academic, since the basic structure of the code doesn't do what is needed. I would approach it by looping through the entire text file line by line, keeping track of the current chapter by checking if the line starts with CHAPTER: Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click Dim reader As New StreamReader(myFile) Dim xmlDoc As New XmlDocument() Dim chapterNode As XmlNode = Nothing Dim node As XmlNode Dim verse As XmlAttribute Dim line As String xmlDoc.Load(xmlFile) line = reader.ReadLine() Do While (line IsNot Nothing) If (line.StartsWith("CHAPTER")) Then 'New chapter chapterNode = xmlDoc.CreateElement("Chapter") node = xmlDoc.CreateElement("Chap") node.InnerText = line chapterNode.AppendChild(node) xmlDoc.DocumentElement.AppendChild(chapterNode) ElseIf ((line.Length > 0) AndAlso (chapterNode IsNot Nothing)) Then 'Non-empty line, add as a verse node = xmlDoc.CreateElement("Verse") verse = xmlDoc.CreateAttribute("num") verse.Value = line.Substring(0, line.IndexOf(" ")) node.Attributes.Append(verse) node.InnerText = line.Substring(line.IndexOf(" ") + 1) chapterNode.AppendChild(node) End If line = reader.ReadLine() Loop reader.Close() xmlDoc.Save(xmlFile) End Sub I hope this is self explanatory. A word of warning - verses 1:6 and 2:4 contain a strange character, which may cause an exception to be thrown. Good luck :cool: Edited August 5, 2006 by MrPaul Quote Never trouble another for what you can do for yourself.
MrPaul Posted August 5, 2006 Posted August 5, 2006 Verses in blue! And for reading the resulting XML back in, turning the verse numbers to blue: Private Sub btnReadXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadXML.Click Dim xmlDoc As New XmlDocument xmlDoc.Load(xmlFile) Dim nod As XmlNode = xmlDoc.SelectSingleNode("Genesis") For Each myNode As XmlNode In xmlDoc.DocumentElement.ChildNodes If myNode.FirstChild.InnerText = "CHAPTER 1" Then ' "CHAPTER 1" will be changed .... For i As Integer = 0 To myNode.ChildNodes.Count - 1 '***** MrPaul edit from here ****** 'If its a verse, add the number If (myNode.ChildNodes(i).Name = "Verse") Then Dim strScript As String = "Verse " & myNode.ChildNodes(i).Attributes("num").Value & ": " rtb1.SelectionStart = rtb1.TextLength rtb1.SelectionColor = Drawing.Color.Blue rtb1.SelectedText = strScript End If rtb1.SelectionStart = rtb1.TextLength rtb1.SelectionColor = Drawing.Color.Black rtb1.SelectedText = myNode.ChildNodes(i).InnerText & vbCrLf '************************************ Next Dim strChap As String = myNode.ChildNodes(0).InnerText Me.Text = strChap & " of " & nod.Attributes("Title").InnerText & " " & _ myNode.ChildNodes.Count - 1 & " Verses in this chapter!" lstChap.Items.Add(strChap) End If Next End Sub Excuse me if this is a bit crude, it's been years since I've worked with an RTB. :cool: Quote Never trouble another for what you can do for yourself.
vbMarkO Posted August 6, 2006 Author Posted August 6, 2006 Thank you for setting me straight. This is now working... and I have already converted nearly half of the Old Testament. Thanx again.... vbMarkO Quote Visual Basic 2008 Express Edition!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.