From: Neal L. <ne...@3d...> - 2007-11-18 22:47:50
|
At 02:13 PM 11/18/2007, Eric Bezault wrote: >Bru...@ao... wrote: > > Eric: > > > > I am looking to read and XML file with Eiffel and it seems that your > > Gobo classes can do this. I looked at the examples for Gobo and I don't > > see any example that actually read AND USE an XML file. > > > > Can you point me to any example that show how to use Gobo to read the > > attributes of an XML file? Ideally, it would include the input XML file > > as well. Hi Bruce, The two approaches I've used are: 1) Create a descendent of XM_CALLBACKS and process the attributes as the parser sends them to your class. An example of this approach may be found at http://swete.svn.sourceforge.net/viewvc/swete/trunk/swete/test/swete_script_callbacks.e?view=markup 2) Use XM_TREE_CALLBACKS_PIPE to create the xml tree in memory and then obtain the information you are looking for from that tree. For example: update_virtual_domain_hosts: BOOLEAN is -- Update table containing all virtual domain hosts from file -- Returns true if table was updated -- false if there was a problem reading the file and the hash table was not updated local temp_virtual_domain_hosts: DS_HASH_TABLE [VIRTUAL_DOMAIN_HOST, STRING] file: KL_TEXT_INPUT_FILE hosts: DS_LIST [XM_ELEMENT] aliases: DS_LIST [XM_ELEMENT] new_host: VIRTUAL_DOMAIN_HOST host_element: XM_ELEMENT read_exception: BOOLEAN do if not read_exception then create temp_virtual_domain_hosts.make (15) create host_parser.make create callbacks.make host_parser.set_callbacks (callbacks.start) create file.make (configuration.virtual_hosts_file_name) file.open_read host_parser.parse_from_stream (file) hosts := callbacks.tree.document.root_element.elements from hosts.start until hosts.after loop host_element := hosts.item (hosts.index) create new_host new_host.set_host_name (host_element.attribute_by_name ("virtual_host_name").value) new_host.set_company (host_element.attribute_by_name ("company").value) new_host.set_company_abbreviation (host_element.attribute_by_name ("company_abbreviation").value) new_host.set_banner_text (host_element.attribute_by_name ("banner_text").value) new_host.set_url (host_element.attribute_by_name ("url").value) new_host.set_style_sheet (host_element.attribute_by_name ("style_sheet").value) new_host.set_logo (host_element.attribute_by_name ("logo").value) new_host.set_logo_width (host_element.attribute_by_name ("logo_width").value.to_integer) new_host.set_logo_height (host_element.attribute_by_name ("logo_height").value.to_integer) new_host.set_default_state (host_element.attribute_by_name ("default_state").value) new_host.set_required_fields_color (host_element.attribute_by_name ("required_fields_color").value) -- new_host.set_program_ready_message (host_element.attribute_by_name ("program_ready_message").value) new_host.set_faq_price_answer (host_element.attribute_by_name ("faq_price_answer").value) new_host.set_price (host_element.attribute_by_name ("price").value.to_integer) if equal (host_element.attribute_by_name ("use_ssl").value.as_upper, "YES") then new_host.set_use_ssl end -- if equal (host_element.attribute_by_name ("show_requirement_at_greeting").value.as_upper, "YES") then -- new_host.set_show_requirement_at_greeting -- end aliases := host_element.element_by_name ("aliases").elements from aliases.start until aliases.after loop temp_virtual_domain_hosts.force_last (new_host, aliases.item (aliases.index).text) aliases.forth end hosts.forth end virtual_domain_hosts.copy (temp_virtual_domain_hosts) file.close end Result := not read_exception rescue read_exception := True retry end host_parser: XM_EIFFEL_PARSER callbacks: XM_TREE_CALLBACKS_PIPE Here is the associated XML: <?xml version="1.0" encoding="UTF-8" ?> - <host_list xmlns="http://www.freesafetyprogram.com/hosts"> - <!-- Virtual Domain Host Content Author: Neal L Lester [nea...@3d...] date: $Date: 2005/03/07 14:45:35 $ revision: $Revision: 1.3.2.3 $ copyright: (c) Neal L Lester --> - <host virtual_host_name="www.mysafetyprogram.com" company="mysafetyprogram.com" company_abbreviation="MSP" banner_text="Click here for Bay Area safety consulting services" url="http://www.mysafetyprogram.com/resume.htm" style_sheet="fsp.css" logo="ff_hepa.gif" logo_width="56" logo_height="80" default_state="CA" required_fields_color="Green" faq_price_answer="Viewing the final created program costs $249. You may view, print, and modify the program (without limitation) for up to one year at no additional cost." price="249" use_ssl="yes"> - <aliases> <alias>www.mysafetyprogram.com</alias> <alias>mysafetyprogram.com</alias> <alias>www.freesafetyprogram.com</alias> <alias>freesafetyprogram.com</alias> <alias>default</alias> </aliases> </host> </host_list> Neal |