Skip to main content
Known Participant
November 28, 2013
Question

How to get attribute name and its values from xml using javascript?

  • November 28, 2013
  • 1 reply
  • 4841 views

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() displays all the values available in product table..First it displays all the attribute names and then its values..It reads line by line..but i need to display each attribute name and its values. after displaying the first attribute and its values i want to go to next attribute name.please help me to complete this script.

Thanks in advance,

Vimala L

This topic has been closed for replies.

1 reply

Green4ever
Inspiring
November 28, 2013

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

Known Participant
November 28, 2013

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 nrows="3" ncols="2" TBGUID="47923389" Format="SuperTable" Transpose="0" aid5:tablestyle="TABLE" aid:trows="3" aid:tcols="2" aid:table="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">$999.99</price>

</TABLE>

</products>

Thanks,

Vimala L

Green4ever
Inspiring
November 28, 2013

Can you show us what is the expected output you want?