How to get the values of the XML tags ?
Content of "sample.xml" file from which the tag values will retrieved using below code
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
Code
'Create Microsoft.XMLDOM object
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False
'path of XML file
XMLDataFile="D:\sample.xml"
'Load the XML File
xmlDoc.Load(XMLDataFile)
'Give the XML path of XML tag you want to access and /text() to access the value of tag.
'XML path follows the hierarchy
'XML path for author tag will start from catalog->book->author
'so the XML path will be /catalog/book/author
'Text() will gie the value of the tag
Set nodes = xmlDoc.SelectNodes("/catalog/book/author/text()")
Msgbox " total count of Author tag is " & nodes.length
For i = 0 To (nodes.Length - 1)
Author= nodes(i).NodeValue
Msgbox "The name of Author is " & Author
Next
'To get the description tag value in the XML file
Set nodes = xmlDoc.SelectNodes("/catalog/book/description/text()")
Msgbox " total count of Description tag is " & nodes.length
For i = 0 To (nodes.Length - 1)
Descripton= nodes(i).NodeValue
Msgbox "The Description is < " & Descripton & " > "
Next
Set xmlDoc=nothing
Content of "sample.xml" file from which the tag values will retrieved using below code
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
Code
'Create Microsoft.XMLDOM object
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False
'path of XML file
XMLDataFile="D:\sample.xml"
'Load the XML File
xmlDoc.Load(XMLDataFile)
'Give the XML path of XML tag you want to access and /text() to access the value of tag.
'XML path follows the hierarchy
'XML path for author tag will start from catalog->book->author
'so the XML path will be /catalog/book/author
'Text() will gie the value of the tag
Set nodes = xmlDoc.SelectNodes("/catalog/book/author/text()")
Msgbox " total count of Author tag is " & nodes.length
For i = 0 To (nodes.Length - 1)
Author= nodes(i).NodeValue
Msgbox "The name of Author is " & Author
Next
'To get the description tag value in the XML file
Set nodes = xmlDoc.SelectNodes("/catalog/book/description/text()")
Msgbox " total count of Description tag is " & nodes.length
For i = 0 To (nodes.Length - 1)
Descripton= nodes(i).NodeValue
Msgbox "The Description is < " & Descripton & " > "
Next
Set xmlDoc=nothing
No comments:
Post a Comment