2014-11-02 53 views
-4

我工作在一个C项目上,需要实现一些与XML文件的交互。需要一个非常简单的例子来解析和编写C语言或C++中的XML文档

我需要一个非常简单的解析和写入XML文件的例子。

我真的很喜欢小库(如一个.H文件和一个.C文件),而不是一个库,需要安装

我会很高兴,如果有人能告诉我:

  • 如何解析在下面的例子中的数据
  • 如何在下面的例子中生成的数据

XML数据:

<root> 
    <source name = 'source1' isReadOnly = 'false'> 
     <view name = 'view1' /> 
     <view name = 'view2' /> 
    </source> 
    <source name = 'source2' isReadOnly = 'true'> 
     <view name = 'view1' /> 
     <view name = 'view2' /> 
    </source> 
</root> 
+1

http://pugixml.org/ – Galik 2014-11-02 13:54:43

+1

升压属性树可以用于解析XML。 http://www.boost.org/doc/libs/1_56_0/doc/html/property_tree.html - 我认为它只是标题。 – 2014-11-02 14:00:33

+3

_“我真的更喜欢小型图书馆(比如一个.h文件和一个.c文件)”_你可能在这里有一个很大的误解。正确解析XML是一项复杂得多的任务,可以合理解决单个模块中的问题。 – 2014-11-02 14:01:57

回答

1

我发现pugixml是非常容易使用。它来自一个小的源文件,您可以编译到您的项目中。

C++ 03

#include <fstream> 
#include <iostream> 
#include "pugixml.hpp" 

int main() 
{ 
    using namespace pugi; 

    // Load XML file from fstream 

    std::ifstream xml_file("test.xml"); 

    if(!xml_file) 
    { 
     std::cerr << "ERROR: opening XML file: " << std::endl; 
     return 1; 
    } 

    xml_document doc; 

    xml_parse_result res = doc.load(xml_file); 

    if(!res) 
    { 
     std::cerr << "ERROR: " << res.description() << std::endl; 
     return 1; 
    } 

    // get all children of <root><source> 

    xml_object_range<xml_named_node_iterator> sources = 
     doc.child("root").children("source"); 

    // Iterate through <root><source> children 

    xml_named_node_iterator s; 
    for(s = sources.begin(); s != sources.end(); ++s) 
    { 
     // get all children named <root><source><view> 

     xml_object_range<xml_named_node_iterator> views = 
      s->children("view"); 

     // Iterate through <root><source><view> children 

     xml_named_node_iterator v; 
     for(v = views.begin(); v != views.end(); ++v) 
      std::cout << v->attribute("name").value() << '\n'; 
    } 
} 

C++ 11

#include <fstream> 
#include <iostream> 
#include "pugixml.hpp" 

int main() 
{ 
    using namespace pugi; 

    std::ifstream xml_file("test.xml"); 

    if(!xml_file) 
    { 
     std::cerr << "ERROR: opening XML file: " << std::endl; 
     return 1; 
    } 

    xml_document doc; 

    xml_parse_result res = doc.load(xml_file); 

    if(!res) 
    { 
     std::cerr << "ERROR: " << res.description() << std::endl; 
     return 1; 
    } 

    auto sources = doc.child("root").children("source"); 

    for(auto&& s: sources) 
    { 
     auto views = s.children("view"); 

     for(auto&& v: views) 
      std::cout << v.attribute("name").value() << '\n'; 
    } 
} 
+0

你的例子太棒了!只需很少的努力就能完成工作 – SomethingSomething 2014-11-03 16:36:35

1

有互联网上对此多篇。假设你是在Windows找到下面的例子供大家参考: -

<?xml version="1.0" encoding="UTF-8"?> 
<Car> 
    <Wheels> 
     <Wheel1>FL</Wheel1> 
     <Wheel2>FR</Wheel2> 
     <Wheel3>RL</Wheel3> 
     <Wheel4>RR</Wheel4> 
    </Wheels> 
</Car> 

下面是代码: -

#include <stdio.h> 
#include <tchar.h> 
#include <windows.h> 
#import <msxml6.dll> rename_namespace(_T("MSXML")) 

int main(int argc, char* argv[]) 
{ 
    HRESULT hr = CoInitialize(NULL); 
    if (SUCCEEDED(hr)) 
    { 
     try 
     { 
      MSXML::IXMLDOMDocument2Ptr xmlDoc; 
      hr = xmlDoc.CreateInstance(__uuidof(MSXML::DOMDocument60), NULL, CLSCTX_INPROC_SERVER); 
      // TODO: if (FAILED(hr))... 

      if (xmlDoc->load(_T("input.xml")) != VARIANT_TRUE) 
      { 
       printf("Unable to load input.xml\n"); 
      } 
      else 
      { 
       printf("XML was successfully loaded\n"); 

       xmlDoc->setProperty("SelectionLanguage", "XPath"); 
       MSXML::IXMLDOMNodeListPtr wheels = xmlDoc->selectNodes("/Car/Wheels/*"); 
       printf("Car has %u wheels\n", wheels->Getlength()); 

       MSXML::IXMLDOMNodePtr node; 
       node = xmlDoc->createNode(MSXML::NODE_ELEMENT, _T("Engine"), _T("")); 
       node->text = _T("Engine 1.0"); 
       xmlDoc->documentElement->appendChild(node); 
       hr = xmlDoc->save(_T("output.xml")); 
       if (SUCCEEDED(hr)) 
        printf("output.xml successfully saved\n"); 
      } 
     } 
     catch (_com_error &e) 
     { 
      printf("ERROR: %ws\n", e.ErrorMessage()); 
     } 
     CoUninitialize(); 
    } 
    return 0; 
} 

您还可以参考以下链接: -

http://codeproject.com/Articles/587488/Streaming-XML-parser-in-Cplusplus

http://msdn.microsoft.com/en-us/library/ms765540(v=vs.85).aspx

与此相反if你是在Linux平台上,然后去TinyXml的: -

http://grinninglizard.com/tinyxml/

+0

我需要linux。我会看看tinyXML。谢谢! – SomethingSomething 2014-11-02 14:02:54