• <small id='ZKIcX'></small><noframes id='ZKIcX'>

    • <bdo id='ZKIcX'></bdo><ul id='ZKIcX'></ul>

        <i id='ZKIcX'><tr id='ZKIcX'><dt id='ZKIcX'><q id='ZKIcX'><span id='ZKIcX'><b id='ZKIcX'><form id='ZKIcX'><ins id='ZKIcX'></ins><ul id='ZKIcX'></ul><sub id='ZKIcX'></sub></form><legend id='ZKIcX'></legend><bdo id='ZKIcX'><pre id='ZKIcX'><center id='ZKIcX'></center></pre></bdo></b><th id='ZKIcX'></th></span></q></dt></tr></i><div id='ZKIcX'><tfoot id='ZKIcX'></tfoot><dl id='ZKIcX'><fieldset id='ZKIcX'></fieldset></dl></div>

        <tfoot id='ZKIcX'></tfoot>
      1. <legend id='ZKIcX'><style id='ZKIcX'><dir id='ZKIcX'><q id='ZKIcX'></q></dir></style></legend>

        使用 Boost 解析 XML 属性

        时间:2023-07-19

        <small id='nPwN0'></small><noframes id='nPwN0'>

      2. <i id='nPwN0'><tr id='nPwN0'><dt id='nPwN0'><q id='nPwN0'><span id='nPwN0'><b id='nPwN0'><form id='nPwN0'><ins id='nPwN0'></ins><ul id='nPwN0'></ul><sub id='nPwN0'></sub></form><legend id='nPwN0'></legend><bdo id='nPwN0'><pre id='nPwN0'><center id='nPwN0'></center></pre></bdo></b><th id='nPwN0'></th></span></q></dt></tr></i><div id='nPwN0'><tfoot id='nPwN0'></tfoot><dl id='nPwN0'><fieldset id='nPwN0'></fieldset></dl></div>

            • <bdo id='nPwN0'></bdo><ul id='nPwN0'></ul>
                <legend id='nPwN0'><style id='nPwN0'><dir id='nPwN0'><q id='nPwN0'></q></dir></style></legend>
                  <tbody id='nPwN0'></tbody>
                <tfoot id='nPwN0'></tfoot>

                  本文介绍了使用 Boost 解析 XML 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想与您分享一个问题,我在尝试使用 Boost 库(1.52.0 版)处理 C++ 中 XML 元素的某些属性时遇到的问题.给定以下代码:

                  I would like to share with you an issue I'm having while trying to process some attributes from XML elements in C++ with Boost libraries (version 1.52.0). Given the following code:

                  #define ATTR_SET ".<xmlattr>"
                  #define XML_PATH1 "./pets.xml"
                  
                  #include <iostream>
                  #include <string>
                  #include <boost/foreach.hpp>
                  #include <boost/property_tree/ptree.hpp>
                  #include <boost/property_tree/xml_parser.hpp>
                  
                  using namespace std;
                  using namespace boost;
                  using namespace boost::property_tree;
                  
                  const ptree& empty_ptree(){
                      static ptree t;
                      return t;
                  }
                  
                  int main() {
                      ptree tree;
                      read_xml(XML_PATH1, tree);
                      const ptree & formats = tree.get_child("pets", empty_ptree());
                      BOOST_FOREACH(const ptree::value_type & f, formats){
                          string at = f.first + ATTR_SET;
                          const ptree & attributes = formats.get_child(at, empty_ptree());
                          cout << "Extracting attributes from " << at << ":" << endl;
                          BOOST_FOREACH(const ptree::value_type &v, attributes){
                              cout << "First: " << v.first.data() << " Second: " << v.second.data() << endl;
                          }
                      }
                  }
                  

                  假设我有以下 XML 结构:

                  Let's say I have the following XML structure:

                  <?xml version="1.0" encoding="utf-8"?>
                  <pets>
                      <cat name="Garfield" weight="4Kg">
                          <somestuff/>
                      </cat>
                      <dog name="Milu" weight="7Kg">
                          <somestuff/>
                      </dog>
                      <bird name="Tweety" weight="0.1Kg">
                          <somestuff/>
                      </bird>
                  </pets>
                  

                  因此,我将得到的控制台输出将是下一个:

                  Therefore, the console output I'll get will be the next:

                  Extracting attributes from cat.<xmlattr>:
                  First: name Second: Garfield
                  First: weight Second: 4Kg
                  Extracting attributes from dog.<xmlattr>:
                  First: name Second: Milu
                  First: weight Second: 7Kg
                  Extracting attributes from bird.<xmlattr>:
                  First: name Second: Tweety
                  First: weight Second: 0.1Kg
                  

                  但是,如果我决定为从根节点开始放置的每个元素都使用一个通用结构(以便从它们的特定属性中识别它们),结果将完全改变.在这种情况下,这可能是 XML 文件:

                  However, if I decide to use a common structure for every single element laying down from the root node (in order to identify them from their specific attributes), the result will completely change. This may be the XML file in such case:

                  <?xml version="1.0" encoding="utf-8"?>
                  <pets>
                      <pet type="cat" name="Garfield" weight="4Kg">
                          <somestuff/>
                      </pet>
                      <pet type="dog" name="Milu" weight="7Kg">
                          <somestuff/>
                      </pet>
                      <pet type="bird" name="Tweety" weight="0.1Kg">
                          <somestuff/>
                      </pet>
                  </pets>
                  

                  输出如下:

                  Extracting attributes from pet.<xmlattr>:
                  First: type Second: cat
                  First: name Second: Garfield
                  First: weight Second: 4Kg
                  Extracting attributes from pet.<xmlattr>:
                  First: type Second: cat
                  First: name Second: Garfield
                  First: weight Second: 4Kg
                  Extracting attributes from pet.<xmlattr>:
                  First: type Second: cat
                  First: name Second: Garfield
                  First: weight Second: 4Kg
                  

                  似乎可以正确识别从根节点悬挂的元素数量,因为已经打印了三组属性.尽管如此,它们都是指第一个元素的属性......

                  It seems the number of elements hanging from the root node is being properly recognized since three sets of attributes have been printed. Nevertheless, all of them refer to the attributes of the very first element...

                  我不是 C++ 专家,对 Boost 也很陌生,所以这可能是我在哈希映射处理方面所缺少的东西......任何建议将不胜感激.

                  I'm not an expert in C++ and really new to Boost, so this might be something I'm missing with respect to hash mapping processing or so... Any advice will be much appreciated.

                  推荐答案

                  你程序的问题在这一行:

                  The problem with your program is located in this line:

                  const ptree & attributes = formats.get_child(at, empty_ptree());
                  

                  通过这一行,您要求从 pets 中获取子 pet. 并且您独立于 f您正在遍历.按照这篇文章,我猜你需要使用的是:

                  With this line you are asking to get the child pet.<xmlattr> from pets and you do this 3 times independently of whichever f you are traversing. Following this article I'd guess that what you need to use is:

                  const ptree & attributes = f.second.get_child("<xmlattr>", empty_ptree());
                  

                  适用于您的两个 xml 文件的完整代码是:

                  The full code, that works with both your xml files, is:

                  #define ATTR_SET ".<xmlattr>"
                  #define XML_PATH1 "./pets.xml"
                  
                  #include <iostream>
                  #include <string>
                  #include <boost/foreach.hpp>
                  #include <boost/property_tree/ptree.hpp>
                  #include <boost/property_tree/xml_parser.hpp>
                  
                  using namespace std;
                  using namespace boost;
                  using namespace boost::property_tree;
                  
                  const ptree& empty_ptree(){
                      static ptree t;
                      return t;
                  }
                  
                  int main() {
                      ptree tree;
                      read_xml(XML_PATH1, tree);
                      const ptree & formats = tree.get_child("pets", empty_ptree());
                      BOOST_FOREACH(const ptree::value_type & f, formats){
                          string at = f.first + ATTR_SET;
                          const ptree & attributes = f.second.get_child("<xmlattr>", empty_ptree());
                          cout << "Extracting attributes from " << at << ":" << endl;
                          BOOST_FOREACH(const ptree::value_type &v, attributes){
                              cout << "First: " << v.first.data() << " Second: " << v.second.data() << endl;
                          }
                      }
                  }
                  

                  这篇关于使用 Boost 解析 XML 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <bdo id='tov0b'></bdo><ul id='tov0b'></ul>

                        <i id='tov0b'><tr id='tov0b'><dt id='tov0b'><q id='tov0b'><span id='tov0b'><b id='tov0b'><form id='tov0b'><ins id='tov0b'></ins><ul id='tov0b'></ul><sub id='tov0b'></sub></form><legend id='tov0b'></legend><bdo id='tov0b'><pre id='tov0b'><center id='tov0b'></center></pre></bdo></b><th id='tov0b'></th></span></q></dt></tr></i><div id='tov0b'><tfoot id='tov0b'></tfoot><dl id='tov0b'><fieldset id='tov0b'></fieldset></dl></div>

                      1. <legend id='tov0b'><style id='tov0b'><dir id='tov0b'><q id='tov0b'></q></dir></style></legend>

                          <small id='tov0b'></small><noframes id='tov0b'>

                            <tbody id='tov0b'></tbody>
                          <tfoot id='tov0b'></tfoot>