如何使用Extendscript scriptui插入、更新、删除XML元素的值
How insert,update,delete value of an XML element with Extendscript scriptui
我正在学习为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
相关文章:
- 使用Jquery添加XML元素
- 如何使用Extendscript scriptui插入、更新、删除XML元素的值
- 如何在IE上创建xml元素并添加nodeValue
- 从 XML 元素获取文本
- 无法使用 jquery 获取 xml 元素文本值
- 获取 XSLT 中的 XML 元素
- 如何使用 jquery 查找名称中带有“:”的 xml 元素
- 使用 jQuery 在 HTML 表中显示单独的 XML 元素
- 仅使用 JavaScript 查找具有给定属性值的 xml 元素的值
- 在节点.js中按类型和 ID 查找 XML 元素
- 测试 XML 元素是否存在时遇到问题
- jQuery嵌套的XML元素
- jQuery:添加XML元素
- 如何使JSONIX在反序列化时容忍额外的XML元素
- MSIE:将XML元素附加到HTML元素
- jquery ajax调用强制响应中的XML元素名称为小写
- 如何遍历xml元素在javascript中的直接子元素,保存特定属性的值
- Jquery改变"value"属性在XML元素中
- 值与XML元素匹配,然后返回所有兄弟属性值
- 如何在javascript中从xml字符串输出特定xml元素的所有值