Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript to edit an XML file

Status
Not open for further replies.

apertosa

Programmer
Aug 11, 2006
15
US
Hi all, and thanks for reading.
I have very little XML/Web experience and I have to do a feasibility study on a editor that reads/writes XML files.
The XML files are "fixed" in other words all the elements and attributes are known.
Some of the elements and attributes must be editable and I'm trying to understand if it is possible to write a javascript and use that in a browser to edit XML files.
Is it possible to use a browser to access all the fields (some choices are not just numbers, but enums) write a XSLT and have the browser be the editor that modifies the values ?
How does Javascript play into that scenario?
As I said I have little experience with XML or XSLT and no experience with Javascript, so I thought of asking someone competent.
I can be more specific and attach an example XML file.
Thanks,
Andrea.
 
and I have to do a feasibility study on a editor that reads/writes XML files

So this is a homework assignment?

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Haha!
I wish.
No: I should have said "I want" to do a feasibility study.
I work for a software company and I would like to support some legacy products that are using XML as data representation. I just don't have the resources to create a proper XML editor from scratch, so I'm looking for a shortcut.
Initially I thought of creating the editor in Qt designer, use python and pyQt and deliver the editor as a series of python scripts (plus Qt libraries).
But this is not feasible because of very limited resources. Sooo.. I'm trying to see if there is an easier way.
Thanx.
andrea.
 
Hi again,
I was wondering if I'm off here thinking that a browser can be used to edit XML files.
Someone at work suggested this route:
XSLT - > to transform the XML file into a HTML code with appropriate forms (Xforms seems to be the way editable information is presented in a browser).
- Javascript - > to process the loading of the XML data into the various fields (upon opening of the XML file) and to save (update) the XML file (or write a completely fresh one) when the user clicks on Save.

If I get enough consensus I'd be willing to invest time in this, despite the fact that it might mean many long sleepless nights!
Thanks!
 
The XML files are "fixed" in other words all the elements and attributes are known.

In that case, I don't see why this couldn't be done with Javascript, but it's probably not worth the time.

Off the top of my head too, it seems quite difficult.


[small]"Mom........MEATLOAF!!!! F***!!!!"[/small]
<.
 
By "Fixed" I mean that I can decide the type of all the elements in the XML file -> I know what they are.
Most are real numbers, some are integers, some are enums.
In my mind real values map to numerical fields, enums map to option menus (containing all the allowable choices), booleans map to check boxes etc...
I think that the core of my question is: with the emphasis on XML data representation and all that, is it possible that building a GUI on top of an XML file is such an endeavor?
thanx.
 
apertosa, if your goal is to read in an xml file, present it in a graphical interface to edit and then write back to an xml file you should be able to do this but have issues with the restrictions of client side script unless you are using ASP JScript server side?

You can write your own parser that will read through a page pulling out tag/data sets and then present them however you will for editing. Retrieving and writing back that data is another issue. Do you have the ability to use server-side code to do the reading/writing? Will the xml files be accessible from your web server so server-side code can work with them?

It is possible to use javascript or vbscript to read/write files through the activex filesystemobject and using local PC resources but this limits you to IE browsers only and will trigger ActiveX warning messages. You could write it as a .HTA application that the client executes locally rather than from a web server and avoid the ActiveX warnings but again it is IE only.

Once the XML is read in as data you can alter it however you like, the trick is in how you will read/write the data and in how you will present it in the GUI for editing.


At my age I still learn something new every day, but I forget two others.
 
If you're using windows, you don't need to write your own parser. Just use msxml. I don't have any JS examples to hand but I have lots of VBS ones. I think CreateObject in VBS is something like new ActiveXObject in JS.

Anyway, in VBS, to read in an XML file, it is just
Code:
      set xmlDoc = CreateObject ("Microsoft.XMLDOM")
      xmlDoc.async = false
      xmlDoc.Load filename
No need to play with the file system object. To get a set of nodes
Code:
   set collection = xmlDoc.getElementsByTagName (nodename)
To examine the nodes
Code:
   for each item in collection
       xxx = item.firstChild.value
   next
And that is it. Nothing very complex or difficult about it. Once again, sorry about the vbscript. It shouldn't be difficult to translate between the two. I've been using VBS a lot lately and can't get the memories of JS into gear.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top