Copy link to clipboard
Copied
I am trying to use javascript to read an XML file with a custom namespace and feed the contents of several nodes into a series of javascriopt variables.
I have gone through numerous posts in this forum and others (see list below), but have not found an answer that works, doesn't contain a reference link to as dead url, or that I can understand (I am pretty new to js).
That said, I am clearly not the first to struggle with this issue.
Some of the specific posts I have looked at include:
So far, I have not been able to get anything to work!
The top of my XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../CO-Medicare_Combined-Structure.xsd"?>
<!DOCTYPE Root [
<!ENTITY path "file:///C:/Users/O146962/OneDrive - Kaiser Permanente/Desktop/Indesign Automation POC/Assets/Icons/">
]>
<Root xmlns="kpPDCommon" xmlns:kpdc="kpPDCommon">
<region kpdc:regionCD="CO">CO</region>
<lob kpdc:lobCD="MCAR">Medicare</lob>
<serviceArea>Denver Boulder</serviceArea>
<planName>Senior Advantage Medicare Medicaid</planName>
I am trying to get the contents of "region", "lob", "serviceArea", and "planName" and use that to popluate js variables. However, even just trying to check to see if the file contains "Root" comes back as "Undefined"!
Here is my current javascript:
#include "glue code.jsx";
var mainFolder = Folder.desktop + "/Indesign Automation POC";
var templateFolder = mainFolder + "/Template Files - CO";
var outputFolder = mainFolder + "/ID_ScriptOutput";
var dataPath = mainFolder + "/Data/CO_DNB-XML_TEST_SPLIT";
//var file = File(dataPath + "/CO_DNB_Details.xml");
var file = dataPath + '/CO_DNB_Providers_Test_1.xml';
var nskpdc = new Namespace ("kpdc", "kpPDCommon");
var xmlFile = new XML(file);
//xmlFile.addNamespace(nskpdc);
//var nspc = xmlFile.contains("kpdc:Root");
var nspc = xmlFile.contains("Root");
var RGN = xmlFile.elements("region")[0];
var LOB = xmlFile.elements("lob")[0];
var SVCA = xmlFile.elements("serviceArea")[0];
var SVCA_PN = xmlFile.elements("planName")[0];
alert(nspc)
//alert('RGN: ' + RGN + ', LOB: ' + LOB + ', SVCA: ' + SVCA + ', Plan: ' + SVCA_PN);
I have tried declaring file as a File. I have tried using XML(file.read()). I have tried declaring my namespace. However I am obviously not doing any of this correctly or it should work. 😅
Appreciate any help!
var f = File (Folder.desktop+"/test.xml");
f.encoding ="UTF-8";
f.open("r");
var x = XML(f.read());
f.close();
alert(x.xpath("//kpdc:serviceArea"));
Copy link to clipboard
Copied
Thanks @Loic.Aigon !