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.
<?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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
Sorry carl didn't read your solution correctly. Quick question about your solution:
Dim node As XmlNode = _doc.SelectSingleNode("//a uthor[tran slate(@fir stname,'AB CDEFGHIJKL MNOPQRSTUV WXYZ','abc defghijklm nopqrstuvw xyz')='" & first.ToLower() & "' and translate(@lastname,'ABCDE FGHIJKLMNO PQRSTUVWXY Z','abcdef ghijklmnop qrstuvwxyz ')='" & 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.
Dim node As XmlNode = _doc.SelectSingleNode("//a
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("//a uthor[@fir stname='" & first.ToLower() & "' and @lastname='" & last.ToLower() & "']")
Dim node As XmlNode = _doc.SelectSingleNode("//a
ASKER
Thanks Carl!
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("//Au
If Not xAuthors Is Nothing Then
Dim xbn, xin, xan, xat As MSXML2.IXMLDOMNode
For Each xbn In xAuthors.childNodes
s = CStr(xbn.attributes.getNam