Re: How to use libxml++ to write a file in c ++ xml



I made some tests with the attached program. The file written with
  document.write_to_file_formatted("examplef.xml");
looks like so

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE EXAMPLE SYSTEM "example.dtd" [
<!ENTITY xml "Extensible Markup Language">
]>
<example>&xml;<example1>text1</example1><example2>text2</example2></example>

When I replace
  xmlpp::add_child_entity_reference(nodeRoot, "xml");
by
  nodeRoot->add_child_text("xml");
the result is (I exclude the first four lines from now on)

<example>xml<example1>text1</example1><example2>text2</example2></example>

When I instead delete
  xmlpp::add_child_entity_reference(nodeRoot, "xml");
the result is

<example>
<example1>text1</example1>
<example2>text2</example2>
</example>

When I return to the origin test program and change xmlsave.c the way you have done, the result is

<example>
&xml;
<example1>text1</example1>
<example2>text2</example2>
</example>

But you say that you get what you want, and you have said earlier that you want

<example>
&xml;
<example1>
          text
</ example1>
<example2>
          text
</ Example2>
</ example>

If that's actually what you get, I'm surprised that our results are so different. Is your test program very different from mine?

Apart from that, I think you will have to make a more detailed description in your bug 669028. The libxml people that will hopefully read it may not follow conversations on libxmlplusplus-list.

I also think that the behaviour of xmlNodeDumpOutputInternal() is deliberate, and that it's done like that because formatting shall never add characters that may be significant to the program that reads the written xml file. After all xml files are not made primarily to be read by humans, they are made to be read by computer programs.

Kjell

2012-01-30 21:05, carlo esposito skrev:

I followed your tips Kiel,

I downloaded the code of libxml2 vers. 2.6.26, and I changed into the module xmlsave.c, the method xmlNodeDumpOutputInternal.

Tthe problem is related to the setting of the "format" parameter:


format = ctxt-> format;
if (format == 1)

    {
tmp = cur-> children;
while (tmp! = NULL)

              {
if ((tmp-> type == XML_TEXT_NODE) | |
(tmp-> type == XML_CDATA_SECTION_NODE))

                     {
-------->>>>>>>>>> / / (Tmp-> type == XML_ENTITY_REF_NODE)) {
ctxt-> format = 0;
break;
}
tmp = tmp-> next;
}
}

I commented out the line :


(Tmp-> type == XML_ENTITY_REF_NODE)


and I compiled the libraryand installed, it seems that now everything is OK ........


What do you think?


// Highly modified version of libxml++/examples/dom_build/main.cc

#include <libxml++/libxml++.h>
#include <libxml/tree.h>
#include <iostream>

namespace xmlpp
{
// Should be a member of xmlpp::Element.
EntityReference* add_child_entity_reference(Element* element, const Glib::ustring& name)
{
  // A good implementation of add_child_entity_reference() ought to check
  // if xmlNewReference() or xmlNewCharRef() shall be called.
  xmlNode* node = xmlNewReference(element->cobj()->doc, (const xmlChar*)name.c_str());
  node = xmlAddChild(element->cobj(), node);
  Node::create_wrapper(node);
  return static_cast<EntityReference*>(node->_private);
}
} // end namespace xmlpp


int
main(int /* argc */, char** /* argv */)
{
  // Set the global C and C++ locale to the user-configured locale,
  // so we can use std::cout with UTF-8, via Glib::ustring, without exceptions.
  std::locale::global(std::locale(""));

  try
  {
    xmlpp::Document document;
    document.set_internal_subset("EXAMPLE", "", "example.dtd");
    document.set_entity_declaration("xml", xmlpp::XML_INTERNAL_GENERAL_ENTITY,
      "", "example.dtd", "Extensible Markup Language");

    xmlpp::Element* nodeRoot = document.create_root_node("example");

    //nodeRoot->add_child_text("xml");
    xmlpp::add_child_entity_reference(nodeRoot, "xml");
    xmlpp::Element* nodeChild1 = nodeRoot->add_child("example1");
    xmlpp::Element* nodeChild2 = nodeRoot->add_child("example2");

    nodeChild1->add_child_text("text1");
    nodeChild2->add_child_text("text2");

    Glib::ustring whole = document.write_to_string();
    Glib::ustring whole_formatted = document.write_to_string_formatted();
    std::cout << "XML built at runtime: " << std::endl << whole << std::endl;
    std::cout << "XML built at runtime, formatted: " << std::endl << whole_formatted << std::endl;
    document.write_to_file("example.xml");
    document.write_to_file_formatted("examplef.xml");
  }
  catch(const std::exception& ex)
  {
    std::cout << "Exception caught: " << ex.what() << std::endl;
    return 1;
  }
  return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]