Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问Extendscript中的XML属性名称_Javascript_Xml_Attributes_Adobe Illustrator_Extendscript - Fatal编程技术网

Javascript 访问Extendscript中的XML属性名称

Javascript 访问Extendscript中的XML属性名称,javascript,xml,attributes,adobe-illustrator,extendscript,Javascript,Xml,Attributes,Adobe Illustrator,Extendscript,我正在尝试解析extendscript中的xml对象,特别是处理属性。我知道我可以通过 xmlObj.@attributename 及 返回所有属性的列表,但我还需要属性名,而不仅仅是值。是否仍然可以获得类似于和关联数组/对象的名称和值 (我对illustrator CS6使用extendscript) 谢谢,, arno下面的代码应该可以让您继续。另外,请看一下 var main=function(){ //创建一些xml并将其写入文件 var root=newxml(“”); var ch

我正在尝试解析extendscript中的xml对象,特别是处理属性。我知道我可以通过

xmlObj.@attributename

返回所有属性的列表,但我还需要属性名,而不仅仅是值。是否仍然可以获得类似于和关联数组/对象的名称和值

(我对illustrator CS6使用extendscript)

谢谢,,
arno

下面的代码应该可以让您继续。另外,请看一下

var main=function(){
//创建一些xml并将其写入文件
var root=newxml(“”);
var child=newxml(“”);
child@string=“Hello属性”;//jshint ignore:line
child@num=23;//jshint忽略:行
根.appendChild(child);
var file=新文件(“~/Desktop/test.xml”);
var xml=root.toXMLString();
文件。打开(“W”);
file.write(xml);
file.close();
//获取当前文档
var doc=app.activeDocument;
//导入xml
doc.importXML(文件);
//获取元素
var xmlroot=doc.xmlElements[0];
var xmlchild=xmlroot.xmlElements[0];
//循环元素“child”的所有属性
//并将它们写入控制台
对于(var i=0;i
我找到了一种用正则表达式解决它的方法

函数getAttributes(xml\u node\u str){ //选择开始标记 var reg_exp=/]*>/; var start\u tag\u str=reg\u exp.exec(xml\u node\u str); //提取属性 reg_exp=/[^”\s]*=“[^”]*”/g; var结果; var属性=[]; while((result=reg\u exp.exec(start\u tag\u str))!==null){ //属性(name=“value”) var attr=结果[0]; //包含名称和“值”的数组 var attr_arr=attr.split('='); //删除“'s” attr_arr[1]=attr_arr[1]。substr(1,attr_arr[1]。长度-2); 属性推送(attr_arr); } 返回属性;
}谢谢,不幸的是,我忘了说我使用Extendscript for Illustrator(cs6),该脚本只在inDesign中工作。有没有办法用illustrators XML类处理它?没问题。我环顾四周,但没有找到任何好的解决方案。我认为可以通过使用xPath表达式来完成。我用xPath尝试过,但不知怎的,它们在extendscript.name(//element/@*[1])中不起作用在任何xpath计算器中都可以工作,但在extendscript中它不会返回任何字符串,不是吗?也许你会在Adobe论坛上问。我还是会选择。他们是最活跃的,经常需要处理xml
xmlObj.attributes()
var main = function() {
  // create some xml and write it to file
  var root = new XML("<root/>");
  var child = new XML("<child/>");
  child.@string = "Hello Attribute"; // jshint ignore:line
  child.@num = 23; // jshint ignore:line
  root.appendChild(child);
  var file = new File("~/Desktop/test.xml");
  var xml = root.toXMLString();
  file.open("W");
  file.write(xml);
  file.close();

  // get the current doc
  var doc = app.activeDocument;
  // import the xml
  doc.importXML(file);
  // get the elements
  var xmlroot = doc.xmlElements[0];
  var xmlchild = xmlroot.xmlElements[0];
  // loop all attributes of element "child"
  // and write them into the console
  for (var i = 0; i < xmlchild.xmlAttributes.length; i++) {
    var attr = xmlchild.xmlAttributes[i];
    $.writeln(attr.name);
  }
};
main();