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

Fetching xml contents from INDD

Explorer ,
Oct 11, 2020 Oct 11, 2020

Hi,

I have used the following code to fetch the xml attribute values from Indesign document. But it returns null value. Please guide me to fetch the attribute values of the xml.

var featureAttributes = getAllAttributeValuesFromINDDFile("features");
alert(featureAttributes)
function getAllAttributeValuesFromINDDFile(attributeName){
var allAttributes = [];
var myDoc = app.documents[0];
var xPath = ["//*[@"+attributeName+"]"];
var root = myDoc.xmlElements[0];
var node = null;
try {
var proc = app.xmlRuleProcessors.add(xPath);
var match = proc.startProcessingRuleSet(root);
while (match != undefined) {
node = match.element;
match = proc.findNextMatch();
if (node != null && node != undefined) {
try{
allAttributes.push(node.xmlAttributes.item(attributeName).value.toString())
}
catch(e){}
}
}
}
catch (ex) {}
finally {
proc.endProcessingRuleSet();
proc.remove();
}
if(allAttributes.length > 0){
return allAttributes;
}
else{
return null;
}
}

 

Regards,

 

Nithu
TOPICS
How to , Scripting
1.5K
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

correct answers 1 Correct answer

Advocate , Oct 13, 2020 Oct 13, 2020

Hi Nithu,

If you want CDATA of XML file only, which is out side of InDesign File, then you can try this snippet:

 

var allCDATA = [];
var myXMLFile = File("C:/Users/r.sunil/Desktop/TestingCDATA/Test.xml");
myXMLFile.open('r');
var allElements = new XML(myXMLFile.read());
myXMLFile.close();
var allFeatureElements = allElements.xpath("//feature");
for(var n = 0; n < allFeatureElements.length(); n++){
    if(allFeatureElements.children().length() > 0){
        getChildData(allFeatureElements);
    
...
Translate
Community Expert ,
Oct 12, 2020 Oct 12, 2020

Seems to work for me, see the screengrab here

-Manan

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
Explorer ,
Oct 12, 2020 Oct 12, 2020

Hi,

For another time I have used this script to fetch the xml attribute value by that time I get an null value. Hereby I have attached the screenshot. 

Nithu_S_2-1602489600846.png

 

Nithu
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
Advocate ,
Oct 12, 2020 Oct 12, 2020

By seeing your screen shot.

What you have selected is the node with name "feature".

 

But what code does is it will find all nodes which are having attributes named as "feature".

And it will give you result in return in an array.

 

P.S. I posted this code on some forum question. That question was to fetch all values for specific attribute.

 

But if you need to fetch all attributes of specific node with node name.

Then you can try this code snippet:

 

var allAttributes = getAllAttributeValuesFromINDDFile("features");
alert(allAttributes)
function getAllAttributeValuesFromINDDFile(nodeName){
    var allAttributes = [];
    var myDoc = app.documents[0];
    var xPath = ["//"+nodeName];
    var root = myDoc.xmlElements[0];
    var node = null;
    try {
        var proc = app.xmlRuleProcessors.add(xPath);
        var match = proc.startProcessingRuleSet(root);
        while (match != undefined) {
            node = match.element;
            match = proc.findNextMatch();
            if (node != null && node != undefined) {
                try{
                    for(var a = 0; a < node.xmlAttributes.length; a++){
                        allAttributes.push(node.xmlAttributes[a].value.toString())
                        }
                    }
                catch(e){}
                }
            }
        }
    catch (ex) {}
    finally {
        proc.endProcessingRuleSet();
        proc.remove();
        }
    if(allAttributes.length > 0){
        return allAttributes;
        }
    else{
        return null;
        }
    }

 

Best

Sunil

 

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
Advocate ,
Oct 12, 2020 Oct 12, 2020
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
Explorer ,
Oct 12, 2020 Oct 12, 2020

Hi,

I have used the given script but it doesn't get the attribute values. But it provide attribute_id and paragraph style used for the attributes present in the xml file. I have attached the screenshots which I get after using this script

 

Nithu_S_0-1602495928032.png

 

Nithu
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
Advocate ,
Oct 12, 2020 Oct 12, 2020

Hi Nithu,

As per your XML structure in InDesign File, it looks like attribute_id, COTYPE, TBGUID & aid:pstyle these are the attributes of that node named "features" & in that alert I can see you are getting values of all those attributes.

 

But it seems you want to do something else. Please elaborate what do you exactly need. Then only I can guide.

 

Sunil

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
Explorer ,
Oct 12, 2020 Oct 12, 2020

Hi,

I need to gets values inside the CDATA of the featurew attribute value

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tradingbell_root catalog_id="3" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">
<catalog_name COTYPE="TEXT" TBGUID="CN2034066292" aid:pstyle="catalog_name"><![CDATA[Seating]]></catalog_name>
<version COTYPE="TEXT" TBGUID="CV840691114" aid:pstyle="version"><![CDATA[]]></version>
<features COTYPE="TEXT" TBGUID="CD289026564" aid:pstyle="features"><![CDATA[]]></features>
<category COTYPE="" category_id="130767" families="1" TBGUID="C2127008489" aid:pstyle="category">
<category_section_id COTYPE="TEXT" TBGUID="CS145280434" aid:pstyle="category_section_id"><![CDATA[CAT257]]></category_section_id>
<category_section_id COTYPE="TEXT" TBGUID="CS11483401631" aid:pstyle="category_section_id"><![CDATA[CAT257]]></category_section_id>
<category_section COTYPE="TEXT" TBGUID="CS1756750598" aid:pstyle="category_section"><![CDATA[lounge - modular]]></category_section>
<category_section COTYPE="TEXT" TBGUID="CS2493288803" aid:pstyle="category_section"><![CDATA[lounge - modular]]></category_section>
<cat_specs_type COTYPE="" TBGUID="ST1795470686">
</cat_specs_type>
<image_type COTYPE="" TBGUID="IT1219369547">
</image_type>
<SUBCATNAME_ID1 COTYPE="TEXT" TBGUID="FCN2099034281" aid:pstyle="SUBCATNAME_ID1"><![CDATA[CAT257]]></SUBCATNAME_ID1>
<SUBCATNAME_L1 COTYPE="TEXT" TBGUID="FCN1451175229" aid:pstyle="SUBCATNAME_L1"><![CDATA[lounge - modular]]></SUBCATNAME_L1>
<category_id COTYPE="TEXT" TBGUID="FCI855580260" aid:pstyle="category_id"><![CDATA[CAT252]]></category_id>
<category_id COTYPE="TEXT" TBGUID="FCI1196275522" aid:pstyle="category_id"><![CDATA[CAT252]]></category_id>
<category_name COTYPE="TEXT" TBGUID="FCN2052734092" aid:pstyle="category_name"><![CDATA[b.1]]></category_name>
<category_name COTYPE="TEXT" TBGUID="FCN1014608296" aid:pstyle="category_name"><![CDATA[b.1]]></category_name>
<cat_specs_type COTYPE="" TBGUID="ST1447082601">
</cat_specs_type>
<family_name COTYPE="TEXT" TBGUID="FN1067951107" aid:pstyle="family_name"><![CDATA[b.1]]></family_name>
<specs_type COTYPE="" TBGUID="ST723118287">
<features attribute_id="40" COTYPE="TEXT" TBGUID="SAV1149696081" aid:pstyle="features"><![CDATA[<strong>Fully~tab~upholstered 123.</strong>~br~<u>Standard </u>HR<s>foam </s><em>back</em> pillow.~br~<sub>StudioSoft </sub>Brushed <sup>stainless </sup>steel base~WS~. When ordering multi-item configurations, please provide the total COM yardage from the same dye lot in a continuous roll(s). *All COM/COLfabrics must be preapproved by Design prior to application. See terms and conditions for instructions on tagging COM.Approved for GSA. Contract GS-27F-010GA. Level 2 and Gold GREENGUARD Gold Certified. When using a combination of fabrics or leathers for 4315BS or 4315BF, the following yardage must be used and upcharges apply. See terms and conditions for contrasting fabric upcharges. Please specify the fabric or leather application by designating the appropriate letter on the purchase order. A) One fabric or leather for outside arms, inside arms, outside back, inside back and front panel yardage: 4.9 yds. leather: 94.2 sq. ft. B) One fabric or leather for seat yardage: 3.8 yds. leather: 52.3 sq. ft. C) One fabric or leather for back pillow yardage: 2.8 yds. leather: 39.8 sq. ft.]]></features>
</specs_type>
<vendor_info COTYPE="" TBGUID="VI1700608280">
</vendor_info>
</category>
</tradingbell_root>

