简体   繁体   English

在 Linux 中使用终端将 XML 文件转换为 C++

[英]Converting XML files to c++ using terminal in Linux

I have large data stored as XML files and I need to convert them to C++ by using the gnome-terminal in Ubuntu 14.04.我将大量数据存储为 XML 文件,我需要使用 Ubuntu 14.04 中的 gnome-terminal 将它们转换为 C++。

What is the best way to do that?最好的方法是什么? Someone suggested to write a shell script to do that since we deal with large data.有人建议编写一个 shell 脚本来做到这一点,因为我们要处理大数据。

Your best method for reading an XML data file from a C++ program is to use an XML library.从 C++ 程序读取 XML 数据文件的最佳方法是使用 XML 库。 Search the internet for "C++ XML library".在 Internet 上搜索“C++ XML 库”。

Option 1 - leave the data in the XML files and read it out at run time.选项 1 - 将数据保留在 XML 文件中并在运行时读出。 (recommended) (受到推崇的)

What you probably want to do is leave your XML files on disk, and then write a program to read those files in at runtime.您可能想要做的是将 XML 文件保留在磁盘上,然后编写一个程序在运行时读取这些文件。 Perhaps the filenames of those XML files can be passed as a command line to your program.也许这些 XML 文件的文件名可以作为命令行传递给您的程序。

Then your program "reads" these XML files and parses them into a tree structure.然后您的程序“读取”这些 XML 文件并将它们解析为树结构。 There are a number of existing XML libraries that do all heavy parsing for you.有许多现有的 XML 库可以为您完成所有繁重的解析工作。 One such library is libxml .一个这样的库是libxml Then your astrophysics code does its work to analyze your data.然后您的天体物理学代码会分析您的数据。

Option 2 - "Use xxd -i" to convert XML into a C resource file.选项 2 - “使用 xxd -i”将 XML 转换为 C 资源文件。

If you absolutely want to convert XML to C++ code, then the best I can offer you is the xxd command with the -i option.如果您绝对想将 XML 转换为 C++ 代码,那么我可以为您提供的最好的方法是带有-i选项的xxd命令。 That will turn a file into an array of unsigned chars in C code.这会将文件转换为 C 代码中的无符号字符数组。 Example:例子:

ubuntu@ip-10-0-0-15:~$ cat foo.xml                                                                                   
<foo>                                                                                                                
   <data>This is some data</data>                                                                                    
</foo>                                                                                                               
ubuntu@ip-10-0-0-15:~$ xxd -i foo.xml                                                                                
unsigned char foo_xml[] = {0x3c, 0x66, 0x6f, 0x6f, 0x3e, 0x0a, 0x20, 0x20, 0x20, 0x3c, 0x64, 0x61, 0x74, 0x61, 0x3e, 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61, 0x3c, 0x2f, 0x64, 0x61, 0x74, 0x61, 0x3e, 0x0a, 0x3c, 0x2f, 0x66, 0x6f, 0x6f, 0x3e, 0x0a};
unsigned int foo_xml_len = 47;  

The unsigned char array, foo_xml is an array of ascii bytes of the original file.无符号字符数组foo_xml是原始文件的 ascii 字节数组。

At runtime, you can convert that unsigned char array back into a string (don't forget to append a null char).在运行时,您可以将该无符号字符数组转换回字符串(不要忘记附加一个空字符)。 Then use the XML library I mentioned above to parse the data.然后使用我上面提到的XML库来解析数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM