Link to home
Start Free TrialLog in
Avatar of donniedarko801
donniedarko801

asked on

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.


Avatar of broadbent
broadbent
Flag of United Kingdom of Great Britain and Northern Ireland image

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)

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of donniedarko801
donniedarko801

ASKER

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
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.
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() & "']")
Thanks Carl!