• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to modify XML?

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
lets say I have a XML file,

I want to modify "Mit" and put "Vasavada" there via Java XML api. I know how to use Java XML API and all but my question is-
1. We have to read the file in DOM object and then modify the element for "Mit" and write the "whole" file back right?
How can we avoid writing whole file back? Is there a way? This is because if this XML file too big then it would be costly operation to modify a single entry like this, right?
Regards
Maulin
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With standard DOM, the only way is to write the whole document back to disk.
There probably are some APIs that allow one to modify the document in a "random access file" way, but I'm unable to point you to one right now.
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can we avoid writing whole file back? Is there a way? This is because if this XML file too big then it would be costly operation to modify a single entry like this, right?

Depends on what your application needs to do....if its only for "rendering", you could use a style sheet and attache the style sheet to your XML file. That way you can load the xml file and style sheet nd render the data. If you want to "persist" the change, then you need to write to the disk (or a DB).
regds.
- m
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
thank you for your answers.
madhav,
I want persistent storage in xml so...I guess I will end up writing whole DOM back..
Regards
Maulin
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about I just want to filter the source XML based on some rules, and then I just need to write the new XML to another Result without changes to the source XML. In this case, what is the proper API I should use for some modifications like, for example, adding a new attribute to a node.

Thanks all,
==
yc
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should look into SAX processing - it makes large documents much easier as long as the tranformation required is simple. You would essentially be reading/parsing the original XML document and writing the revised version at the same time.
Bill
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic