如何使用 Extendscript scriptui 插入、更新、删除 XML 元素的值
How insert,update,delete value of an XML element with Extendscript scriptui
我正在学习用于编写 adobe 脚本的 extendscript illustrator.It 我很难编写 xml 相关的解析。
我的问题陈述如下:- "I have taken three text boxes namely name,city and country."name" 是唯一的 key.when 我单击确定按钮如果名称不存在,则数据必须保存在 xml 否则更新以前的名称创建 duplicate.All xml 文件中的名称显示在列表中 box.The 特定名称的日期可以通过删除删除 button.which 将删除列表框选定项目中的数据。
我尝试做的代码是:-
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 ();
请帮助我。
这不应被视为完整答案。我仍然 post 因为它可能有助于找到一个。
这是我试图写的答案。 read/write 部分有效,但元素存在的检查失败。
如果 child
不是完全匹配,则 xml.contains(xml) 不会 return 为真。
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。
我正在学习用于编写 adobe 脚本的 extendscript illustrator.It 我很难编写 xml 相关的解析。 我的问题陈述如下:- "I have taken three text boxes namely name,city and country."name" 是唯一的 key.when 我单击确定按钮如果名称不存在,则数据必须保存在 xml 否则更新以前的名称创建 duplicate.All xml 文件中的名称显示在列表中 box.The 特定名称的日期可以通过删除删除 button.which 将删除列表框选定项目中的数据。 我尝试做的代码是:-
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 ();
请帮助我。
这不应被视为完整答案。我仍然 post 因为它可能有助于找到一个。
这是我试图写的答案。 read/write 部分有效,但元素存在的检查失败。
如果 child
不是完全匹配,则 xml.contains(xml) 不会 return 为真。
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。