0

I am learning extendscript for scripting adobe illustrator.It is very difficult for me to write xml related parsing. My problem statement is given below:- "I have taken three text boxes namely name,city and country."name" is a unique key.when i click ok button the data must be saved in xml if name does not exist else update the previous name with out creating duplicate.All the names in xml file are displayed in list box.The date of particular name could be deleted by remove button.which will remove data in selected item of list box. The code i tried to do is:-

   var myWindow = new Window ("dialog", "Form");
   var txt1 = myWindow.add ("edittext");//name unique
   var txt2 = myWindow.add ("edittext");//city
   var txt3 = myWindow.add ("edittext");//country
   var btn=myWindow.add ("button", undefined, "OK");
   btn.onClick = function () {
   for (i=0;i<numberofnamesinxml;i++)//coding required
   {
      if(txt1.text!=xmlname[i]) // to verify name not there since it is like      primary key
       xmlFile.open("a");  
       xmlFile.write(root.toXMLString());
       xmlFile.copy ('C:/data.xml');
       xmlFile.close();
      //myList refresh
   }
}
   var myList = myWindow.add ("listbox");
   for (i=0;i<numberofnamesinxml;i++)//coding required
   {
       config='C:/data.xml';
       config.open("r");  
       data= xmlname[i] //here i need to set data to  
       config.close();  
       myList.add ("item", data);
   }
   var btn1=myWindow.add ("button", undefined, "remove");
   btn1.onClick = function () {
   myList.remove (myList1.selection[i]);
   //xml data having this list name must be removed
   }
   myWindow.show ();

Please kindly help me.

RobC
  • 22,977
  • 20
  • 73
  • 80
Hari Krishna
  • 2,372
  • 2
  • 11
  • 24
  • i would suggest using JSON if possible. The xml features of ExtendScript lack a lot of functionality. I tried to write something that fits your needs but ran into problems when trying to check if the element already exists. I'll post the code but don't consider it a full working answer. – fabianmoronzirfas Apr 29 '16 at 07:21

1 Answers1

0

This should not be considered a full answer. I still post it because it might help finding one.

This is what I tried to write as an answer. The read/write part works but the checking of an element exists fails. if the child is not an exact match, xml.contains(xml) will not return true.

var path = '~/Desktop/test.xml';
var xmlfile = File(path);
if (!xmlfile.exists) {
  $.writeln('xml file does not exist. Creating new one');
  xmlfile = new File(path);
  xmlfile.open('w');
  xmlfile.write('<Root></Root>');
  xmlfile.close();
}
xmlfile.open('r');
xmlcontent = xmlfile.read();
xmlfile.close();
//$.writeln(xmlcontent);
var xml = new XML(xmlcontent);

// here the problems start
// the check is only working for known elements
var child = new XML('<name data="bob"></name>');
if (xml.contains(child)) {
  child.@data = 'jim';
  $.writeln('A child called "name" exists. Update');
  xml.replace('name', child);
} else {
  $.writeln('no child called "name" exists. Append');
  child.@data = 'bob';
  xml.appendChild(child);
}
xmlfile.open('w');
xmlfile.write(xml.toXMLString());
xmlfile.close();

My real answer is: Use JSON instead.

fabianmoronzirfas
  • 4,091
  • 4
  • 24
  • 41
  • Could you implement it in my code.even it is json.I cant implement for my code. – Hari Krishna Apr 29 '16 at 07:59
  • getting error :Error 24:xmlfile.open is not a function. Line: 10-> xmlfile.open("r"); – Hari Krishna Apr 29 '16 at 08:06
  • Sorry. I currently don't have the time to implement __your__ code. About the error. I ran it from ExtendScript Toolkit and it works fine. I ran it from InDesign and it works fine. When running it from Illustrator `xmlfile` contains the Application `Folder` of Illustrator not the `File` Object on the Desktop. Looks like an Illustrator problem to me. You should ask in the Illustrator Scripting Forum for help about this https://forums.adobe.com/community/illustrator/illustrator_scripting – fabianmoronzirfas Apr 29 '16 at 10:30