Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

read xml in reverse order

Guest
Feb 04, 2016 Feb 04, 2016

How to read xml elements in reverse order from the indesign structure panel??

I want to read the parent element of a particular element in a loop

TOPICS
Scripting
501
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Feb 05, 2016 Feb 05, 2016
LATEST

Hi,

First of all, if you only need to retrieve string data from an element, I would definitively recomment using a XML object instead. It's much much faster.

var reverseXMLthroughObject = function() {

    var doc, xf, xo, cs, c, n, arr = [];

   

    if (!app.documents.length

    || !app.activeDocument.xmlElements[0].xmlElements.length) {

        alert("Nothing to work with…");

        return;

    }

    doc = app.activeDocument;

    xf = File ( Folder.temp+"/tmp.xml");

    doc.exportFile ( ExportFormat.XML, xf );

    xf.open('r');

    xo = XML(xf.read());

    xf.close();

    xf.remove();

   

    cs = xo.xpath("*/*");

    n = cs.length();

   

    while (n--) arr[arr.length]=String(cs);

   

    return arr.join(",");

}

reverseXMLthroughObject();

However if you need to do work with xmlElements inside the indesign structure, you may try this way.

var reverseXMLThroughStructure = function() {

    var doc, root, cs, c, n, arr = [];

   

    if (!app.documents.length

    || !app.activeDocument.xmlElements[0].xmlElements.length) {

        alert("Nothing to work with…");

        return;

    }

    doc = app.activeDocument;

    root = doc.xmlElements[0];

    cs = root.evaluateXPathExpression("./*");

    n = cs.length;

   

    while (n--) cs.texts[0].isValid && arr[arr.length]=cs.contents;

   

    return arr.join(",");

}

reverseXMLThroughStructure();

Another approach is XMLRules.

Loic

www.ozalto.com

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines