-4

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.

What is the best way to do that? Someone suggested to write a shell script to do that since we deal with large data.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
M.Talafha
  • 7
  • 6
  • 2
    Can you give an example of an XML sample and the corresponding C++ you want to create? Do you have a schema for those XML samples and want to map the data to C++ classes? There are products doing that, like http://www.codesynthesis.com/products/xsd/. – Martin Honnen Nov 10 '15 at 19:16
  • @MartinHonnen XML file contains the reaction rates for nuclei in stars what do you mean by schema for xml sorry i'm physicist – M.Talafha Nov 10 '15 at 19:24
  • 1
    You can't *convert XML to C++*. XML contains data, not source code. – Ken White Nov 10 '15 at 19:32
  • What do you mean by convert XML to C++? Do you want to read the XML files from C++ code? If so, CodeSynthesis XSD mentioned in the earlier comment is one option, or look at [this](http://stackoverflow.com/q/9387610/241631). Or you might be looking for the [ODB](http://www.codesynthesis.com/products/odb/) product from CodeSynthesis. – Praetorian Nov 10 '15 at 19:33
  • Probably a better solution would be to embed the XML files as resources into the C++ program. And then have an XML library parse the data out. What are you ***really trying to do***? – selbie Nov 10 '15 at 19:37
  • @KenWhite i need to extract the data from XMl files then to rebuild by script in C++ – M.Talafha Nov 10 '15 at 19:41
  • @Praetorian No the xml file contains data i want to extract them and rebuild them by c++ in a tree – M.Talafha Nov 10 '15 at 19:46
  • 1
    @selbie im trying to make a c++ program computing many things and depends on data which is already i had as an xml files by converting the data to a tree inside the terminal using the C++ – M.Talafha Nov 10 '15 at 19:53
  • No, you can't *convert the XML to a tree inside the terminal using the C++*, whatever that means – Praetorian Nov 10 '15 at 19:57
  • Please provide an example of what you want. – mzu Nov 10 '15 at 20:12
  • @M.Talafha Read a beginners book about C++. There's too much to learn to press it in an answer here. – deviantfan Nov 10 '15 at 20:16

2 Answers2

1

Your best method for reading an XML data file from a C++ program is to use an XML library. Search the internet for "C++ XML library".

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
1

Option 1 - leave the data in the XML files and read it out at run time. (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. Perhaps the filenames of those XML files can be passed as a command line to your program.

Then your program "reads" these XML files and parses them into a tree structure. There are a number of existing XML libraries that do all heavy parsing for you. One such library is 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.

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. That will turn a file into an array of unsigned chars in C code. 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.

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.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • I was thinking about this, but we don't know the OP's data layout and there would be a lot of global variables. Also, encoding the relationship to these variables would be a lot of work. – Thomas Matthews Nov 10 '15 at 20:21
  • @selbie i have three several things first of all the Root program by CERN and the second is orders written in terminal and the third is a big data base as an xml files i need them together in one scripts which will be written in c++ so i need to construct a tree inside root which will contain the data and by the orders which i already have do calculation that is my work – M.Talafha Nov 11 '15 at 06:39