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
2010-05-27 23:37:20
I have memory restrictions therefore I want to avoid this.
Punit
2010-05-27 23:38:41
So, what is it that you *really* want to do?
WhirlWind
2010-05-27 23:39:20
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
2010-05-27 23:45:48
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
2010-05-27 23:50:56
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
2010-05-27 23:57:18
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
2010-05-27 23:57:49
@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
2010-05-28 00:02:35
+3
A:
This should work:
void main()
{
puts(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<feed xmlns=\"http://www.w3.org/2005/Atom\">"
" <entry>"
" <title type=\"text\">To use xml in C</title>"
" <author>"
" <name>Punit</name>"
" <uri>http://stackoverflow.com/users/352194</uri>"
" </author>"
" <published>2010-05-27T23:36:02Z</published>"
" <updated>2010-05-27T23:37:20Z</updated>"
" <summary type=\"html\">"
" <p>Any one who knows how to create and update XML file in C?</p>"
""
" </summary>"
" </entry>"
"</feed>"
);
}
msw
2010-05-27 23:58:18
Oh thanks a lot for such a quick and precise response. I will try to implement this way.
Punit
2010-05-28 00:00:50
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
2010-05-28 00:05:03
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
2010-05-28 00:06:36