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>
12
  • You forgot to provide a specific example of the XML file. Please, edit the question and specify this. Commented Feb 27, 2012 at 22:26
  • Thanks @DimitreNovatchev : just provided the example. Commented Feb 27, 2012 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. Commented Feb 27, 2012 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. Commented Feb 27, 2012 at 23:35
  • hari: my strong recommendation is not to waste any time on this and to use a standard .config file. Commented Feb 27, 2012 at 23:46

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.