Solved

Extracting Data from an XML file

Posted on 2013-11-04
10
461 Views
Last Modified: 2013-11-07
Experts, I am using the following code to get the message names and the values of text fields within a message <Message> which is in the attached file. What i need to do is change the text value of all fields of type "Text" for a given message. Then i want to extract that message into a string variable. Please show me an example of how to do this.
    Dim xdoc As DOMDocument60, varLoop As Long, ciClass As ci1000, _
        message As IXMLDOMElement, field As IXMLDOMElement
        
    Set xdoc = New DOMDocument60
    Set ciClass = main.printers(main.printerView.SelectedItem.Key)
    
    xdoc.validateOnParse = False
    
    If xdoc.Load("C:\Program Files\ID Technology\CiControl\Backup Files\" & _
                  main.printerView.SelectedItem.Key & "\" & _
                  Format(Date, "mm.dd.yy") & ".xml") Then
    
        For Each message In xdoc.selectNodes("/CiControl/Messages/Message/Name")
            
            Debug.Print message.Text
            
            msgView.Nodes.Add , , "m" & message.Text, message.Text
            
            If message.Text = ciClass.getStatus(9) Then
            
                msgView.Nodes("m" & message.Text).Image = 1
            
            Else
            
                msgView.Nodes("m" & message.Text).Image = 2
                
            End If
            
            For Each field In xdoc.selectNodes("/CiControl/Messages/Message[Name='" & _
                              message.Text & "']/Field[Type='Text']/Text")
                      
                varLoop = varLoop + 1
                
                msgView.Nodes.Add "m" & message.Text, tvwChild, "f" & field.Text, _
                                  "Field: " & varLoop & ": " & field.Text, 3
                
            Next
            
            varLoop = 0
               
        Next
    
    Else
               
        Dim errorStr As String, xpe As IXMLDOMParseError
        
        Set xpe = xdoc.parseError
        
        With xpe
        
            errorStr = "There was an error loading the backup file!     " & vbCrLf & _
                       "The backup file may be missing or corrupt.     " & vbCrLf & _
                       "A backup of the system can be created manually     " & vbCrLf & _
                       "in the system configuration form.     " & vbCrLf & vbCrLf & _
                       "due the following error." & vbCrLf & _
                       "Error #: " & .errorCode & ": " & xpe.reason & _
                       "Line #: " & .Line & vbCrLf & _
                       "Line Position: " & .linepos & vbCrLf & _
                       "Position In File: " & .filepos & vbCrLf & _
                       "Source Text: " & .srcText & vbCrLf & _
                       "Document URL: " & .url
                       
            MsgBox errorStr, vbExclamation, "ID Technology"
         
         End With
               
    End If
    
    Set ciClass = Nothing
    Set xdoc = Nothing

Open in new window

11.04.13.xml
0
Comment
Question by:Basicfarmer
  • 4
  • 2
10 Comments
 
LVL 24

Expert Comment

by:fridom
ID: 39624781
Stupid counter question. Have you checked some tutorial about XPath? I suggest to do it and you'll get your answer..

Regards
Friedrich
0
 

Author Comment

by:Basicfarmer
ID: 39625767
Yes and i dont understand it.
0
 

Accepted Solution

