Re: XML Question [message #42816 is a reply to message #42815] |
Wed, 23 February 2005 09:23   |
Karl Schultz
Messages: 341 Registered: October 1999
|
Senior Member |
|
|
On Tue, 22 Feb 2005 16:25:53 -0700, David Fanning wrote:
> Folks,
>
> I think I am beginning to understand how people feel when
> they start to learn about objects for the first time.
> That is to say, the IDL documentation is just about
> impossible.
>
> I'm trying to get up to speed on XML files very quickly
> (that is, I needed this yesterday). I'm looking at the
> documentation and my anxiety is increasing exponentially.
> Does anyone have a fairly simple example of reading an
> XML file and doing something useful with the information?
>
> I would like, for example, to read a configuration file
> with parameters and their associated values. I would like
> to build a widget that would allow the user to change the
> values, and finally I would like to write the configuration
> file back to an XML file. Does anyone have anything remotely
> like that?
>
> I see to be floundering in IDLffDOM minutiae. :-(
>
> Cheers,
>
> David
XML DOM is pretty complicated, so it takes awhile to learn.
Rather than tackle the "write-back" part at the same time, let's just work
on building the widget app from a config file.
Here's a sample config file. To really make it solid from an XML point of
view, one needs to create a DTD or a schema to describe the data and
enforce it during parsing. If you let the parser enforce the schema, you
can greatly simplify your code that walks the DOM tree, since you don't
have to prepare for every possible input possibility. For now, we'll
assume that the input xml file is "legal" and we'll make a lot of
assumptions when parsing it.
<?xml version='1.0' encoding='us-ascii'?>
<demo title="XMLDOM Sample">
<widget type="slider" min="0" max="20">
<value>13</value>
</widget>
<widget type="text">
<value>Hello</value>
</widget>
</demo>
I purposely made the "value" tags XML Elements instead of attributes on
the "widget" Element because this would allow widget data to be specified
in a more flexible way. For example, you can make put an "array" tag
instead of a "value" tag that specifies the contents for a table widget.
Here's an IDL program that reads the above config file and builds a
super-simple widget app.
pro demo
oDoc = OBJ_NEW('IDLffXMLDOMDocument', FILENAME='demo.xml')
oTopLevel = oDoc->GetDocumentElement()
oWidgetList = oTopLevel->GetElementsByTagName("widget")
wBase = WIDGET_BASE(/COL, TITLE=oTopLevel->GetAttribute("title"))
for i=0, oWidgetList->GetLength()-1 do begin
oWidget = oWidgetList->Item(i)
case oWidget->GetAttribute("type") OF
'slider': begin
wSlider = WIDGET_SLIDER(wBase, $
MINIMUM=FIX(oWidget->GetAttribute("min")), $
MAXIMUM=FIX(oWidget->GetAttribute("max")))
oValueList = oWidget->GetElementsByTagName("value")
oValue = oValueList->Item(0)
WIDGET_CONTROL, wSlider, $
SET_VALUE=FIX((oValue->GetFirstChild())->GetNodeValue())
end
'text': begin
wText = WIDGET_TEXT(wBase)
oValueList = oWidget->GetElementsByTagName("value")
oValue = oValueList->Item(0)
WIDGET_CONTROL, wText, $
SET_VALUE=(oValue->GetFirstChild())->GetNodeValue()
end
else: print, "Unknown widget type"
endcase
endfor
OBJ_DESTROY, oDoc
WIDGET_CONTROL, wBase, /REALIZE
end
Hope this helps. I can help further, if needed.
Karl
|
|
|