[eiffel-users] Creation of XML file

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[eiffel-users] Creation of XML file

Ricardo Barrera Ramirez
Good night,

I am trying to make an XML file with Eiffel but after the program finished running, the XML file who has been created, contains 2x times the XML_DECLARATION I am using.
I followed the program with debugger trying to find what is the problem but until now I can't see it.

Maybe is a bug with XML_FORMATTER. Could you maybe look at it and tell me if I do something wrong?

Thanks in advantage.

Program :

note
       description: "XMLTest application root class"
       date: "$Date$"
       revision: "$Revision$"

class
       APPLICATION
inherit
       ARGUMENTS

create
       make

feature {NONE} -- Initialization
       make -- Run application.
              do
                     make_xml_file
              end

 -- Make XML file
 make_xml_file
       local
              l_xml_prolog: XML_DECLARATION
              l_doc: XML_DOCUMENT
              l_root: XML_ELEMENT
              l_node_bk_to_cstr_stmt: XML_ELEMENT
              l_ns_default: XML_NAMESPACE
              l_formatter: XML_FORMATTER
              l_file: FILE
              l_file_name: STRING
              l_doc_name: STRING_32
              l_ns_xsi: XML_NAMESPACE
              l_ns_xsd: XML_NAMESPACE
              l_ns_xmlns: XML_NAMESPACE

       do
              -- Document
              l_doc_name := "Document"
              create l_doc.make

              -- <?xml version="1.0" encoding="utf-8"?>
              create l_xml_prolog.make_in_document (l_doc, {XML_MARKUP_CONSTANTS}.xml_version_1_0, {GT_XML_DOCUMENT}.c_encoding, false)

              l_root := l_doc.root_element
              l_root.set_name (l_doc_name)

              create l_ns_default.make_default
              create l_node_bk_to_cstr_stmt.make (l_root, "BkToCstmrStmt", l_ns_default )
              l_root.put_first (l_node_bk_to_cstr_stmt)

              create l_formatter.make
              create l_file_name.make_from_string ("xmltest.xml" )
              create {PLAIN_TEXT_FILE}l_file.make_with_name (l_file_name)

              -- Open file
              l_file.open_write

              l_formatter.set_output_file (l_file)
              l_formatter.process_document (l_doc)

              -- Close file
              l_file.close
       end
end

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Content of xmltest.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<Document><BkToCstmrStmt></BkToCstmrStmt></Document>

--
You received this message because you are subscribed to the Google Groups "Eiffel Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
Visit this group at https://groups.google.com/group/eiffel-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [eiffel-users] Creation of XML file

Jocelyn.Fiat.ES
Indeed this is a bug in the xml_tree library.
For now, you could change your code to:

....
 make_xml_file -- Make XML file local l_xml_prolog: XML_DECLARATION l_doc: XML_DOCUMENT l_root: XML_ELEMENT l_node_bk_to_cstr_stmt: XML_ELEMENT l_ns_default: XML_NAMESPACE l_formatter: XML_FORMATTER l_file: FILE l_file_name: STRING l_doc_name: STRING_32 l_ns_xsi: XML_NAMESPACE l_ns_xsd: XML_NAMESPACE l_ns_xmlns: XML_NAMESPACE l_pretty_print: XML_PRETTY_PRINT_FILTER l_xmlns_generator: XML_XMLNS_GENERATOR t2e: XML_TREE_TO_EVENTS do -- Document l_doc_name := "Document" create l_doc.make -- <?xml version="1.0" encoding="utf-8"?> create l_xml_prolog.make_in_document (l_doc, {XML_MARKUP_CONSTANTS}.xml_version_1_0, "UTF-16", false) l_root := l_doc.root_element l_root.set_name (l_doc_name) create l_ns_default.make_default create l_node_bk_to_cstr_stmt.make (l_root, "BkToCstmrStmt", l_ns_default) l_root.put_first (l_node_bk_to_cstr_stmt) create l_file_name.make_from_string ("xmltest.xml") create {PLAIN_TEXT_FILE} l_file.make_with_name (l_file_name) -- Open file l_file.open_write create l_pretty_print.make_null l_pretty_print.set_output_file (l_file) create l_xmlns_generator.set_next (l_pretty_print)
