removing encoding attribute from xml using boost

2019-07-25 09:07发布

问题:

<?xml version="1.0" encoding="utf-8"?>
<methodCall><methodName>number</methodName></methodCall>

    boost::property_tree::ptree assuranceprofilelookup_node;
    assuranceprofilelookup_node.put("methodCall.methodName", method_name);
    write_xml("AssuranceProfileLookup.xml", assuranceprofilelookup_node);
    assuranceprofilelookup_node.find("xml")->second.erase("<xmlattr>");

How to remove encoding from above program I am trying to erase it but giving seg fault

Few examples I got but it is not using ptree, if possible I want to delete from ptree element

回答1:

You do not want to remove it from a ptree element. Because it was never part of an element.

It's part of an XML document, and removing it makes it less of an XML document: http://xmlwriter.net/xml_guide/xml_declaration.shtml

Really, wanting to remove the XML declaration is a sure sign someone doesn't understand XML and how it should be used.

If you don't mind using an undocumented function, you can write a single element, so you won't see the XML declaration:

Live On Coliru

#include <boost/property_tree/xml_parser.hpp>
#include <iostream>

int main() {
    boost::property_tree::ptree pt;
    {
        std::istringstream iss("<methodCall><methodName>number</methodName></methodCall>");
        read_xml(iss, pt);
    }

    // write_xml(std::cout, pt); // with xml declaration:

    // without
    boost::property_tree::xml_parser::write_xml_element(std::cout, pt.front().first, pt.front().second, 0, boost::property_tree::xml_writer_make_settings<std::string>(' ', 0));
}

Prints

<methodCall><methodName>number</methodName></methodCall>


标签: xml boost