Nithu
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
Advocate ,
Oct 13, 2020 Oct 13, 2020

Hi Nithu,

If you want CDATA of XML file only, which is out side of InDesign File, then you can try this snippet:

 

var allCDATA = [];
var myXMLFile = File("C:/Users/r.sunil/Desktop/TestingCDATA/Test.xml");
myXMLFile.open('r');
var allElements = new XML(myXMLFile.read());
myXMLFile.close();
var allFeatureElements = allElements.xpath("//feature");
for(var n = 0; n < allFeatureElements.length(); n++){
    if(allFeatureElements.children().length() > 0){
        getChildData(allFeatureElements);
        }
    }
//===================
function getChildData(nodeData){
    for(var c = 0; c < nodeData.children().length(); c++){
        if(nodeData.children()[c].toXMLString().toString().indexOf("<![CDATA[") == 0){
            alert(nodeData.children()[c].toString());
            allCDATA.push(nodeData.children()[c].toString());
            }
        if(nodeData.children()[c].children().length() > 0){
            getChildData(nodeData.children()[c]);
            }
        }
    }
alert(allCDATA);

 

 

P.S. <![CDATA[Some data]]> will not get imported into InDesign File as CDATA is not supported by InDesign.

Best

Sunil

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
Advocate ,
Oct 13, 2020 Oct 13, 2020

Has this above code solved your concern?

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
New Here ,
Feb 28, 2021 Feb 28, 2021
LATEST

Hi,

Enhanced script would be like:

 

var inputXML = File('D:/Pavithra/TestingCDATA/Test.xml');
if(inputXML.exists == true) {
    inputXML.open('r');
    var text = inputXML.read();
    inputXML.close();
    var content = new XML(text);
    var CDATA = [];
    var elements = content.xpath("//CDATA");
    for(var n = 0; n < elements.length(); n++){
        CDATA.push(elements[n].toString());
        }
    }
alert(CDATA);

 

Pavithra

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