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

Read the Image Files in the XML files

Participant ,
Nov 21, 2023 Nov 21, 2023

Hi,

I have used the script below to retrieve the image_type values from an XML file. However, I am unable to fetch the values when reading the XML using the script. I have attached both the script and XML file. Could you please guide me on how to resolve this issue?

var xmlString = GetXMLFile();
var imageFiles = [];
var splitImageFiles = [];
var folderPaths = [];
var combinedResults = [];

var imageNodes = xmlDoc.image_type.children();
alert(imageNodes)
// Populate imageFiles and splitImageFiles arrays
for (var i = 0; i < imageNodes.length(); i++) {
    var imageFile = imageNodes[i].@IMAGE_FILE.toString();

    if (imageFile) {
        imageFiles.push(imageFile);
        splitImageFiles.push(imageFile.split(','));
    }
}

for (var j = 0; j < splitImageFiles.length; j++) {
    alert("Group " + (j + 1) + ": " + splitImageFiles[j].join(', '));
}

// Check if the folder path contains "\Images" and populate folderPaths array
var doc = app.activeDocument;
for (var k = 0; k < doc.links.length; k++) {
    var link = doc.links[k];
    var filePath = link.filePath;
    var file = new File(filePath);
    var folderPath = file.parent.fsName;
     if (folderPath.indexOf("Images") !== -1) {
        // Split folderPath based on the word "Images"
        var pathParts = folderPath.split("Images");
        var partBeforeImages = pathParts[0];
        var partAfterImages = pathParts[1];
    }
   
}

// Concatenate splitImageFiles[j] and folderPaths after excluding common values
for (var m = 0; m < splitImageFiles.length; m++) {
    var combinedResult =   partBeforeImages + "\\"+ splitImageFiles[m].join(', ') ;
    combinedResults.push(combinedResult);
}

// Log the combined results
for (var n = 0; n < combinedResults.length; n++) {
    alert(combinedResults[n]);
}

function GetXMLFile()
{
	
		myDoc = app.activeDocument;
		myLinks = myDoc.links;
		for (j = myLinks.length - 1; j >= 0; j--)
		{  
			myName = myLinks[j].filePath; 
			var ext =  myName.substring(myName.length-3,myName.length) ;
			if (ext=="xml")
			{   
				var myXMLFile = File(myName);  
				var myResult = myXMLFile.open("r", undefined, undefined); 
				if(myResult == true)
				{ 
				var myXMLDefaults = myXMLFile.read(); 
               // alert(myXMLDefaults)
				myXMLFile.close(); 
				var myXMLDefaults = new XML(myXMLDefaults); 
				return myXMLDefaults;
				}			
		}//for
	}
	
}

 

 

-Monisha
TOPICS
How to , Scripting
224
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

Contributor , Nov 21, 2023 Nov 21, 2023

Hi Monisha,

you can use xPath from E4X, for example:

 

var imageTypes = myXMLDefaults.xpath("//image_type");
var firstImageFile = imageTypes["*"]["@IMAGE_FILE"][0];

 

 or

 

var imageFiles = myXMLDefaults.xpath("//image_type/*/@IMAGE_FILE");
$.writeln(imageFiles.length());

 

Roland

Translate
Contributor ,
Nov 21, 2023 Nov 21, 2023
LATEST

Hi Monisha,

you can use xPath from E4X, for example:

 

var imageTypes = myXMLDefaults.xpath("//image_type");
var firstImageFile = imageTypes["*"]["@IMAGE_FILE"][0];

 

 or

 

var imageFiles = myXMLDefaults.xpath("//image_type/*/@IMAGE_FILE");
$.writeln(imageFiles.length());

 

Roland

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