Skip to content

XML creation and import

fabiantheblind edited this page Jun 1, 2016 · 1 revision

Baby steps in creating xml files and import them into InDesign.
Take also a look into the XMLElement Object.

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

Written for this stackoverflow question.

Home

Clone this wiki locally