create t2e.make (l_xmlns_generator) l_doc.process (t2e) -- Close file l_file.close end

For indented XML, you can use XML_INDENT_PRETTY_PRINT_FILTER for the l_pretty_print variable.

Thank you for catching this issue.
It is surprising that we haven't met this issue yet, but after looking at various code and project, I noticed XML_FORMATTER is used as simple solution, where xml_declaration is not provided (mostly to store data locally as XML like format).
And otherwise code uses directly class XML_TREE_TO_EVENTS or XML_CALLBACKS_FILTER_FACTORY.callbacks_pipe (...) ... as at first XML_FORMATTER was not available, or just code had more complex need when generating the XML.

But in the future, both solution will be working fine.
The correction may not be included in upcoming EiffelStudio 17.05 since it is almost out, but it will be definitally for the next release.
In the meantime, please use the solution provided in this answer.

Regards,
-- Jocelyn


On Fri, May 19, 2017 at 12:48 AM, Ricardo Barrera Ramirez <[hidden email]> wrote:
Good night,

I am trying to make an XML file with Eiffel but after the program finished running, the XML file who has been created, contains 2x times the XML_DECLARATION I am using.
I followed the program with debugger trying to find what is the problem but until now I can't see it.

Maybe is a bug with XML_FORMATTER. Could you maybe look at it and tell me if I do something wrong?

Thanks in advantage.

Program :

note
       description: "XMLTest application root class"
       date: "$Date$"
       revision: "$Revision$"

class
       APPLICATION
inherit
       ARGUMENTS

create
       make

feature {NONE} -- Initialization
       make -- Run application.
              do
                     make_xml_file
              end

 -- Make XML file
 make_xml_file
       local
              l_xml_prolog: XML_DECLARATION
              l_doc: XML_DOCUMENT
              l_root: XML_ELEMENT
              l_node_bk_to_cstr_stmt: XML_ELEMENT
              l_ns_default: XML_NAMESPACE
              l_formatter: XML_FORMATTER
              l_file: FILE
              l_file_name: STRING
              l_doc_name: STRING_32
              l_ns_xsi: XML_NAMESPACE
              l_ns_xsd: XML_NAMESPACE
              l_ns_xmlns: XML_NAMESPACE

       do
              -- Document
              l_doc_name := "Document"
              create l_doc.make

              -- <?xml version="1.0" encoding="utf-8"?>
              create l_xml_prolog.make_in_document (l_doc, {XML_MARKUP_CONSTANTS}.xml_version_1_0, {GT_XML_DOCUMENT}.c_encoding, false)

              l_root := l_doc.root_element
              l_root.set_name (l_doc_name)

              create l_ns_default.make_default
              create l_node_bk_to_cstr_stmt.make (l_root, "BkToCstmrStmt", l_ns_default )
              l_root.put_first (l_node_bk_to_cstr_stmt)

              create l_formatter.make
              create l_file_name.make_from_string ("xmltest.xml" )
              create {PLAIN_TEXT_FILE}l_file.make_with_name (l_file_name)

              -- Open file
              l_file.open_write

              l_formatter.set_output_file (l_file)
              l_formatter.process_document (l_doc)

              -- Close file
              l_file.close
       end
end

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Content of xmltest.xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<Document><BkToCstmrStmt></BkToCstmrStmt></Document>

--
You received this message because you are subscribed to the Google Groups "Eiffel Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
Visit this group at https://groups.google.com/group/eiffel-users.
For more options, visit https://groups.google.com/d/optout.



--
Jocelyn
------------------------------------------------------------------------
Eiffel Software
https://www.eiffel.com
Customer support: https://support.eiffel.com
User group: https://groups.google.com/forum/#!forum/eiffel-users
------------------------------------------------------------------------

--
You received this message because you are subscribed to the Google Groups "Eiffel Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
Visit this group at https://groups.google.com/group/eiffel-users.
For more options, visit https://groups.google.com/d/optout.
Loading...