Solved

Extracting Data from an XML file

Posted on 2013-11-04
10
457 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create XML 5 49
ASP.NET reading ATOM 2 62
XML XSL Choose example 3 25
Eliminate additional border 1 18
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 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