How to get attribute name and its values from xml using javascript?
Copy link to clipboard
Copied
Hi,
I am trying to get the attribute name and its values from xml(i have saved this xml in some folder) using javascript.the code has given below,
var currentFolder = Folder.selectDialog();
if (currentFolder == null) exit();
var currentFolderPath = currentFolder.absoluteURI + "/";
var xmlFile = new File(currentFolderPath +xmlname);
if (!xmlFile.exists) exit();
xmlFile.open("r");
var xmlStr = xmlFile.read();
xmlFile.close();
var root = new XML(xmlStr);
var linksArr = [];
var componentList = root.xpath("//product_family");
var componentsLength = componentList.length(); var component;
var product;var pricevalue;var products;var itemvalue;
for (var c = 0; c <componentsLength; c++)
{
component = componentList
product=component.xpath("//products").children()
for(var d=0;d<product.length();d++)
{
prod=product.children().length();
for(var x=0;x<product.children().length();x++)
{
attribute=product.children()
}
}
}
In this,attribute=product.children()
Thanks in advance,
Vimala L
Copy link to clipboard
Copied
See the following example to know how to get the attribute values using javascript.
var myXML = new XML("<root><p att1=\"value1\" att2=\"value2\"></p></root>");
var myXMLElement = myXML.xpath ("//p");
alert(myXMLElement.attributes()[0].name()+": "+myXMLElement.attributes()[0].toString());
~Green4ever
Copy link to clipboard
Copied
Thanks for ur quick response..I have used this code in my script,but it displays TBGUID="TPS807484739"..I dont know how to access the values..I need to display the value 10014 under SKU(Header) and display the price value $999.99 under Price(Header).please go through the below xml and help me..
<products nrows="3" ncols="2" TBGUID="P1585824296" Format="SuperTable" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">
</TABLE>
</products>
Thanks,
Vimala L
Copy link to clipboard
Copied
Can you show us what is the expected output you want?
Copy link to clipboard
Copied
Ya...sure.I will give the details....
I have saved my xml in some folder.my xml is,
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<root catalog_id="2" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">
</TABLE>
</products>
</productfamily></root>
I want to take the attribute name and its values...from the above xml, i need to display the value 10014 under SKU(Header) and display the price value $999.99 under Price(Header).
If we flow this xml in indesign, the product table will be in below format,
SKU Price
10014 $999.99
So i want to take the attribute name(SKU) first,then its value(10014).After that Price and its value($999.99) from xml.Because i need to update the xml value(available in some folder) in Indesign document.For example, in the xml i will change the price value as $10.00,then i need to update this new value in indesign document.
Thanks,
Vimala L
Copy link to clipboard
Copied
Hi,
I have done some changes in my script..please refer it and help me to complete this script.
var currentFolder = Folder.selectDialog();
if (currentFolder == null) exit();
var currentFolderPath = currentFolder.absoluteURI + "/";
var xmlFile = new File(currentFolderPath +xmlname);
if (!xmlFile.exists) exit();
xmlFile.open("r");
var xmlStr = xmlFile.read();
xmlFile.close();
var root = new XML(xmlStr);
var linksArr = [];
var componentList = root.xpath("//product_family");
var componentsLength = componentList.length(); var component;
var product;var pricevalue;var products;var itemvalue;
for (var c = 0; c <componentsLength; c++)
{
component = componentList
product=component.xpath("//products").children()
var row= product.attributes()[0];
var coln= product.attributes()[1];
var cnt=parseInt(coln);
var val=(parseInt(row)-1)*parseInt(coln);
for(var x=0;x<=parseInt(val);x++)
{
if(parseInt(coln)>x)
{
var attr=product.children()
}
if((x+parseInt(cnt))>=parseInt(val))
{
break;
}
var value=product.children()[x+parseInt(cnt)];alert(value); //here it displays all the values available in table(line by line)[from above xml, values are 10014 and $999.99]
}}
I need to display the attribute name first(SKU) and then the values under SKU(10014)..
For example, product table tag in xml,
<products><table>
<SKU_Header aid:ccols="1" aid:crows="2" aid5:cellstyle="SKU_Header" TBGUID="TPS807484739"product_id=""COTYPE="CELL" aid:theader="" Attribute_type="1" aid:table="cell">SKU</SKU_Header>
<price_Header aid:ccols="1" aid:crows="2" aid5:cellstyle="price_Header" TBGUID="TPS805428377"product_id=""COTYPE="CELL" aid:theader="" Attribute_type="4" aid:table="cell">Price</price_Header>
<SKU aid:ccols="1" aid:crows="1" aid5:cellstyle="SKU" TBGUID="TPS350334503" product_id="174"COTYPE="CELL"attribute_id="1" Attribute_type="1" aid:table="cell">10014</SKU>
<price aid:ccols="1" aid:crows="1" aid5:cellstyle="price" TBGUID="TPS637151899" product_id="174"COTYPE="CELL"attribute_id="46" Attribute_type="4" aid:table="cell">$10.00</price>
<SKU aid:ccols="1" aid:crows="1" aid5:cellstyle="SKU" TBGUID="TPS350334503" product_id="174"COTYPE="CELL"attribute_id="1" Attribute_type="1" aid:table="cell">10015</SKU>
<price aid:ccols="1" aid:crows="1" aid5:cellstyle="price" TBGUID="TPS637151899" product_id="174"COTYPE="CELL"attribute_id="46" Attribute_type="4" aid:table="cell">$20.00</price>
<SKU aid:ccols="1" aid:crows="1" aid5:cellstyle="SKU" TBGUID="TPS350334503" product_id="174"COTYPE="CELL"attribute_id="1" Attribute_type="1" aid:table="cell">10016</SKU>
<price aid:ccols="1" aid:crows="1" aid5:cellstyle="price" TBGUID="TPS637151899" product_id="174"COTYPE="CELL"attribute_id="46" Attribute_type="4" aid:table="cell">$30.00</price>
</table></products>
After flow the xml in indesign the table will be in below format,
SKU Price
10014 $10.00
10015 $20.00
10016 $30.00
From the above values,i want to display the name first(SKU),then the values under it(10014,10015,10016). After that Price and then the values under it( $10.00, $20.00,$30.00).I dont know how to modify this..help me to fix it.
Thanks,
Vimala L
Copy link to clipboard
Copied
Hi,
Please change "aid5:cellstyle='price'" to "aid5:cellstyle='Price'"('P' should be caps) in your xml file and try the below,
var currentFolder = Folder.selectDialog();
if (currentFolder == null) exit();
var currentFolderPath = currentFolder.absoluteURI + "/";
var xmlFile = new File(currentFolderPath +"test.xml");
xmlFile.open('r');
var content = xmlFile.read();
var root = new XML(content);
var Header = (root.xpath("/products/table/*/child::text()"));
for(var i =0;i<Header.length();i++){
var TableEntry = root.xpath("/products/table/*[@aid5:cellstyle = '"+ Header +"']/child::text()");
if(TableEntry != ""){
alert(Header);
for(var k=0;k<TableEntry.length();k++){
alert(TableEntry
}
}
}
Copy link to clipboard
Copied
Thanks for ur reply..I have tried ur script,but it shows the empty space when i give the alert(Header).how to fix it..?
Thanks,
Vimala L
Copy link to clipboard
Copied
The below code alerts only when the TableEntry is not equal to null, so check you code again,
var TableEntry = root.xpath("/products/table/*[@aid5:cellstyle = '"+ Header +"']/child::text()");
if(TableEntry != ""){
alert(Header);
Copy link to clipboard
Copied
In the below line i have used the alert,It shows empty space
var Header = (root.xpath("/products/table/*/child::text()"));
alert(Header);
Copy link to clipboard
Copied
Hi,
It seems that your xml doesn't match with the xpath, re-check your xml file.
<products>
<table>
<SKU_Header.....>SKU</SKU_Header>
</table>
</products>
Copy link to clipboard
Copied
Thank u so much..Now its working..

