Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
looping through Xml elements in indesign extendscript

I'm working on reading an xml via indesign script and create text boxes. Given this xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <project name="IPS300980XX_NEW" pagecount="50">
        <styles>
            <style nome="xxx" font="Arial" fontsize="10" bold="2" center="2" />
            <style nome="yyy" font="Times" fontsize="24" bold="1" center="1" />
            <style nome="zzzz" font="Helvetica" fontsize="22" bold="2" center="1" />
        </styles>
    </project>
</root>

I'm trying to loop through "style" nodes, without success. the code I'm using is the following (but i tried with several combinations of xmlelements, children, items, whatever :-( with no success).

"myxml" is the just opened xml document. Every variable has the expected values except "getTheStyle" (and consequently "test").

function creastili(){
        var styles = myxml.project.styles;
        var list = styles.child("style");
        var getTheStyle = "";
    for(var i = 0; i < list.length(); i++){
        getTheStyle = list[i];
        test = getTheStyle.toString();
        //getTheStyle is always empty.
        }
}

Any help would be really appreciated! Thank you very much.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Perhaps you have to use toXMLString() instead of toString().

This code works for me:

var xml = '''<?xml version="1.0" encoding="UTF-8"?>
<root>
    <project name="IPS300980XX_NEW" pagecount="50">
        <styles>
            <style nome="xxx" font="Arial" fontsize="10" bold="2" center="2" />
            <style nome="yyy" font="Times" fontsize="24" bold="1" center="1" />
            <style nome="zzzz" font="Helvetica" fontsize="22" bold="2" center="1" />
        </styles>
    </project>
</root>''';


function creastili(xml) {
    var root = new XML(xml); // native Extendscript XML parser
    var list = root.project.styles.children();
    for(var i = 0; i < list.length(); i++){
        var getTheStyle = list[i];
        var test = getTheStyle.toXMLString();
        alert(test);
    }
}

creastili(xml);

enter image description here

You can get attributes of a 'style' this way:

var root = new XML(xml);
var style = root.project.styles.children()[0];
var name = style.@nome;
var font = style.@font;
var size = style.@fontsize;
// etc

alert([name, font, size].join('\n'));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!