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

How insert,update,delete value of an XML element with Extendscript scriptui

本文关键字:XML 元素 删除 更新 Extendscript scriptui 插入 何使用      更新时间:2023-09-26

我正在学习为adobe illustrator编写脚本的扩展脚本。对我来说,编写与xml相关的解析非常困难。我的问题陈述如下:"我取了三个文本框,即姓名、城市和国家。"name"是一个唯一的键。当我单击"确定"按钮时,如果name不存在,则数据必须保存在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 ();

请帮帮我。

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

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