2

I have 5 different key/value pairs and I know how my xml file should look. How can I create an xml file out of it? 1) I can always do printf() - not preferable. 2) Can I use xslt (stylesheet (.xsl) file) to do that?

And, I also want to be able to do the reverse operation of what I just mentioned. Given an xml file, I want to extract those 5 key/value pairs. I guess, I would need xsl file for this operation for sure.

I just need a starting point in terms of whether is doable or not. I can code myself in C.

Sample xml file:

<element1 type="type1" name="value1">
  <start play="no"/>
  <element2 aaa="AAA"/>
  <element2 bbb="BBB"/>
  <element3 ccc="CCC">
     <element4/><!-- play="no"/>-->
  </element3>
</element1>
hari
  • 9,439
  • 27
  • 76
  • 110
  • You forgot to provide a specific example of the XML file. Please, edit the question and specify this. – Dimitre Novatchev Feb 27 '12 at 22:26
  • Thanks @DimitreNovatchev : just provided the example. – hari Feb 27 '12 at 22:33
  • hari: This XML can hardly be called "format" -- there is almost no structure and the element names are meaningless. One should pay much attention to the format of the XML file -- otherwise a bad design will affect the whole project. I expected something at least as meaningful as a .config file. Speaking of which -- there are usually facilities of a given OS for working with config files -- you shouldn't be wasting precious time in re-inventing a worse format -- unless there is a very good and well-understood reason for this. – Dimitre Novatchev Feb 27 '12 at 22:45
  • @DimitreNovatchev I understand your concern and its apparent that I do not want to disclose actual node/values. I just wanted to get an idea/jump start. Thanks though. – hari Feb 27 '12 at 23:35
  • hari: my strong recommendation is not to waste any time on this and to use a standard .config file. – Dimitre Novatchev Feb 27 '12 at 23:46
  • @DimitreNovatchev : I cannot. As I *have* to spit out and consume xml files. – hari Feb 28 '12 at 00:06
  • harri: Actually you *can* decide the format of the XML file and *can* choose a config file format. The result of this decision is that you *can* and almost certainly *have to* use all already existing support for config files, which gives you free code solution and you don't have to reinvent the wheel. Should you feel devastated and empty with this result, you still can do a volunteering day for your district community :) – Dimitre Novatchev Feb 28 '12 at 00:11
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/8279/discussion-between-hari-and-dimitre-novatchev) – hari Feb 28 '12 at 00:13
  • @DimitreNovatchev I am not able to get your point. You mean, I can use "config" format (which I do not understand) and still spit out and consume xml files? – hari Feb 28 '12 at 00:18
  • hari: any "config file" is an XML file. A "config file" is not only an XML file, but it has a well-defined format. Because of this, there is usually support (ready code that you don't have to write again and again) at the OS level for creating, reading and updating a config file. The format of a "config file" mainly serves the purpose of specifying and maintaining key-value pairs. It can be regarded as a "XML-ized" version of the "ini files" that we were using in the past. So, if you want just to create/read/modify an XML file that contains name-value pairs, this has already been done for you. – Dimitre Novatchev Feb 28 '12 at 00:26
  • @DimitreNovatchev Thanks. Can you please provide an example for me to look at? any example from freebsd/linux would do. appreciate your help. – hari Feb 28 '12 at 00:34
  • hari: I have very superficial knowledge of linux and the chances are its config files aren't in XML format, as linux existed long before XML. If you search for: "XML config file format" you'll get a lot of useful examples. – Dimitre Novatchev Feb 28 '12 at 00:50

1 Answers1

1

Sounds like you're looking for libxml2, which allows you to read and write XML from C. It's very easy to use, and has several xml writer examples.

If you want to read XML in, take a look at their xml reader examples, or their parsing examples.

If you want to do XSLT from C, you can also use libxslt, which is built on top of libxml2. Although, if you want to read the values into variables in your code, you'd probably be better off just with the libxml2 parsers. XSLT is better for transforming an XML file into another XML file.

Timothy Jones
  • 21,495
  • 6
  • 60
  • 90
  • Thanks Timothy, I will look into it. It seems, I need to create .xml file from the values using xmlwriter and when provided with an xml file, need to use xslt to understand/parse it to get values out of it. – hari Feb 27 '12 at 23:32