-1

I have an XML file in my app resources folder. I am trying to update that file with new dictionaries dynamically. In other words I am trying to edit an existing XML file to add new keys and values to it.

First of all can we edit a static XML file and add new dictionary with keys and values to it. What is the best way to do this.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
SRUE
  • 3
  • 1

1 Answers1

0

In general, you can read an XML file into a document object (choose your language), use methods to modify it (add your new dictionary), and (re-)write it back out to either the original XML file, or a new one.

That's straightforward ... just roll up the ol' sleeves and code it up.

The real problem comes in with formatting in the XML file before and after said additions.

If you are going to 'unix diff' the XML file before and after, then order is important. Some standard XML processors do better with order than others.

If the order changes behind the scenes, and is gratuitously propagated into your output file, you lose standard diffing advantages, such as some gui differs, and some scm diffs (svn, cvs, etc.).

For example, browse to:

Order of XML attributes after DOM processing

They discuss that DOM loses order where SAX does not.

You can also write a custom XML 'diff'er (there may be such off-the-shelf ... for example check out 'http://diffxml.sourceforge.net/') that compares 2 XML documents tag-by-tag, attribute-by-attribute, etc.

Perhaps some standard XML-related tool such as XSLT will allow you to keep the formatting constant without changing tag or attribute order. You'd have to research that.

BTW, a related problem is the config (.ini) file problem ... many common processors flippantly announce that the write-order may not agree with the read-order.

Community
  • 1
  • 1
David Elson
  • 211
  • 2
  • 3