Results 1 to 14 of 14

Thread: XML Editing

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    8

    XML Editing

    Hello All,

    I am currently trying to create a VB program that will take an XML document, search through it for certain specified fields and if I choose, change them. Then it will save it as a different titled XML file.

    I first tried opening it in notepad format and changing via a text editor, however I can't save it correctly back into XML format.

    I then found code for VB concerning XML files. Tweaking this a bit I am able to extract certain data, but I am unable to alter this data and save it as a retitled XML file.

    Here is my code currently:

    Code:
    Option Explicit
    
    Private m_AppPath As String
    
    ' Return the node's value.
    Private Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, ByVal node_name As String, Optional ByVal default_value As String = "") As String
    Dim value_node As IXMLDOMNode
    
        Set value_node = start_at_node.selectSingleNode(".//" & node_name)
        If value_node Is Nothing Then
            GetNodeValue = default_value
        Else
            GetNodeValue = value_node.Text
        End If
    End Function
    
    ' Load saved values from XML.
    Private Sub LoadValues()
    Dim xml_document As DOMDocument
    Dim values_node As IXMLDOMNode
    
        ' Load the document.
        Set xml_document = New DOMDocument
        xml_document.Load m_AppPath & "test 2.xml"
    
        ' If the file doesn't exist, then
        ' xml_document.documentElement is Nothing.
        If xml_document.documentElement Is Nothing Then
            ' The file doesn't exist. Do nothing.
            Exit Sub
        End If
    
        ' Find the Values section.
        Set values_node = xml_document.selectSingleNode("DynaMark-Project")
    
        ' Read the saved values.
        txtBarcode.Text = GetNodeValue(values_node, "Text", "???")
    End Sub
    
    Private Sub Form_Load()
        ' Get the application's startup path.
        m_AppPath = App.Path
        If Right$(m_AppPath, 1) <> "\" Then m_AppPath = m_AppPath & "\"
    
        ' Load the values.
        LoadValues
    End Sub
    My XML experience is non-existent basically, so any help would be very grateful. As you can see from the code I can extract the data into a text box, but if i change what is in the text box, I want to then alter the value in a similar XML file.

    Can anyone help?

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML Editing

    This line loads a selected node into a node object.
    Code:
    Set values_node = xml_document.selectSingleNode("DynaMark-Project")
    Once you have a node loaded you can read and set it's text value by using the text property of the node.
    To read
    Code:
    MsgBox values_node.Text
    To change
    Code:
    values_node.Text = "NewValue"
    Now to save as a new document, you use the save method of the document object.
    Code:
    xml_document.Save "NewPath"

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    8

    Re: XML Editing

    I have attached a similar XML file called shot1.xml
    I am trying to take the data in the node "text", in this case it should be "617458" and insert it into a textbox called txtBarcode.text.

    If I change what is in the text box and click a command button do I put in the command button click event what you have outlined.

    I tried:

    Code:
    values_node.text = "Newvalue"
    xml_document.save = "Newpath"
    but it didn't save it in the same format as the original shot1.XML, it just saved the node and value.
    Attached Files Attached Files

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML Editing

    Here is a real quick example. It uses 2 command buttons (cmdLoad and cmdSave) and one textbox (default name) and assumes the file is at "c:\shot1.xml"
    Code:
    Option Explicit
    Dim objDoc As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMNode
    
    Private Sub cmdLoad_Click()
        Set objDoc = New MSXML2.DOMDocument
        objDoc.async = False
        objDoc.Load "c:\shot1.xml"
    
        Set objNode = objDoc.selectSingleNode("//Text")
        Text1.Text = objNode.Text
    End Sub
    
    Private Sub cmdSave_Click()
        objNode.Text = Text1.Text
        objDoc.save "c:\shot2.xml"
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        If Not objNode Is Nothing Then
            Set objNode = Nothing
        End If
        
        If Not objDoc Is Nothing Then
            Set objDoc = Nothing
        End If
    End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    8

    Re: XML Editing

    That is Great. Thanks so much.

    One more small query about the XML document. I am retrieving info from the Text node, if I try and retrieve info from the Offset node it returns a blank. Why is this? Is it because it is not in <Offset>""<Offset> format?

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML Editing

    It is because the Offset node does not have any values. It has attributes. What you need to do is load the Offset node and the query it's attributes
    Code:
    Private Sub cmdLoad_Click()
    Dim objDoc As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMNode
    Dim objAttrib As MSXML2.IXMLDOMAttribute
    
        Set objDoc = New MSXML2.DOMDocument
        objDoc.async = False
        objDoc.Load "c:\shot1.xml"
    
        Set objNode = objDoc.selectSingleNode("//Offset")
        Set objAttrib = objNode.Attributes.getNamedItem("value")
        
        Text1.Text = objAttrib.Text
    End Sub

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    8

    Re: XML Editing

    Thankyou very much MarkT. You have really helped me today.

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    8

    Re: XML Editing

    Back again. I have a continuing problem with XML Editing.

    In the previous attached XML file I was looking to Extract the Text Node data into a text box, then if the text box was changed the Text Node will also change.

    With help from MarkT I have been able to get this working for the previous attached file, however with this attached file it won't work.

    As can be seen it is a Duplicated XML document, therefore there is Two instances of Text Nodes. My current Code, (See below), will only change the first Text Node, not the second. How can I get this to happen.

    Code:

    Code:
    Option Explicit
    Dim objDoc As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMNode
    Dim objAttrib As MSXML2.IXMLDOMAttribute
    
    Private Sub cmdLoad_Click()
    
        Set objDoc = New MSXML2.DOMDocument
        objDoc.async = False
        objDoc.Load "C:\shottwo.xml"
        
        Set objNode = objDoc.selectSingleNode("//Text")
            Text1.Text = objNode.Text
    End Sub
    
    Private Sub cmdSave_Click()
        objNode.Text = Text1.Text
        objDoc.save "C:\shotthree.xml"
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        If Not objNode Is Nothing Then
            Set objNode = Nothing
        End If
        
        If Not objDoc Is Nothing Then
            Set objDoc = Nothing
        End If
    End Sub
    Attached Files Attached Files

  9. #9
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML Editing

    Are you looking to set all the text nodes to the same value?

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    8

    Re: XML Editing

    Yes, thanks.

  11. #11
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML Editing

    instead of selecting a single node you need to return a nodelist and then loop through the nodelist and set the text property of the nodes
    Code:
    Option Explicit
    
    Dim objDoc As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMNode
    Dim objAttrib As MSXML2.IXMLDOMAttribute
    Dim objNodeList As MSXML2.IXMLDOMNodeList
    
    Private Sub cmdLoad_Click()
    
        Set objDoc = New MSXML2.DOMDocument
        objDoc.async = False
        objDoc.Load "C:\shottwo.xml"
        
        'Return all the text nodes as a nodelist
        Set objNodeList = objDoc.selectNodes("//Text")
        'Set objNode = objDoc.selectSingleNode("//Text")
        'Text1.Text = objNode.Text
        Text1.Text = objNodeList.Item(0).Text
    End Sub
    
    Private Sub cmdSave_Click()
        For Each objNode In objNodeList
            objNode.Text = Text1.Text
        Next objNode
        
        objDoc.save "C:\shotthree.xml"
    End Sub
    
    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        If Not objNode Is Nothing Then
            Set objNode = Nothing
        End If
        
        If Not objNodeList Is Nothing Then
            Set objNodeList = Nothing
        End If
        
        If Not objDoc Is Nothing Then
            Set objDoc = Nothing
        End If
    End Sub

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    8

    Re: XML Editing

    Thanks, does the same apply to Attributes?

  13. #13
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML Editing

    Yes. You would have to create your nodelist and then loop through the nodelist. For each node you would then have to select the attribute and then set it's text property.

  14. #14

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    8

    Re: XML Editing

    Super job MarkT. I think i'm slowly understanding this stuff.

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