Problem Updating xml file using C 
Author Message
 Problem Updating xml file using C

Hi,

I have a file created. This file is a xml file. now once the file is
created i want to updated it with more node elements adding to it. for
example
I have a file name call "log.xml". inside this file i have following
nodes

<Root>
 <details>
  <Name>A</Nasme>
 </details>
</Root>

I already have  these nodes created first time.. next time when i open
the same file i want to add further to this. I want to add "<details>"
node again. which is recurring. When i try to open file using "a+" in
"fopen" function it appends at the end of this file. but i want to add
those extra tags inside "<Root>" tag. Is it possible.  for eg:
<Root>
 <details>
  <Name>A</Nasme>
 </details>
 <details>
  <Name>A</Nasme>
 </details>
</Root>

Please help.
Agam
--



Sat, 08 Oct 2005 02:58:18 GMT  
 Problem Updating xml file using C

[...]

Quote:
>node again. which is recurring. When i try to open file using "a+" in
>"fopen" function it appends at the end of this file. but i want to add
>those extra tags inside "<Root>" tag. Is it possible.  for eg:

No.  It's not possible.

[...]

Quote:

>Please help.

Open a new file.  Write the data there.  When complete, close both
files, delete the old file, and rename the new file to the old name.

Regards,

                               -=Dave
--
Change is inevitable, progress is not.
--



Sun, 09 Oct 2005 04:18:27 GMT  
 Problem Updating xml file using C

in comp.lang.c.moderated:

Quote:
> Hi,

> I have a file created. This file is a xml file. now once the file is
> created i want to updated it with more node elements adding to it. for
> example
> I have a file name call "log.xml". inside this file i have following
> nodes

> <Root>
>  <details>
>   <Name>A</Nasme>
>  </details>
> </Root>

> I already have  these nodes created first time.. next time when i open
> the same file i want to add further to this. I want to add "<details>"
> node again. which is recurring. When i try to open file using "a+" in
> "fopen" function it appends at the end of this file. but i want to add
> those extra tags inside "<Root>" tag. Is it possible.  for eg:
> <Root>
>  <details>
>   <Name>A</Nasme>
>  </details>
>  <details>
>   <Name>A</Nasme>
>  </details>
> </Root>

> Please help.
> Agam

Do you have a C reference book, or the man pages or online help that
comes with your compiler?  If you do, and it does not tell you that
"a+" is required to always place the file position at the end when
performing write operations.  To be able to read and write to a file,
and write at any arbitrary position, you need "r+".

If your C reference does not contain the information above, get a
better one.

But there is another issue.  There are no library functions in C that
can "push" part of a file "down" to make room for you to insert
something in the middle, like adding text inside an editor.

Generally, unless you are replacing some existing characters with
exactly the same number of characters, you need to create a new file
and read the original file into it, making any additions, deletions,
or changes on the fly.  Then remove() the original file and rename()
the new one.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
--



Sun, 09 Oct 2005 04:18:33 GMT  
 Problem Updating xml file using C

Quote:

> I already have  these nodes created first time.. next time when i open
> the same file i want to add further to this. I want to add "<details>"
> node again. which is recurring. When i try to open file using "a+" in
> "fopen" function it appends at the end of this file.

Don't do that, then.  You have two options: make a modified copy of
the original file (using two FILE *, one opened for reading the old,
one for writing the new version), or update the old one in-place using
"r+" mode.  Position to the start of the closing </Root> tag, write
your new entry and a new </Root> to it.

The latter method will work *only* for XML files that have exactly the
structure you showed.  If there's anything else after the </Root>,
you're toast.
--

Even if all the snow were burnt, ashes would remain.
--



Sun, 09 Oct 2005 04:18:35 GMT  
 Problem Updating xml file using C

Quote:

> When i try to open file using "a+" in
> "fopen" function it appends at the end of this file.
> but i want to add
> those extra tags inside "<Root>" tag. Is it possible.

The standard I/O system does not know how to parse XML format.
Therefore you need to do this yourself, or find an XML support
library.  In any case you'll need to copy the file while
incorporating the changes into what is written out.  You
could do this to a temporary file then at the end copy it
back on top of the original, or not..
--



Sun, 09 Oct 2005 04:18:36 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. creating xml schema programmatically from an an xml file without using ADO.NET

3. Two CS files (using namespaces)

4. resx files needed for cs - files ??

5. Using xml files in C#

6. XML - using .xsd-files

7. VC++ and XML, I want to retrieve an XML file from URL

8. Using XML as Config File in MSVC++

9. VC++ and XML, I want to retrieve an XML file from a URL

10. CS files display in VS6 like CPP files ?

11. XML Web Service client using C++ - A problem

12. problem with using vc(com) parse xml

 

 
Powered by phpBB® Forum Software