How can I modify the xml file directly?

Hi, folks
Here I have a piece of xml file example:

Programming Ruby
Dave T.
$54.96

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

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

On 2010-01-16, Milo L. [email protected] wrote:

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

On Jan 16, 2010, at 12:50 PM, Seebs wrote:

On 2010-01-16, Milo L. [email protected] wrote:

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.

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 W.

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

MarkT

Seebs wrote:

On 2010-01-16, Milo L. [email protected] wrote:

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

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. :slight_smile:

BR,
Milo

On Saturday 16 January 2010 11:50:04 am Seebs wrote:

On 2010-01-16, Milo L. [email protected] wrote:

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.

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.

David M. wrote:

On Saturday 16 January 2010 11:50:04 am Seebs wrote:

On 2010-01-16, Milo L. [email protected] wrote:

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.

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

Mark T wrote:

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

Seebs wrote:

On 2010-01-17, Milo L. [email protected] wrote:

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:
hello, world

You want to change that to “hello, world!”. So you do. And you get:
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.

On 2010-01-17, Milo L. [email protected] wrote:

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:
hello, world

You want to change that to “hello, world!”. So you do. And you get:
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