Solved

Extracting Data from an XML file

Posted on 2013-11-04
10
446 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now