• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Reading XML into an object for searching purposes

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
donniedarko801
Asked:
donniedarko801
  • 3
  • 2
1 Solution
 
broadbentCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
donniedarko801Author Commented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
donniedarko801Author Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
donniedarko801Author Commented:
Thanks Carl!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now