Skip to main content
Inspiring
March 25, 2022
Answered

Get the original content from the XML in the InDesign Document

  • March 25, 2022
  • 2 replies
  • 648 views

I have fetched the xml content having bullet points. When I have fetched the xml value then it get displaced as anʉۢ.And I have used the following code to get the content of XML. How can I replace this particular special character to bullet point?

 

if(inputXML.exists == true) {
inputXML.open('r');
var text = inputXML.read();
inputXML.close();
var content = new XML(text);
var elements = content.xpath("//CDATA");
for(var n = 0; n < elements.length(); n++){
if(elements[n].parent()["@"+attribute].toString() == id){
CDATA.push(elements[n].toString());
// alert(elements[n].toString())
}
}
}

This topic has been closed for replies.
Correct answer Dirk Becker

Your particular problem with bullets sounds like the file is read with the wrong encoding.

File.open does not know about XML, so it ignores the encoding= in the XML declaration, instead it just has a limited peek (some few characters) into the file when it has to guess the applied encoding.

Set inputXML.encoding = "UTF-8" before your open might get you further.

 

The rest of above script is a futile attempt to deal with all those CDATA - better read up the basics of XML, CDATA is separate concept and not addressed like elements. You might get further with XPath expressions such as  //*[contains(text(),'CDATA')] but I fear it will be more complicated.

 

Anyway, the file is already peppered with xmlns:aid and so forth that suggests it is intended for InDesign's document structure XML, a different beast than the ExtendScript xml technology that you're currently using. Please reconsider, this will be more things to learn but would also give you an XSLT processor on import.

2 replies

Community Expert
March 29, 2022

You have pasted incomplete code that does not even execute and errors out. The definition of the variables used is missing, how can someone guess then what results you get and how to test.

Specifically the variables inputXML, CDATA, attribute, id have missing definitions, out of which CDATA, attribute, id would be needed to atleast understand what the problem is.

-Manan

 

-Manan
Inspiring
March 29, 2022

From the previous forum suggestion, I had fetched the XML content. But in that, I'm facing an issue. In the XML there are some special characters while reading the content the special character values like (,') are replaced by (•, ’). And for this, I have attached the XML, INDD document, and script which I have been used for getting the XML contents.

 

https://1drv.ms/u/s!AvQ7CeTqFL3EiVuJOhiPk_Ha4Kqj 

-Monisha
Dirk BeckerCorrect answer
Legend
March 29, 2022

Your particular problem with bullets sounds like the file is read with the wrong encoding.

File.open does not know about XML, so it ignores the encoding= in the XML declaration, instead it just has a limited peek (some few characters) into the file when it has to guess the applied encoding.

Set inputXML.encoding = "UTF-8" before your open might get you further.

 

The rest of above script is a futile attempt to deal with all those CDATA - better read up the basics of XML, CDATA is separate concept and not addressed like elements. You might get further with XPath expressions such as  //*[contains(text(),'CDATA')] but I fear it will be more complicated.

 

Anyway, the file is already peppered with xmlns:aid and so forth that suggests it is intended for InDesign's document structure XML, a different beast than the ExtendScript xml technology that you're currently using. Please reconsider, this will be more things to learn but would also give you an XSLT processor on import.

Community Expert
March 25, 2022

Please sbare the xml file. I think this might be related to encoding of the file object you create, but can't be sure unless we test it.

-Manan

-Manan
Inspiring
March 28, 2022

Hi @Manan Joshi 

 

Hereby I have attached the XML file and InDesign document for reference. 

-Monisha