|
-
Feb 12th, 2008, 10:59 PM
#1
Thread Starter
New Member
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
-
Feb 13th, 2008, 08:38 AM
#2
Frenzied Member
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 ?
-
Feb 13th, 2008, 01:47 PM
#3
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.
-
Feb 13th, 2008, 10:14 PM
#4
Re: XML Parsing Question
Your not getting the nodes and returning default value. Check your XML and XPath.
-
Feb 14th, 2008, 12:13 AM
#5
Thread Starter
New Member
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>
-
Feb 14th, 2008, 12:19 AM
#6
Thread Starter
New Member
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...
-
Feb 14th, 2008, 01:45 AM
#7
Frenzied Member
-
Feb 14th, 2008, 02:46 AM
#8
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
-
Feb 14th, 2008, 03:28 AM
#9
Frenzied Member
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. 
-
Feb 14th, 2008, 05:54 AM
#10
Thread Starter
New Member
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.
-
Feb 14th, 2008, 07:47 AM
#11
-
Feb 14th, 2008, 08:04 AM
#12
Hyperactive Member
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:
Dim theDoc as MSXML2.DOMDocument Set theDoc = New MSXML2.DOMDocument theDoc.Load "[path to your xml document]" txtLatLong1 = theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[0]/FRAME_LON").Text & ", " & theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[0]/FRAME_LAT").Text txtLatLong2 = theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[1]/FRAME_LON").Text & ", " & theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[1]/FRAME_LAT").Text txtLatLong3 = theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[2]/FRAME_LON").Text & ", " & theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[2]/FRAME_LAT").Text txtLatLong4 = theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[3]/FRAME_LON").Text & ", " & theDoc.SelectSingleNode("Dimap_Document/Dataset_Frame/Vertex[3]/FRAME_LAT").Text 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|