Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Extracting Data from an XML file

Posted on 2013-11-04
10
Medium Priority
?
473 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This video teaches viewers about errors in exception handling.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

715 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