如何使用Extendscript scriptui插入,更新,删除XML元素的值

时间:2016-04-29 06:09:44

标签: javascript xml adobe adobe-illustrator extendscript

我正在学习用于编写adobe illustrator脚本的extendscript。我很难编写与xml相关的解析。 我的问题陈述如下: - “我已经取了三个文本框,即名称,城市和国家。”名称“是一个唯一的密钥。当我点击确定按钮时,如果名称不存在,数据必须保存在xml中,否则更新以前的名称没有创建副本。所有xml文件中的名称都显示在列表框中。特定名称的日期可以通过删除按钮删除。它将删除列表框中所选项目中的数据。 我试图做的代码是: -

   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 ();

请帮助我。

1 个答案:

答案 0 :(得分:0)

这不应被视为完整答案。我仍然发布它,因为它可能有助于找到一个。

这是我试图写的答案。读/写部分可以工作,但是元素的检查存在失败。 如果child不是完全匹配,则xml.contains(xml)将不返回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();

我真正的答案是: 改用JSON。