Results 1 to 12 of 12

Thread: XML Parsing Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Question XML Parsing Question

    Hey Everyone, I'm trying to make a program that parses XML data and puts it into Text Boxes.

    I've gotten it all coded and don't get any error messages, but I always get "???" displayed in my text boxes. I can't figure out what I coded wrong.

    Please Help! I need to have this figured out by tomorrow.

    Here's the code:
    Code:
    Option Explicit
    
    Dim xml_document As New DOMDocument60
    Dim value_node As IXMLDOMNode
    Dim objX As New MSXML2.DOMDocument60
    Dim parentNode As IXMLDOMNode
    Dim strFileRoot As String
    
    Private Sub LoadValue(xmlDoc As String)
    xml_document.Load xmlDoc
    If xml_document.documentElement Is Nothing Then
    Dim strErrText As String
    Dim xPE As MSXML2.IXMLDOMParseError
    Set xPE = objX.parseError
    With xPE
    strErrText = "Your XML Document failed to load" & _
    "due to the following error." & vbCrLf & _
    "Error #:" & .errorCode & ":" & .reason & _
    "Line #:" & .Line & vbCrLf & _
    "Line Position" & .linepos & vbCrLf & _
    "Position in File:" & .filepos & vbCrLf & _
    "Source Text:" & .srcText & vbCrLf & _
    "Document URL:" & .url
    End With
    MsgBox strErrText, vbExclamation
    End If
    End Sub
    
    Private Sub setRoot(rootElement As String)
    Set parentNode = xml_document.selectSingleNode(rootElement)
    End Sub
    
    Private Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, _
    ByVal node_name As String, _
    Optional ByVal default_value As String) _
    As String
    
    Set value_node = start_at_node.selectSingleNode(".//" & node_name)
    If value_node Is Nothing Then
    GetNodeValue = default_value
    Else
    GetNodeValue = value_node.Text
    End If
    End Function
    
    Private Sub cmdSelectXML_Click()
    
    Dim strFileName As String
    
    
    objX.async = False
    
    CDL.Filter = "XML file (*.xml)|*.xml"
    CDL.DialogTitle = "Select the XML file"
    CDL.ShowOpen
    strFileName = CDL.FileName
    
    Screen.MousePointer = vbHourglass
    
    LoadValue (strFileName)
    setRoot "Dimap_Document"
    txtIDNumber.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtLatLong1.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtLatLong2.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtLatLong3.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtLatLong4.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtScene.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtHoriz.Text = GetNodeValue(parentNode, "Horizontal_CS_Type", "???")
    
    Screen.MousePointer = vbDefault
    Set objX = Nothing
    
    Exit Sub
    
    
    End Sub
    Last edited by Hack; Feb 13th, 2008 at 08:03 AM. Reason: Added Code Tags

  2. #2
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: XML Parsing Question

    Welcome to the Forums


    Dont have any knowledge in XML parsing , but you are getting ??? because you pass that as the default value to your GetNodeValue function. (Did you notice that?) So the problem may be that it cant find the given node ?
    The problem may be in that.

    See whether you get parentnode set in setRoot Function

    This is where i fails I think in GetNodeValue

    Code:
    If value_node Is Nothing Then
    GetNodeValue = default_value
    Else
    GetNodeValue = value_node.Text
    End If
    But the problem root may be SetRoot.


    And you are retrieving same thing for all text boxes

    Code:
    txtIDNumber.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtLatLong1.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtLatLong2.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtLatLong3.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtLatLong4.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    txtScene.Text = GetNodeValue(parentNode, "Dataset_ID", "???")
    is that what you need ?

    IIF(Post.Rate > 0 , , )

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: XML Parsing Question

    As zeezee stated, your GetNodeValue function is returning the default value. If you post the xml file it should be difficult to find the problem.

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: XML Parsing Question

    Your not getting the nodes and returning default value. Check your XML and XPath.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Re: XML Parsing Question

    Here's The XML File...
    What do you think?

    <?xml version="1.0" ?>
    - <Dimap_Document>
    - <Metadata_Id>
    <METADATA_FORMAT version="1.0">DIMAP</METADATA_FORMAT>
    <METADATA_PROFILE>SPOTSCENE_1A</METADATA_PROFILE>
    <METADATA_GUY>SWITS</METADATA_GUY>
    </Metadata_Id>
    - <Dataset_Id>
    <DATASET_NAME>SCENE 5 5349 02/08/25 17:15:55 2 T</DATASET_NAME>
    <COPYRIGHT>COPYRIGHT CNES 25 08 2002 17 H 15 MN 55 S</COPYRIGHT>
    <DATASET_QL_PATH href="PREVIEW.JPG" />
    <DATASET_QL_FORMAT version="6.a.1">JPEG</DATASET_QL_FORMAT>
    <DATASET_TN_PATH href="ICON.JPG" />
    <DATASET_TN_FORMAT>JPEG</DATASET_TN_FORMAT>
    </Dataset_Id>
    - <Dataset_Frame>
    - <Vertex>
    <FRAME_LON>-87.035166</FRAME_LON>
    <FRAME_LAT>50.251936</FRAME_LAT>
    <FRAME_ROW>1</FRAME_ROW>
    <FRAME_COL>1</FRAME_COL>
    </Vertex>
    - <Vertex>
    <FRAME_LON>-86.139997</FRAME_LON>
    <FRAME_LAT>50.047374</FRAME_LAT>
    <FRAME_ROW>1</FRAME_ROW>
    <FRAME_COL>24000</FRAME_COL>
    </Vertex>
    - <Vertex>
    <FRAME_LON>-86.473785</FRAME_LON>
    <FRAME_LAT>49.536392</FRAME_LAT>
    <FRAME_ROW>24000</FRAME_ROW>
    <FRAME_COL>24000</FRAME_COL>
    </Vertex>
    - <Vertex>
    <FRAME_LON>-87.315299</FRAME_LON>
    <FRAME_LAT>49.739149</FRAME_LAT>
    <FRAME_ROW>24000</FRAME_ROW>
    <FRAME_COL>1</FRAME_COL>
    </Vertex>
    - <Scene_Center>
    <FRAME_LON>-86.736234</FRAME_LON>
    <FRAME_LAT>49.896207</FRAME_LAT>
    <FRAME_ROW>12000</FRAME_ROW>
    <FRAME_COL>12000</FRAME_COL>
    </Scene_Center>
    <SCENE_ORIENTATION>27.611721</SCENE_ORIENTATION>
    </Dataset_Frame>
    - <Coordinate_Reference_System>
    <GEO_TABLES version="5.2">EPSG</GEO_TABLES>
    - <Horizontal_CS>
    <HORIZONTAL_CS_CODE>epsg:4326</HORIZONTAL_CS_CODE>
    <HORIZONTAL_CS_TYPE>GEOGRAPHIC</HORIZONTAL_CS_TYPE>
    <HORIZONTAL_CS_NAME>WGS 84</HORIZONTAL_CS_NAME>
    </Horizontal_CS>
    </Coordinate_Reference_System>
    </Dimap_Document>

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Re: XML Parsing Question

    And, the textboxes are all set to the same thing because, well, I didn't want to put in all of the values when all I was getting was ???'s. I'll change them after I get the first one working.

    Please help...

  7. #7
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: XML Parsing Question

    Is XML case sensitive?
    Ok your original code didnt work
    but this worked

    Code:
    GetNodeValue(parentNode, "Dataset_Id", "???")
    See the difference in the node name? I copied it from the xml document and pasted. now it works. Dont know why, but its like that.

    Anyway the xml code you have posted here has - which is wrong also. paste the correct xml document or attach it.

    IIF(Post.Rate > 0 , , )

  8. #8
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: XML Parsing Question

    Yes XML is case sensitive, observe naming conventions. The dashes in his post represent expanded nodes (e.g. treeview).

    For more info:
    http://www.w3schools.com/xml/default.asp
    http://www.w3schools.com/xpath/default.asp

  9. #9
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: XML Parsing Question

    The dashes in his post represent expanded nodes
    Oh yeah. I think he just pasted it when it was shown in the IE.
    well you need to remove the dashes before putting it in the XML file.
    I validated it and told there was an error, so then only i saw the dahes.
    Removing it worked

    http://www.w3schools.com/dom/dom_validate.asp

    Never used XML before.

    IIF(Post.Rate > 0 , , )

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2008
    Posts
    4

    Re: XML Parsing Question

    Thanks guys... awesome help!

    Now, I have one more quick question-

    There's 4 different elements named "Vertex", and I need to make the "FRAME_LON" and "FRAME_LAT" values from each of them parse into seperate text boxes.

    These bold numbers need to go into txtLatLong1:
    <Vertex>
    <FRAME_LON>-87.035166</FRAME_LON>
    <FRAME_LAT>50.251936</FRAME_LAT>
    <FRAME_ROW>1</FRAME_ROW>
    <FRAME_COL>1</FRAME_COL>
    </Vertex>

    These bold numbers need to go into txtLatLong2:
    <Vertex>
    <FRAME_LON>-86.139997</FRAME_LON>
    <FRAME_LAT>50.047374</FRAME_LAT>
    <FRAME_ROW>1</FRAME_ROW>
    <FRAME_COL>24000</FRAME_COL>
    </Vertex>

    These bold numbers need to go into txtLatLong3:
    <Vertex>
    <FRAME_LON>-86.473785</FRAME_LON>
    <FRAME_LAT>49.536392</FRAME_LAT>
    <FRAME_ROW>24000</FRAME_ROW>
    <FRAME_COL>24000</FRAME_COL>
    </Vertex>

    And these bold numbers need to go into txtLatLong4.
    <Vertex>
    <FRAME_LON>-87.315299</FRAME_LON>
    <FRAME_LAT>49.739149</FRAME_LAT>
    <FRAME_ROW>24000</FRAME_ROW>
    <FRAME_COL>1</FRAME_COL>
    </Vertex>

    Since the elements are all named the same, how do I get the proper numbers into the proper text boxes?

    Thanks!
    Last edited by magyar123; Feb 14th, 2008 at 06:06 AM.

  11. #11
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: XML Parsing Question

    To be honest, I dont know how to do it.
    I havent done any XML parsing before.
    Hope someone would help you in that though.

    IIF(Post.Rate > 0 , , )

  12. #12
    Hyperactive Member Arachnid13's Avatar
    Join Date
    Jan 2003
    Location
    England
    Posts
    327

    Re: XML Parsing Question

    You could use the Microsoft XML Library, load the xml document into a DOMDocument, and use XPath to get the relevant nodes like this:

    vb Code:
    1. Dim theDoc as MSXML2.DOMDocument
    2.  
    3. Set theDoc = New MSXML2.DOMDocument
    4.  
    5. theDoc.Load "[path to your xml document]"
    6.  
    7. txtLatLong1 = theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[0]/FRAME_LON").Text & ", " & theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[0]/FRAME_LAT").Text
    8. txtLatLong2 = theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[1]/FRAME_LON").Text & ", " & theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[1]/FRAME_LAT").Text
    9. txtLatLong3 = theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[2]/FRAME_LON").Text & ", " & theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[2]/FRAME_LAT").Text
    10. txtLatLong4 = theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[3]/FRAME_LON").Text & ", " & theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[3]/FRAME_LAT").Text
    11.  
    12. Set theDoc = Nothing

    If your text boxes were an array you could use GetNodes instead of GetSingleNode and loop throught the returned NodeSet, however with only four textboxes it wouldnt make much difference
    Do you wake up in the morning feeling sleepy and grumpy? Then you must be Snow White

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width