使用Boost的财产树木添加XML头 [英] Add XML headers using Boost's property trees

查看:159
本文介绍了使用Boost的财产树木添加XML头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在一个XML读/写,我用Boost的财产树这样做。

I've been working on a XML reader/writer, and I used Boost's property trees to do so.

一切工作,只有一件事是缺少在输出文件:我想在文件的顶部添加两个标题标签。眼下,只有头是这个,通过Boost的自动写入 write_xml()功能:

Everything is working, only one thing is missing in the output file: I'd like to add two header tags at the top of the file. Right now, the only header is this one, automatically written by Boost's write_xml() function:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

不过,我想这两个如下的已有补充:

However, I'd like to add these two below the one already there:

<!-- Custom stylesheet -->
<?xml-stylesheet type="text/xsl" href="browser_view.xslt"?>
<!-- Authentic View -->
<?xmlspysps authentic_view.sps?>

有谁知道我怎么能做到这一点不与升压产生后编辑文件?

Does anyone know how I could do that without editing the file after generating it with Boost?

推荐答案

这个词是处理指令。而我pretty肯定你不能(他们为什么要实施?有没有加速XML库毕竟)。

The word is "processing instruction". And I'm pretty sure you can't (why would they implement that? There is no Boost Xml library after all).

在双重检查 xml_writer_settings 确实是有什么控制的处理指令(否则你AVE燮pressed他们,而不是打印整个$印刷p $ pamble自己)。

After double checking the xml_writer_settings there is indeed nothing that controls the printing of the processing instructions (otherwise you could ave suppressed them and instead printed the whole preamble yourself).

下面是我的作为在它PugiXML:

Here's my take on it with PugiXML:

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

#include <pugixml.hpp>

int main() {

    std::stringstream ss;

    {
        boost::property_tree::ptree pt;
        pt.add("demo", "bla");
        boost::property_tree::xml_parser::write_xml(ss, pt);
    }

    {
        pugi::xml_document doc;
        doc.load(ss);

        auto pi = doc.prepend_child(pugi::xml_node_type::node_pi);
        pi.set_name("xmlspysps");
        pi.set_value("authentic_view.sps");

        pi = doc.prepend_child(pugi::xml_node_type::node_pi);
        pi.set_name("xml-stylesheet");
        pi.set_value("type=\"text/xsl\" href=\"browser_view.xslt\"");

        doc.save_file("test.xml");
    }
}

保存:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="browser_view.xslt"?>
<?xmlspysps authentic_view.sps?>
<demo>bla</demo>

当然,如果你真的想只是一个序列化 ptree中这太没效率 - 但显然你不是的只是的序列化。你打了起来,在您需要的标记库,preferrably一个XML能够之一。

Of course that's horribly inefficient if you really want to just serialize a ptree - but apparently you're not just serializing. You're marking up, for which you need a markup-library, preferrably an XML capable one.

这篇关于使用Boost的财产树木添加XML头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