Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reading XML into an object for searching purposes

Posted on 2006-05-01
6
Medium Priority
?
212 Views
Last Modified: 2010-04-23
I would like to parse an XML file like:

<?xml version='1.0'?>
<authors>
  <author firstname="William" lastname="Shakespeare">
    <book>"The Tempest"</book>
    <book>"Romeo and Juliet"</book>
    <book>"Macbeth"</book>
    <book>"The Taming of the Shrew"</book>
  </author>
  <author firstname="Steven" lastname="King">
    <book>"Carrie"</book>
    <book>"Cujo"</book>
    <book>"Insomnia"</book>
  </author>
  <author firstname="Craig" lastname="Clevenger">
    <book>"Dermaphoria"</book>
    <book>"The Contortionists Handbook"</book>
  </author>
</authors>

into an object(array? struct? class? linked list?) and be able to use it in this way:

if user types "William" and "Shakespeare" into two text boxes then write to console books that author wrote
if no exact match then do nothing, prompt again.


0
Comment
Question by:donniedarko801
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:broadbent
ID: 16583986
to start you off, and I am writing this on the fly so haven't tested anything

Imports MSXML2.DOMDocument40Class

Private Sub ReadXML()
Dim XML As New MSXML2.DOMDocument40
XML.validateOnParse = False
XML.load("yourxmlfile")
Dim xAuthors As MSXML2.IXMLDOMNode
xAuthors = XML.selectSingleNode("//Authors")
If Not xAuthors Is Nothing Then
Dim xbn, xin, xan, xat As MSXML2.IXMLDOMNode
For Each xbn In xAuthors.childNodes
s = CStr(xbn.attributes.getNamedItem("book").nodeValue)

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1400 total points
ID: 16584484
Heres a simple console app that should do what you want:

Imports System.Xml

Module Module1

    Private _doc As XmlDocument

    Sub Main()

        _doc = New XmlDocument()
        _doc.Load("C:\Books.xml")

        Call GetUserInput()

    End Sub

    Private Sub GetUserInput()

        Dim cont As Char = "y"
        Dim first As String
        Dim last As String

        While cont <> "n"

            Console.Write("Enter first name: ")
            first = Console.ReadLine()

            Console.Write("Enter last name: ")
            last = Console.ReadLine()

            If first = "" Or last = "" Then
                Console.WriteLine("Please provide a first and last name!")
                Continue While
            End If

            Dim node As XmlNode = _doc.SelectSingleNode("//author[translate(@firstname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & first.ToLower() & "' and translate(@lastname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & last.ToLower() & "']")
            If Not node Is Nothing Then
                For Each book As XmlNode In node.ChildNodes
                    Console.WriteLine(book.InnerText)
                Next
            Else
                Console.WriteLine("No matches found!")
            End If

            Console.WriteLine()
            Console.WriteLine("Search again (Y/N)?")

            cont = Console.ReadLine()(0)

        End While

    End Sub

End Module
0
 
LVL 1

Author Comment

by:donniedarko801
ID: 16590876
I am looking to put them into some sort of data structue so that I can use it without parsing through the XML everytime, parsing through is something I understand, but putting it into some sort of data structure is what I need help with
0
Technology Partners: 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!

 
LVL 1

Author Comment

by:donniedarko801
ID: 16591290
Sorry carl didn't read your solution correctly. Quick question about your solution:

Dim node As XmlNode = _doc.SelectSingleNode("//author[translate(@firstname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & first.ToLower() & "' and translate(@lastname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='" & last.ToLower() & "']")

how would this line look if you were not lowercasing all the text. this appears to do what I want it to, but I can assume the text will be exact.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16591392
You just need to remove the translate function call, so it becomes:

Dim node As XmlNode = _doc.SelectSingleNode("//author[@firstname='" & first.ToLower() & "' and @lastname='" & last.ToLower() & "']")
0
 
LVL 1

Author Comment

by:donniedarko801
ID: 16591710
Thanks Carl!
0

Featured Post

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!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

578 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