🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #14570

Re: XML Import

#14570
From: Mark_Volmer
Date: Thu, 22 Jan 2004 15:11
178 lines
5915 bytes
It took me quite some time to figure out how to set this up so I thought I'd share this for whomever cares ;-)

Here's a snippet from the XML file I tried to import automatically:

 
<SPEECH> 
<SPEAKER>PRINCE</SPEAKER> 
<LINE>This letter doth make good the friar's words,</LINE> 
<LINE>Their course of love, the tidings of her death:</LINE> 
<LINE>And here he writes that he did buy a poison</LINE> 
<LINE>Of a poor 'pothecary, and therewithal</LINE> 
<LINE>Came to this vault to die, and lie with Juliet.</LINE> 
<LINE>Where be these enemies? Capulet! Montague!</LINE> 
<LINE>See, what a scourge is laid upon your hate,</LINE> 
<LINE>That heaven finds means to kill your joys with love.</LINE> 
<LINE>And I for winking at your discords too</LINE> 
<LINE>Have lost a brace of kinsmen: all are punish'd.</LINE> 
</SPEECH>

<SPEECH> 
<SPEAKER>CAPULET</SPEAKER> 
<LINE>O brother Montague, give me thy hand:</LINE> 
<LINE>This is my daughter's jointure, for no more</LINE> 
<LINE>Can I demand.</LINE> 
</SPEECH>

<SPEECH> 
<SPEAKER>MONTAGUE</SPEAKER> 
<LINE>But I can give thee more:</LINE> 
<LINE>For I will raise her statue in pure gold;</LINE> 
<LINE>That while Verona by that name is known,</LINE> 
<LINE>There shall no figure at such rate be set</LINE> 
<LINE>As that of true and faithful Juliet.</LINE> 
</SPEECH>

<SPEECH> 
<SPEAKER>CAPULET</SPEAKER> 
<LINE>As rich shall Romeo's by his lady's lie;</LINE> 
<LINE>Poor sacrifices of our enmity!</LINE> 
</SPEECH>

<SPEECH> 
<SPEAKER>PRINCE</SPEAKER> 
<LINE>A glooming peace this morning with it brings;</LINE> 
<LINE>The sun, for sorrow, will not show his head:</LINE> 
<LINE>Go hence, to have more talk of these sad things;</LINE> 
<LINE>Some shall be pardon'd, and some punished:</LINE> 
<LINE>For never was a story of more woe</LINE> 
<LINE>Than this of Juliet and her Romeo.</LINE> 
</SPEECH> 




I created a masterPage defining textFrames which I gave scriptLabels named frmCardN, frmSpeaker and frmLines. Something like this;



 
|------------------------| 
|   |-------------|      | 
|   | frmCardN    |      | 
|   |-------------|      | 
|   | frmSpeaker  |      | 
|   |             |      | 
|   |-------------|      | 
|   | frmLines    |      | 
|   |             |      | 
|   |             |      | 
|   |-------------|      | 
|                        | 
|------------------------|





Then I took a snippet of code I found on this forum for itterating through the XML tree and started experimenting.
I don't have the time to go to deep in detail on it but I guess is pretty straightforward. The main idea is that you'll have to override masterpage items on a new page so that you can add content to it...



 
firstElement = app.activeDocument.xmlElements.item(0);  
var iNumOfSpeeches = 0; 
processElement(firstElement); 

function processElement(thisElement)  
{ 

	for (i=0; i<thisElement.xmlElements.length; i++)  
	{  
		processElement(thisElement.xmlElements.item(i));  
	} 	

	if (thisElement.markupTag.name == "SPEECH") 
	{ 
		thisElement.select(); 
		iNumOfSpeeches++ ; 
		var newPage 
		newPage = app.activeDocument.pages.add(); 
		for(i=0;i<newPage.appliedMaster.textFrames.length;i++) 
		{ 
			if(newPage.appliedMaster.textFrames.item(i).label.toString().substring(0, 8) == "frmCardN") 
			{

				overriddenFrame = newPage.appliedMaster.textFrames.item(i).override(newPage); 
				overriddenFrame.contents = iNumOfSpeeches.toString(); 
			} 
		}





	} 
	if (thisElement.markupTag.name == "SPEAKER") 
	{ 
		thisElement.select(); 
		var currentPage; 
		currentPage = app.activeDocument.pages.lastItem(); 
		for(i=0;i<currentPage.appliedMaster.textFrames.length;i++) 
		{ 
			try 
			{ 
				if(currentPage.appliedMaster.textFrames.item(i).label.toString().substring(0,10) == "frmSpeaker") 
				{

					overriddenFrame = currentPage.appliedMaster.textFrames.item(i).override(currentPage); 
					overriddenFrame.placeXML(thisElement); 
				} 
			} 
			catch(err) 
			{ 
				for(i=0;i<currentPage.textFrames.length;i++) 
				{ 
					if(currentPage.textFrames.item(i).label.toString().substring(0,10) == "frmSpeaker") 
					{ 
						targetFrame = currentPage.textFrames.item(i); 
						targetFrame.contents = targetFrame.contents + thisElement.contents; 
					}	 
				} 
			}

		}

	} 
	if (thisElement.markupTag.name == "LINE") 
	{ 
		thisElement.select(); 
		var currentPage; 
		currentPage = app.activeDocument.pages.lastItem(); 
		for(i=0;i<currentPage.appliedMaster.textFrames.length;i++) 
		{ 
			if(currentPage.appliedMaster.textFrames.item(i).label.toString().substring(0, 8) == "frmLines") 
			{ 
				try 
				{ 
				overriddenFrame = currentPage.appliedMaster.textFrames.item(i).override(currentPage);

				overriddenFrame.contents = overriddenFrame.contents + thisElement.contents; 
				} 
				catch(err) 
				{ 
					for(i=0;i<currentPage.textFrames.length;i++) 
					{ 
						if(currentPage.textFrames.item(i).label.toString().substring(0,8) == "frmLines") 
						{ 
							targetFrame = currentPage.textFrames.item(i); 
							targetFrame.contents = targetFrame.contents + thisElement.contents; 
						}	 
					} 
				} 
			} 
		}

	} 
}
Technical Details
Message-ID: <2ccf20ba.0@webx.la2eafNXanI>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!webx
References: <2ccf20ba.-1@webx.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f939a,1e77ea5981630d3e
X-Google-Attributes: gidf939a,public
X-Google-ArrivalTime: 2004-01-22 07:37:46 PST
From: Mark_Volmer@adobeforums.com
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!webx
Newsgroups: adobe.scripting.indesign
Subject: Re: XML Import
Message-ID: <2ccf20ba.0@webx.la2eafNXanI>
Date: Thu, 22 Jan 2004 07:11:48 -0800
References: <2ccf20ba.-1@webx.la2eafNXanI>
Lines: 185
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit