Click here to Skip to main content
Licence 
First Posted 17 Oct 2004
Views 90,440
Bookmarked 29 times

XML Parser - A Simple approach to XML File reading

By | 17 Oct 2004 | Article
This article describes the XML Parser class, a class that is designed to make the use of XML files easier for the developer. Using collections the XML Parser classes contain only the amount of information you will need about the XML tree.

Introduction

The XML Parser classes are used to simplify the process of loading and accessing a regular XML file.

Some key features:

  • XML Load of the file with just an absolute path.
  • Collections of Attributes and Nodes for easy access.
  • Methods for finding the node(s) you want by name or by attribute(name/value).

The full documentation is included in the zip file. It's documentation.chm.

Background

Some knowledge of how XML files are built is required if you are the one writing the XML files. If you are just reading someone else's file, just know that a XML file is much like a Windows Explorer Tree, in the aspect that each node can have subnodes and value.

Using the code

The document1.xml file i'm using as an example:

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <parent type="Father">John</parent>
  <parent type="Mother">Mary</parent>
  <child id="FirstSon">
    <name>Joseph</name>r>    </child>
  <child id="SecondSon">
    <name>Christian</name>r>  </child>
</root>

Using the code to load a "document1.xml" file:

 Private Sub Page_Load(ByVal sender As System.Object, 
 ByVal e As System.EventArgs) Handles MyBase.Load
    Dim xmlRoot As New SNANET.XMLParser.XMLRoot(
     Server.MapPath("xmlFile1.xml"))
    If xmlRoot.isLoaded Then
      response.write ("File succesfully loaded.")
  End If
  End Sub

Using the code to access the child node number 0 of the root node:

  Private Sub Page_Load(ByVal sender As System.Object, 
 ByVal e As System.EventArgs) Handles MyBase.Load
    Dim xmlRoot As New SNANET.XMLParser.XMLRoot(
     Server.MapPath("xmlFile1.xml"))
    If xmlRoot.isLoaded Then
      response.write (xmlRoot.children(0).name & "=" 
       & xmlRoot.children(0).value)
  End If
  End Sub

Using the code to access the child node number 0 of the root node and print it's attributes:

  Private Sub Page_Load(ByVal sender As System.Object, 
   ByVal e As System.EventArgs) Handles MyBase.Load
    Dim xmlRoot As New SNANET.XMLParser.XMLRoot(
  Server.MapPath("xmlFile1.xml"))
    If xmlRoot.isLoaded Then
    For Each att as SNANET.XMLParser.XMLAttribute In xmlRoot.Attributes
        Response.Write (att.name & "=" & att.value)
    Next
  End If
  End Sub

Using the code to access the child nodes that has the name "Child":

  Private Sub Page_Load(ByVal sender As System.Object, 
   ByVal e As System.EventArgs) Handles MyBase.Load
    Dim xmlRoot As New SNANET.XMLParser.XMLRoot(
   Server.MapPath("xmlFile1.xml"))
    If xmlRoot.isLoaded Then
    For Each item As SNANET.XMLParser.XMLNode In xmlRoot.GetNodesByName("child")
        Response.Write(item.Name & "=" & item.value)
      Next
  End If
  End Sub

Using the code to access the child nodes that has the attribute "type" and that attribute has the value of "Father":

  Private Sub Page_Load(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles MyBase.Load
    Dim xmlRoot As New SNANET.XMLParser.XMLRoot(
   Server.MapPath("xmlFile1.xml"))
    If xmlRoot.isLoaded Then
    For Each item As SNANET.XMLParser.XMLNode 
     In xmlRoot.GetNodesByType("type","father")
        Response.Write(item.Name & "=" & item.value)
      Next
  End If
  End Sub

To-Do List (help is most welcome :))

  • Enabling the XML Parser to receive the XML text as a parameter.
  • Enabling the XML Parser to save the updated XML structure to disk.

History

  • [13-OCT-2004] First version of the XML Parser Classes released

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Bernardo Heynemann



Brazil Brazil

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionTransfering xml node values to asp.net textboxes,using VB Pinmemberrcorreia2:32 16 Oct '08  
GeneralHm... Pinmemberdimzon3:28 18 Oct '04  
GeneralRe: Hm... Pinmemberbernardoh3:39 18 Oct '04  
GeneralRe: Hm... Pinmemberdimzon5:46 18 Oct '04  
GeneralRe: Hm... Pinmemberbernardoh6:01 18 Oct '04  
GeneralRe: Hm... Pinsusskom200221:12 9 Apr '05  
GeneralXmlDocment PinmemberAubyone20:51 19 Oct '04  
GeneralRe: XmlDocment Pinmemberbernardoh3:05 20 Oct '04  
GeneralRe: XmlDocment PinmemberAubyone13:43 20 Oct '04  
GeneralRe: XmlDocment Pinmemberbernardoh2:15 21 Oct '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 18 Oct 2004
Article Copyright 2004 by Bernardo Heynemann
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid