|
I think something is missing in xmlpp::Element. There is no way to
create an xmlpp::EntityReference or an
xmlpp::ProcessingInstructionNode. (They can be created when you
parse an xml file, but I can find no way to create an xml file with
those nodes without resorting to functions in the underlying
libxml2.) In the attached small program I have added an add_child_entity_reference() function. Then it's possible to write the file you want. I recommend that you file a bug on libxml++. Kjell 2012-01-26 10:35, carlo esposito skrev:
|
// Highly modified version of libxml++/examples/dom_build/main.cc
#include <libxml++/libxml++.h>
#include <libxml/tree.h>
#include <iostream>
// Wanted xml file:
// 1 <? Xml version = "1.0"?>
// 2 <! DOCTYPE EXAMPLE SYSTEM "example.dtd" [
// 3 <! ENTITY xml "Extensible Markup Language">
// 4]>
// 5 <example>
// 6 &xml;
// 7 </example>
namespace xmlpp
{
// Should be a member of xmlpp::Element, I think.
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("\n");
xmlpp::add_child_entity_reference(nodeRoot, "xml");
nodeRoot->add_child_text("\n");
Glib::ustring whole = document.write_to_string();
std::cout << "XML built at runtime: " << std::endl << whole << std::endl;
document.write_to_file("example.xml");
}
catch(const std::exception& ex)
{
std::cout << "Exception caught: " << ex.what() << std::endl;
return 1;
}
return 0;
}