tags:

views:

84

answers:

2

Any one who knows how to create and update XML file in C?

+2  A: 

Use a library, such as libxml2.

There are numerous examples, depending on the particulars of what you are trying to accomplish.

WhirlWind
I have memory restrictions therefore I want to avoid this.
Punit
So, what is it that you *really* want to do?
WhirlWind
Is this the only way to write xml in C?So if I use fopen("file.xml", w) it will treat it as a text file right?
Punit
Sure, you can parse xml yourself without using a library. Just look for < and >, and build up a data structure based on them. But you really should make your question a bit more specific; how was I supposed to know you unstated requirements?
WhirlWind
Oh ok, sorry for that.Well in that case I did that and that works fine.Well if you see another post of mine "File upload progress bar", there I am using text file, so I thought to use the xml file. But using xml file also didn't solve the problem.So for xml I am doing this:char xmlDat1[] = "<DOCUMENT><PROGRESS>";char xmlDat2[] = "</PROGRESS></DOCUMENT>";fptr = fopen("progress_bar.xml", "w");fprintf(fptr, "%s", xmlDat1); fprintf(fptr, "%d" ,j); fprintf(fptr, "%s" ,xmlDat2); fseek(fptr, 0, SEEK_SET); fclose(fptr);
Punit
My client is able to read data from this xml file using request.responseXML.getElementsByTagName('PROGRESS')[0].firstChild;but xml file is also showing the same behavior as text file is doing.So, I though there is some other way to update xml file so that it is automatically available to client after each update.
Punit
@Punit, 'C' is a barebones language originally intended to make the Unix operating system portable to different architectures. It's standard libraries include only the basics to allow other libraries to be written. It's not like more modern, bloated systems that include everything and the kitchen sink. It seems that you want to save memory, yet you are disappointed that C does not include a bunch of memory consuming libraries.
Vagrant
+3  A: 

This should work:

void main() 
{
    puts(
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
        "<feed xmlns=\"http://www.w3.org/2005/Atom\"&gt;"
        "    <entry>"
        "        <title type=\"text\">To use xml in C</title>"
        "        <author>"
        "            <name>Punit</name>"
        "            <uri>http://stackoverflow.com/users/352194&lt;/uri&gt;"
        "        </author>"
        "        <published>2010-05-27T23:36:02Z</published>"
        "        <updated>2010-05-27T23:37:20Z</updated>"
        "        <summary type=\"html\">"
        "            &lt;p&gt;Any one who knows how to create and update XML file in C?&lt;/p&gt;"
        ""
        "        </summary>"
        "    </entry>"
        "</feed>"
    );
}
msw
Oh thanks a lot for such a quick and precise response. I will try to implement this way.
Punit
I don't think that covers the update part of his response. You should probably make the publish element dynamic. Then the question will be totally answered.
jasonmp85
Here is what i used:puts( "<?xml version=\"1.0\" encoding=\"utf-8\"?>" "<DOCUMENT>" "<PROGRESS>" "60" "</PROGRESS>" "</DOCUMENT>" );But, instead of 60 I need sum variable like "j"And 60 gets printed on my html page which I do not want to.
Punit
@jasonmp85: How to make some element dynamic in xml/C ?
Punit
Punit Good luck with that.
WhirlWind