Copy link to clipboard
Copied
Hi,
Can someone guide for finding the modified contents in the xml.
Regards,
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);
}
}
//========
...
Copy link to clipboard
Copied
Please provide more detail.
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.
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 .
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
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
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])
}
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.
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])
}
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
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..
Copy link to clipboard
Copied
Hi
I have attached the screenshot what I used to get the xml attribute values.
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
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