Skip to main content
revathiv80720177
Known Participant
May 7, 2018
Answered

Getting XML attribute value

  • May 7, 2018
  • 1 reply
  • 2731 views

Hi,

I tried to get particular attribute  value of a element in xml.I used

var famid=mySel.associatedXMLElement.parent.xmlAttributes.itemByName("family_id").value;  

This worked for me for some elements.But for some elements it returned 'Object is Invalid" error.What should be done to get the attribute value.?Please do guide on this..

Regards,

Revathi V

This topic has been closed for replies.
Correct answer BarlaeDC

Hi Uwe,

Thanks for your guidance.I have uploaded the document and the link is

Link:  https://www.dropbox.com/s/ht5wb3vxrpe0b7h/Sample1.idml?dl=0https://www.google.com/url?q=https://www.dropbox.com/s/ht5wb3vxrpe0b7h/Sample1.idml?dl%3D0&sa=D&source=hangouts&ust=1525856229236000&usg=AFQjCNHBYFrOvOtnnbg69ciMCu-jdPtuuA

In this document I have placed two xml elements.Please do run the script by selecting the frame.

mySel=app.selection[0];

var famid=    mySel.associatedXMLElement.parent.xmlAttributes.itemByName("family_id").value;

alert(famid);

For the first element I am able to get the family id but for the second one I get "Object is invalid"error.How can I get the attribute value here.

Regards,

Revathi


Hi,

I think I have found the problem when running your code this is the result :

Text Frame one  ( with the URI looking contents) has 4 attributes - COTYPE, family_id, sub_Families, TBGUID

Whereas

Text Frame two ( with the random text) has only 2 attributes - COTYPE, TBGUID

Therefore the code is failing on the itemByName ("family_id"); part because there is no "family_id" attribute to get.

The reason for this appears to be the structure of your XML in the document.

For text frame one it is a direct descendant of the "product_family" tag,  (

whereas

text frame two is a child of a direct descendant of the "product_family" tag.

To fix the code to work for the second text frame you need to add an extra ".parent" so you would have.

mySel=app.selection[0]; var famid=    mySel.associatedXMLElement.parent.parent.xmlAttributes.itemByName("family_id").value; alert(famid);

obviously this would then cause the first frame to be incorrect, so you need to somehow detect if you are at the correct level, I would suggest doing something like

mySel=app.selection[0];

var parentNode = mySel.associatedXMLElement.parent

var nodeValue = parentNode.xmlAttributes.itemByName("family_id")

if ( !nodeValue.isValid)

{

     nodeValue = parentNode.parent.xmlAttributes.itemByName("family_id");

}

nodeValue = nodeValue.value;

alert ( nodeValue);

Hope this helps

Malcolm

1 reply

BarlaeDC
Participating Frequently
May 7, 2018

HI,

Can you post/share a document that shows the problem?

Regards

Malcolm

revathiv80720177
Known Participant
May 8, 2018

Hi Malcolm,

Thanks for your response.I tried to attach a document..but i couldn't..How can I attach a document here?

Regards,

Revathi

Brainiac
May 8, 2018

Hi Revathi,

attaching documents is not possible / allowed.

Upload your files to a service like Dropbox and post the download link here.

Regards,
Uwe