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

Get the Attribute content of XML

Participant ,
Oct 11, 2021 Oct 11, 2021

Hi,

I need to get the content of the xml attribute values. In which I need to fetch the CDATA values based on the Attribute name for that I have attached the xml document which contains the CDATA values with the attributename ("TBGUID").

 

indd.png

-Monisha
TOPICS
How to , Scripting
700
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

correct answers 1 Correct answer

Advocate , Oct 19, 2021 Oct 19, 2021

As far as unique ID is concerned, you can try this simpler code : 

 

var file = File('~/Desktop/cdata.xml');
var returnedValue = getCDATAWithID(file, "TBGUID", "SAV1149696081");
if(returnedValue.length > 0) alert (returnedValue);
else alert ("Not Found", "Not Found", true)
//==============================
function getCDATAWithID(inputXML, attribute, id){
    var CDATA = [];
    if(inputXML.exists == true) {
        inputXML.open('r');
        var text = inputXML.read();
        inputXML.close();
...
Translate
Community Expert ,
Oct 11, 2021 Oct 11, 2021

Could you try again describing what you need, I unfortunately could not understand it at all.

-Manan

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
Participant ,
Oct 11, 2021 Oct 11, 2021

I have fetched the content of the xml using the attribute name, but I need to get the content of the xml file which is present inside the CDATA values using an unique id (TBGUID). I have attached the code which is used to get the content using the attribute name. Please guide me on this.,,

 

var allCDATA = [];
var myXMLFile = File("D:/XML/Parts.xml");
myXMLFile.open('r');
var allElements = new XML(myXMLFile.read());
myXMLFile.close();
var allFeatureElements = allElements.xpath("//features");
for(var n = 0; n < allFeatureElements.length(); n++){
if(allFeatureElements.children().length() > 0){
getChildData(allFeatureElements);
}
}
function getChildData(nodeData){
for(var c = 0; c < nodeData.children().length(); c++){
if(nodeData.children()[c].toXMLString().toString().indexOf("<![CDATA[") == 0){
alert(nodeData.children()[c].toString());
allCDATA.push(nodeData.children()[c].toString());
}
if(nodeData.children()[c].children().length() > 0){
getChildData(nodeData.children()[c]);
}
}
}
alert(allCDATA);

-Monisha
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
Advocate ,
Oct 18, 2021 Oct 18, 2021

It looks like this question has already been asked in past & resolved as well.

 

Here is the link : Fetching xml contents from INDD 

Here is the code snippet :

var allCDATA = [];
var myXMLFile = File("C:/Users/r.sunil/Desktop/TestingCDATA/Test.xml");
myXMLFile.open('r');
var allElements = new XML(myXMLFile.read());
myXMLFile.close();
var allFeatureElements = allElements.xpath("//feature");
for(var n = 0; n < allFeatureElements.length(); n++){
    if(allFeatureElements.children().length() > 0){
        getChildData(allFeatureElements);
        }
    }
//===================
function getChildData(nodeData){
    for(var c = 0; c < nodeData.children().length(); c++){
        if(nodeData.children()[c].toXMLString().toString().indexOf("<![CDATA[") == 0){
            alert(nodeData.children()[c].toString());
            allCDATA.push(nodeData.children()[c].toString());
            }
        if(nodeData.children()[c].children().length() > 0){
            getChildData(nodeData.children()[c]);
            }
        }
    }
alert(allCDATA);

Best

Sunil

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
Advocate ,
Oct 19, 2021 Oct 19, 2021

As far as unique ID is concerned, you can try this simpler code : 

 

var file = File('~/Desktop/cdata.xml');
var returnedValue = getCDATAWithID(file, "TBGUID", "SAV1149696081");
if(returnedValue.length > 0) alert (returnedValue);
else alert ("Not Found", "Not Found", true)
//==============================
function getCDATAWithID(inputXML, attribute, id){
    var CDATA = [];
    if(inputXML.exists == true) {
        inputXML.open('r');
        var text = inputXML.read();
        inputXML.close();
        var content = new XML(text);
        var elements = content.xpath("//CDATA");
        for(var n = 0; n < elements.length(); n++){
            if(elements[n].parent()["@"+attribute].toString() == id){
                CDATA.push(elements[n].toString());
                }
            }
        }
    return CDATA;
    }

 

Best

Sunil

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
Advocate ,
Oct 20, 2021 Oct 20, 2021
LATEST

Is it working for you?

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