How can I modify the xml file directly?

M

Milo Luo

Hi, folks
Here I have a piece of xml file example:
<book>
<name>Programming Ruby</name>
<author>Dave Thomas</author>
<price>$54.96</price>
</book>

If I want to modify the price of the book directly to the xml file, can
I do that and how can I do that?
Thanks very much.

BR,
Milo
 
S

Seebs

I meant without string or tmp file, write to original file.

In general, on most systems, the only modifications you can make without
a temporary file or something comparable are those which do not change the
number of characters in a file. You can't insert or delete characters in
the middle of a file.

-s
 
G

Gary Wright

In general, on most systems, the only modifications you can make without
a temporary file or something comparable are those which do not change the
number of characters in a file. You can't insert or delete characters in
the middle of a file.

I often wondered if there is any OS floating around out there that goes
beyond the traditional file semantics (replace, append, truncate, extend).
For example:
-- prepend
-- delete (from beginning or middle)
-- insert

I would think that many of those operations would be painfully inefficient
unless perhaps they were done on block-sized chunks to avoid having to
keep track of partially used blocks.

Gary Wright
 
D

David Masover

In general, on most systems, the only modifications you can make without
a temporary file or something comparable are those which do not change the
number of characters in a file. You can't insert or delete characters in
the middle of a file.

Not true; you can append to the end of a file.

But yes, that's the point, and that's why XML (or JSON, or Yaml) is not
efficient as a database format, unless you work around it somehow. It's fine
(preferred, even) if the files are small enough, but yes, you are going to
have to write to a tempfile first, then move that on top of the original file.

Or, if you're feeling reckless, you can truncate the original file to 0, then
overwrite it directly.
 
M

Mark T

If you are looking for an efficient way of editing xml 'records' then
gooling 'xml databases' will return hits.

MarkT

Hi, folks
Here I have a piece of xml file example:
<book>
=C2=A0 =C2=A0<name>Programming Ruby</name>
=C2=A0 =C2=A0<author>Dave Thomas</author>
=C2=A0 =C2=A0<price>$54.96</price>
</book>

If I want to modify the price of the book directly to the xml file, can
I do that and how can I do that?
Thanks very much.

BR,
Milo



--=20
=E3=83=81=E3=82=A7=E3=83=83=E3=82=AF=E3=82=A2=E3=82=A6=E3=83=88=E3=81=8C=E3=
=80=81Jingle =E3=81=A0!
It's a Jingle Out There!
 
M

Milo Luo

Seebs said:
In general, on most systems, the only modifications you can make without
a temporary file or something comparable are those which do not change
the
number of characters in a file. You can't insert or delete characters
in
the middle of a file.

-s

Hi, Seebs
Do you mean it's related with the file system API?
If it can located or got the node & text from XML, it should be modified
the node or text value without too many problems.
I don't know if I misunderstand the process of getting the node & text.
Anyway, thanks. :)

BR,
Milo
 
S

Seebs

Do you mean it's related with the file system API?
Yes.

If it can located or got the node & text from XML, it should be modified
the node or text value without too many problems.

Not in general.

The problem is that, unless the new value is the same length as the old
value, modifying text in the middle of the file produces problems.

Imagine that we have a really simple XML file:
<greet>hello, world</greet>

You want to change that to "hello, world!". So you do. And you get:
<greet>hello, world!/greet>

See the problem? You can't make the later parts of the file move ahead;
you would actually have to rewrite the whole file.

-s
 
M

Milo Luo

David said:
Not true; you can append to the end of a file.

But yes, that's the point, and that's why XML (or JSON, or Yaml) is not
efficient as a database format, unless you work around it somehow. It's
fine
(preferred, even) if the files are small enough, but yes, you are going
to
have to write to a tempfile first, then move that on top of the original
file.

Or, if you're feeling reckless, you can truncate the original file to 0,
then
overwrite it directly.

Hi, David
It seems that I can only truncate the original file to 0, then overwrite
it directly, but it will cost a lot of IO if the file is very large.
But seems that it's the best way to do that.
Thank you,sir.

BR,
Milo
 
M

Milo Luo

Mark said:
If you are looking for an efficient way of editing xml 'records' then
gooling 'xml databases' will return hits.

MarkT

Oh, thanks Mark. I will try.

BR,
Milo
 
M

Milo Luo

Seebs said:
Not in general.

The problem is that, unless the new value is the same length as the old
value, modifying text in the middle of the file produces problems.

Imagine that we have a really simple XML file:
<greet>hello, world</greet>

You want to change that to "hello, world!". So you do. And you get:
<greet>hello, world!/greet>

See the problem? You can't make the later parts of the file move ahead;
you would actually have to rewrite the whole file.

-s

Oh, I got the meaning. It will overwrite later characters, data will
lost...
That's bad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,283
Latest member
JoannaGrif

Latest Threads

Top