by:
Basicfarmer earned 0 total points
ID: 39625849
I have got it working but i do not understand it completely. What i see online gives errors. I have fumbled around enough to finally get it working. I am very disappointed in the responses that i have received.
Private Sub apply_Click()
    
    Dim xdoc As DOMDocument60, xField As IXMLDOMElement, xNode As IXMLDOMElement, _
        xSub As IXMLDOMNode, messageName As String, varLoop As Long, xmlStr As String
    
    Set xdoc = New DOMDocument60
    
    xdoc.validateOnParse = False
    xdoc.setProperty "SelectionLanguage", "XPath"
    
    'Attempt to load the backup file for the selected printer.
    If xdoc.Load("C:\Program Files\ID Technology\CiControl\Backup Files\" & _
                  main.printerView.SelectedItem.Key & "\" & _
                  Format(Date, "mm.dd.yy") & ".xml") Then
                  
        'The backup file loaded successfully.
        
        'Get the name of the message to search for fields.
        'Determine if the user has selected a field or the message.
        'The changes can be applied if either the message or one
        'of the message fields has been selected.
        
        'The user has selected the message.
        If msgView.Nodes(msgView.SelectedItem.Index).Parent Is Nothing Then
        
            messageName = msgView.SelectedItem.Text
            
        'The user has selected one of the message fields.
        Else
        
            messageName = msgView.SelectedItem.Parent.Text
            
        End If
        
        varLoop = 0
        
        'Loop through each field in the message and set the text of the field
        'in the xml backup file.
        For Each xField In xdoc.selectNodes("/CiControl/Messages/Message[Name='" & _
                           messageName & "']/Field[Type='Text']/Text")
            
            varLoop = varLoop + 1
            
            'Get the name of the message to search for fields.
            'Determine if the user has selected a field or the message.
            'The changes can be applied if either the message or one
            'of the message fields has been selected.
            
            'The key field for the message node begins with the letter "m" and
            'is concatenated with an index number. Example "m1".
            
            'The key field for the field node includes the message key field
            'and is concatenated with the letter "f" and an index number for
            'the fields of the message. Example "m1f1".
            
            'The user has selected the message.
            If msgView.Nodes(msgView.SelectedItem.Index).Parent Is Nothing Then
                
                xField.Text = "Hello"
                'xField.Text = msgView.Nodes(msgView.SelectedItem.Key & "f" & varLoop).Text
            
            'The user has selected one of the message fields.
            Else
                
                xField.Text = "World"
                'xField.Text = msgView.Nodes(msgView.SelectedItem.Parent.Key & "f" & varLoop).Text
            
            End If
            
        Next
        
        'Save the xml file with the field changes.
        xdoc.save ("C:\Program Files\ID Technology\CiControl\Backup Files\" & _
                  main.printerView.SelectedItem.Key & "\" & _
                  Format(Date, "mm.dd.yy") & ".xml")
                  
        'Begin extracting the message xml from the backup file.
        
        'Find the message xml in the backup file.
        Set xNode = xdoc.selectSingleNode("//Messages/Message[Name='JUSTIN']")
        
        'Begin extracting all of the message xml.
        For Each xSub In xNode.childNodes
            
            Debug.Print xSub.xml
            
            'Build the xml string.
            xmlStr = xmlStr & xSub.xml
            
        Next
        
        'Add the message tags to the xml string.
        xmlStr = "<Message>" & xmlStr & "</Message>"
        
        main.startMessage (xmlStr)
    
    Else
    
        'The backup file failed to load.
        'Display a message for the user explaining the failure
        'and provide information about the error.
               
        Dim errorStr As String, xpe As IXMLDOMParseError
        
        Set xpe = xdoc.parseError
        
        With xpe
            
            'Build the error string to display.
            errorStr = "There was an error loading the backup file!     " & vbCrLf & _
                       "The backup file may be missing or corrupt.     " & vbCrLf & _
                       "A backup of the system can be created manually     " & vbCrLf & _
                       "in the system configuration form.     " & vbCrLf & vbCrLf & _
                       "The following error occured:" & vbCrLf & _
                       "Error #: " & .errorCode & ": " & xpe.reason & _
                       "Line #: " & .Line & vbCrLf & _
                       "Line Position: " & .linepos & vbCrLf & _
                       "Position In File: " & .filepos & vbCrLf & _
                       "Source Text: " & .srcText & vbCrLf & _
                       "Document URL: " & .url
            
            'Display the error string.
            MsgBox errorStr, vbExclamation, "ID Technology"
         
         End With
               
    End If

    Set xdoc = Nothing
    Set xField = Nothing
    
End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Assisted Solution

by:Basicfarmer
Basicfarmer earned 0 total points
ID: 39626482
I've requested that this question be closed as follows:

Accepted answer: 0 points for Basicfarmer's comment #a39625849

for the following reason:

I have got it working but i do not understand it completely. What i see online gives errors. I have fumbled around enough to finally get it working. I am very disappointed in the responses that i have received.
0
 

Author Comment

by:Basicfarmer
ID: 39628475
Why is this question still open? I solved the problem myself.
0
 
LVL 24

Expert Comment

by:fridom
ID: 39632732
I will tell you one thing SouthMode. Have a nice day I'm gone. So feel free to feel better.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Message not shown 5 82
Help needed with Powershell  XML to MySQL 5 72
XML and version of MS XML Dom Document versions 5 32
xml with php question 5 30
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question