Fetching modified xml content

Community Beginner ,
Oct 02, 2020

Copy link to clipboard

Copied

Hi,

Can someone guide for finding the modified contents in the xml.

Regards,

 

Rajendran
Adobe Community Professional
Correct answer by Sunil Yadav | Adobe Community Professional

Hi Rajendran,

If you are looking for CDATA specifically.

Then you may use this code 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);

 

Nearly same question has been asked here => This link

 

Best

Sunil

TOPICS
Import and export, Scripting

Views

239

Likes

Translate

Translate

Report

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

Fetching modified xml content

Community Beginner ,
Oct 02, 2020

Copy link to clipboard

Copied

Hi,

Can someone guide for finding the modified contents in the xml.

Regards,

 

Rajendran
Adobe Community Professional
Correct answer by Sunil Yadav | Adobe Community Professional

Hi Rajendran,

If you are looking for CDATA specifically.

Then you may use this code 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);

 

Nearly same question has been asked here => This link

 

Best

Sunil

TOPICS
Import and export, Scripting

Views

240

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 02, 2020 0
Adobe Community Professional ,
Oct 02, 2020

Copy link to clipboard

Copied

Please provide more detail.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 02, 2020 0
Community Beginner ,
Oct 02, 2020

Copy link to clipboard

Copied

Hi, 

I have an xml with some attributes like features, notes, descriptions, tables. In which I want to fetch the contents of any specific attribute.

Rajendran

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 02, 2020 0
Advisor ,
Oct 03, 2020

Copy link to clipboard

Copied

The Oxygen XML editor has a diff tool, probably also other XML editors.

There is even a tool that specializes on XML differencing - DeltaXML.

The generic text editor BBEdit also has a powerful file comparison.

You'd use either of them on old and new revision of your document and see the "modified contents".

 

As this is a InDesign scripting "question", you can also run your own comparison - e.g. load old and new version and compare element by element, attribute by attribute. Dependent on your data structure, disregard order e.g. of attributes and match by name. Compare elements by ID rather than by position - if they have an ID. And so forth.

 

If you just want to access a specific attribute from your script, one XML way to specify any node is called XPath. You'd start from any element such as your root element, then use evaluateXPathExpression .

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 03, 2020 0
Adobe Community Professional ,
Oct 05, 2020

Copy link to clipboard

Copied

If you need to read XML and fetch values directly from xml which is outside of InDesign, then you can try like this:

 

//===================================
var myXMLFile = File("C:/Users/SunilYadav/Desktop/NEW/Test.xml");
myXMLFile.open('r');
var allElements = new XML(myXMLFile.read());
myXMLFile.close();
var featureAttributes = getAllAttributeValues("features");
var notesAttributes = getAllAttributeValues("notes");
var descriptionsAttributes = getAllAttributeValues("descriptions");
var tablesAttributes = getAllAttributeValues("tables");
//===================================
function getAllAttributeValues(attributeName){
    var allAttributes = [];
    var allNodes = allElements[0].xpath("//*[@"+attributeName+"]");
    for(var i = 0; i < allNodes.length(); i++){
        allAttributes.push(allNodes[i].attribute(attributeName).toString());
        }
    if(allAttributes.length > 0){
        return allAttributes;
        }
    else{
        return null;
        }
    }
//===================================

 

Best

Sunil

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2020 1
Adobe Community Professional ,
Oct 05, 2020

Copy link to clipboard

Copied

And If you want to read xml attributes values from InDesign File then you can try this code snippet:

//==================================================================
var featureAttributes = getAllAttributeValuesFromINDDFile("features");
var notesAttributes = getAllAttributeValuesFromINDDFile("notes");
var descriptionsAttributes = getAllAttributeValuesFromINDDFile("descriptions");
var tablesAttributes = getAllAttributeValuesFromINDDFile("tables");
//==================================================================
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;
        }
    }
//==================================================================

Best

Sunil

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 05, 2020 1
Community Beginner ,
Oct 06, 2020

Copy link to clipboard

Copied

Hi,

I have used this code to fetch the table details but I used to get all the xml contents so please guide me to fetch the table values. Hereby I have attached the code I hav used and an screenshots which I used to get it while executing the code. Please guide me.

var myXMLFile = File("C:/Users/user/Desktop/InDesign/XML/Parts.xml");
myXMLFile.open('r');
var allElements = new XML(myXMLFile.read());
myXMLFile.close();

var nodes = allElements[0].xpath("//tables");
for(var i = 0; i < nodes.length(); i++){
alert (nodes[i])
}

 

script.png

Rajendran

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 06, 2020 0
Community Beginner ,
Oct 07, 2020

Copy link to clipboard

Copied

Hi,

While using this I get the null value as an output. I can't able to fetch the values.

Rajendran

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 07, 2020 0
Community Beginner ,
Oct 07, 2020

Copy link to clipboard

Copied

Hi,

By using this i get an null values as an attributes values. Hereby I attached the code to fetch the xml content, but I need to fetch the attributes values. Please give me some more suggestion

 

var myXMLFile = File("C:/Users/Desktop/InDesign/XML/Parts.xml");
myXMLFile.open('r');
var allElements = new XML(myXMLFile.read());
myXMLFile.close();
var nodes = allElements[0].xpath("//product_family");
for(var i = 0; i < nodes.length(); i++){
alert (nodes[i])
}

Rajendran

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 07, 2020 0
Adobe Community Professional ,
Oct 12, 2020

Copy link to clipboard

Copied

Can you elaborate a little bit more what exactly are you trying to do, then I think I can guide you a bit more.

 

Best

Sunil

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 12, 2020 0
Community Beginner ,
Oct 12, 2020

Copy link to clipboard

Copied

Hi,

I need to fetch the xml contents from indesign document.For that I have used the following code . While using that script I used to get all the content present in the xml, but I need to fetch the attribute values. At the sametime I have used the above code for reading xml files from InDesign Document but it doesn't give any value it provides an empty value. Please guide me on this..

Rajendran

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 12, 2020 0
Community Beginner ,
Oct 12, 2020

Copy link to clipboard

Copied

Hi

I have attached the screenshot what I used to get the xml attribute values.

Rajendran_T_0-1602494538501.png

 

Rajendran

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 12, 2020 0
Adobe Community Professional ,
Oct 12, 2020

Copy link to clipboard

Copied

Hi Rajendran,

If you are trying to fetch all attributes value from XML just use this code snippet:

var myXMLFile = File("C:/Users/Desktop/InDesign/XML/Parts.xml");
myXMLFile.open('r');
var allElements = new XML(myXMLFile.read());
myXMLFile.close();
var nodes = allElements[0].xpath("//product_family");
for(var i = 0; i < nodes.length(); i++){
    for(var a = 0; a < nodes[i].attributes().length(); a++){
        alert(nodes[i].attributes()[a].toString());
        }
    }

 And what is the confusion regarding finding node inside InDesign file?

 

Sunil

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 12, 2020 0
Adobe Community Professional ,
Oct 13, 2020

Copy link to clipboard

Copied

Hi Rajendran,

If you are looking for CDATA specifically.

Then you may use this code 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);

 

Nearly same question has been asked here => This link

 

Best

Sunil

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 13, 2020 0