Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Extracting Data from an XML file

Posted on 2013-11-04
10
Medium Priority
?
477 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.
Suggested Courses

885 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