Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Nov 28, 2013 Nov 28, 2013

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

TOPICS
Scripting
4.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 28, 2013 Nov 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 28, 2013 Nov 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 28, 2013 Nov 28, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 28, 2013 Nov 28, 2013

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/">

- <catalog_name COTYPE="TEXT" TBGUID="CN1303362806" aid:pstyle="catalog_name">
</catalog_name>
<description ................></description>
<productfamily..........>
<category_id............................><category_id>
<description ................></description><productsnrows="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>

      </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






 

-
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 29, 2013 Nov 29, 2013

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(); alert(attr);    //here it displays the attribute name(from the above                                                                                                                                                xml,attribute names are SKU and Price.)     

           }

             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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 29, 2013 Nov 29, 2013

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);

                    }

            }

        }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 01, 2013 Dec 01, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 02, 2013 Dec 02, 2013

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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 02, 2013 Dec 02, 2013

In the below line i have used the alert,It shows empty space

   var Header = (root.xpath("/products/table/*/child::text()"));

alert(Header);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 02, 2013 Dec 02, 2013

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 02, 2013 Dec 02, 2013
LATEST

Thank u so much..Now its working..

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines