CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: VB XML

  1. #1
    Join Date
    Dec 2001
    Location
    NJ
    Posts
    17

    VB XML

    I am trying to parse and load XML file into Tree Control. Section I is what I am doing in my code. Section II is my question.
    Section I -- remember that this is not a complete code. cut-n-paste will not work.
    -- Loading xml file into oDoc
    Set oDoc = New DOMDocument
    oDoc.Load "file:///" & txtFile.Text
    -- Displaying it into Tree
    AddNode oDoc.documentElement
    (
    Private Sub AddNode(ByRef oElem As IXMLDOMNode, Optional ByRef oTreeNode As Node)
    Dim oNewNode As Node
    Dim oNodeList As IXMLDOMNodeList
    Dim i As Long
    oNewNode.Text = oElem.nodeName
    Set oNewNode.Tag = oElem
    Set oNodeList = oElem.childNodes
    For i = 0 To oNodeList.length - 1
    AddNode oNodeList.Item(i), oNewNode
    Next
    End Sub
    )
    -- my xml is look like this
    <?xml version="1.0"?>
    <!DOCTYPE FAMILY [
    <!ELEMENT FAMILY (PERSON+, PET*)>
    <!ELEMENT PERSON EMPTY>
    <!ATTLIST PERSON
    name CDATA #REQUIRED
    >
    <!ELEMENT PET EMPTY>
    <!ATTLIST PET
    name CDATA #REQUIRED
    type (cat|dog) #REQUIRED
    >
    ]>
    <FAMILY>
    <PERSON name="Freddy"/>
    <PERSON name="Maartje" />
    <PERSON name="Gerard"/>
    <PERSON name="Gerard"/>
    <PERSON name="Peter"/>
    <PET name="Bonzo" type="dog"/>
    <PET name="Arnie" type="cat"/>
    <PET name="dogy" type="rat"/>
    </FAMILY>

    Section II
    When I clicked display button it shows
    FAMILY
    |
    |--PERSON
    |--PERSON
    |--PERSON
    |--PET
    |--PET
    I want the attributes should be printed. instead of PERSON it should print Freddy, Peter etc. etc.


    Please advice

    Thanks


  2. #2
    Join Date
    Feb 2000
    Posts
    149

    Re: VB XML

    Well, I don't see the code for your display button, but you should be able to get what you want by accessing the nodeTypedValue like this...if this objNode was an element or attribute with a value you could print the value to the immediate window like this.


    Dim objNode as IXMLDOMNode
    Debug.print objNode.nodeTypedValue






  3. #3
    Join Date
    Feb 2000
    Posts
    149

    Re: VB XML

    I'm just going to post a bit of code I use to switch sender and recipient of a message, this should show you everything you need. To make this more robust, you can pass in the element names you want to search for and replace, this function has them hardcoded. This is changing attributes and elements.


    public Function SwitchJackSenderAndRecipient(byval FileName as string) as Boolean

    Dim objSender as MSXML2.IXMLDOMNode
    Dim objRecipient as MSXML2.IXMLDOMNode

    Dim objMessage as MSXML2.IXMLDOMNode

    Dim objAttribute as MSXML2.IXMLDOMAttribute

    Dim strSender as string
    Dim strRecipient as string
    Dim strTemp as string

    Dim blnSuccess as Boolean

    on error GoTo ErrHandler

    If objDOM.Load(FileName) then

    set objMessage = objDOM.selectSingleNode("//Message")

    If Not objMessage is nothing then
    set objAttribute = objMessage.Attributes.getNamedItem("Sender")

    If Not objAttribute is nothing then
    strSender = objAttribute.nodeTypedValue
    End If

    set objAttribute = objMessage.Attributes.getNamedItem("Recipient")

    If Not objAttribute is nothing then
    strRecipient = objAttribute.nodeTypedValue
    End If

    'now switch the values
    strTemp = strSender
    strSender = strRecipient
    strRecipient = strTemp

    set objAttribute = objMessage.Attributes.getNamedItem("Sender")

    If Not objAttribute is nothing then
    objAttribute.nodeTypedValue = strSender
    End If

    set objAttribute = objMessage.Attributes.getNamedItem("Recipient")

    If Not objAttribute is nothing then
    objAttribute.nodeTypedValue = strRecipient
    End If

    strTemp = vbNullString
    strSender = vbNullString
    strRecipient = vbNullString

    End If

    set objSender = objDOM.selectSingleNode("//Sender")

    If Not objSender is nothing then
    strSender = objSender.nodeTypedValue
    End If

    set objRecipient = objDOM.selectSingleNode("//Recipient")

    If Not objRecipient is nothing then
    strRecipient = objRecipient.nodeTypedValue
    End If

    'now switch the values
    strTemp = strSender
    strSender = strRecipient
    strRecipient = strTemp

    objSender.nodeTypedValue = strSender
    objRecipient.nodeTypedValue = strRecipient

    objDOM.save FileName

    set objSender = nothing
    set objRecipient = nothing
    set objMessage = nothing
    set objAttribute = nothing

    blnSuccess = true
    else
    blnSuccess = false
    End If

    SwitchJackSenderAndRecipient = blnSuccess

    Exit Function

    ErrHandler:
    SwitchJackSenderAndRecipient = false
    End Function





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured