Highlighted

Extract tags from XML file

Community Beginner ,
Jul 27, 2020

Copy link to clipboard

Copied

With XML files structured like this, how to get name of all tags directly from the files in the order they appear.

</STORY>
  <EVENT>
    <TYPE>The event type</TYPE>
    <CODE>12 digits</CODE>
    <DESC>A description of the event</DESC>
    <CITY>Name of the city</CITY>
    <COUNTRY>Name of the country</COUNTRY>
    <DATE>start - end</DATE>
  </EVENT>
</STORY>

In this case the result should be : [STORY,EVENT,TYPE,CODE,DESC,CITY,COUNTRY,DATE]

All I am able to do is to get the name of the first tag. I can't find a way to loop thru every tag. 

Any help on this?

var xmlFile = File( "/Some/File.xml" ); 
xmlFile.open("r"); 
var myXMLObject = XML ( xmlFile.read() ); 
xmlFile.close(); 
$.writeln ( myXMLObject.name() );

 

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Your code does not work for me, but what i notice is that you need to define the array before you make the call to the function else it will be undefined. So the code should be as follows

var tagNames = new Array() ;
getNode(a.children()[1])

 

-Manan

TOPICS
Scripting

Views

63

Likes

Translate

Translate

Report

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

Extract tags from XML file

Community Beginner ,
Jul 27, 2020

Copy link to clipboard

Copied

With XML files structured like this, how to get name of all tags directly from the files in the order they appear.

</STORY>
  <EVENT>
    <TYPE>The event type</TYPE>
    <CODE>12 digits</CODE>
    <DESC>A description of the event</DESC>
    <CITY>Name of the city</CITY>
    <COUNTRY>Name of the country</COUNTRY>
    <DATE>start - end</DATE>
  </EVENT>
</STORY>

In this case the result should be : [STORY,EVENT,TYPE,CODE,DESC,CITY,COUNTRY,DATE]

All I am able to do is to get the name of the first tag. I can't find a way to loop thru every tag. 

Any help on this?

var xmlFile = File( "/Some/File.xml" ); 
xmlFile.open("r"); 
var myXMLObject = XML ( xmlFile.read() ); 
xmlFile.close(); 
$.writeln ( myXMLObject.name() );

 

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Your code does not work for me, but what i notice is that you need to define the array before you make the call to the function else it will be undefined. So the code should be as follows

var tagNames = new Array() ;
getNode(a.children()[1])

 

-Manan

TOPICS
Scripting

Views

64

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 27, 2020 0
Adobe Community Professional ,
Jul 27, 2020

Copy link to clipboard

Copied

Have not tested it fully but the following should give you a starting point

 

var xmlFile = File( "/Users/manan/Desktop/abc.xml" ); 
xmlFile.open("r"); 
var myXMLObject = new XML ( xmlFile.read() ); 
xmlFile.close(); 
getNode(myXMLObject)

function getNode(node)
{
	$.writeln(node.name())
	for(var i = 0; i < node.descendants().length(); i++)
	{
		if(node.descendants()[i].nodeKind() == "element")
			$.writeln(node.descendants()[i].name())
	}
}

 

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2020 0
Community Beginner ,
Jul 27, 2020

Copy link to clipboard

Copied

Hi Manan,

 

I've amended a bit you script and it returns exactly what I need.

 

var xmlFile = File( "/Users/manan/Desktop/abc.xml" ); 
xmlFile.open("r"); 
var a = new XML ( xmlFile.read() ); 
xmlFile.close(); 

getNode(a.children()[1])

function getNode(node)
{
	for(var i = 0; i < node.children().length(); i++)
	{
		$.writeln (node.children()[i].name())
		if(node.children()[i].children().length() > 1)
			getNode(node.children()[i])  
	}
}

 

 

But if I try to get the names as an array, I get an empty result:

 

var tagNames = new Array() ;
function getNode(node)
{
	for(var i = 0; i < node.children().length(); i++)
	{
		tagNames.push (node.children()[i].name())
		if(node.children()[i].children().length() > 1)
			getNode(node.children()[i])  
	}
}

$.writeln(tagNames);

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2020 0
Adobe Community Professional ,
Jul 27, 2020

Copy link to clipboard

Copied

Your code does not work for me, but what i notice is that you need to define the array before you make the call to the function else it will be undefined. So the code should be as follows

var tagNames = new Array() ;
getNode(a.children()[1])

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2020 0
Adobe Community Professional ,
Jul 27, 2020

Copy link to clipboard

Copied

To complete the discussion this is the script that works fine for me

var xmlFile = File( "/Users/manan/Desktop/abc.xml" ); 
xmlFile.open("r"); 
var myXMLObject = new XML ( xmlFile.read() ); 
xmlFile.close(); 
var tagNames = new Array() ;
getNode(myXMLObject)

function getNode(node)
{
	tagNames.push (node.name())
	for(var i = 0; i < node.descendants().length(); i++)
	{
		if(node.descendants()[i].nodeKind() == "element")
			tagNames.push (node.descendants()[i].name())
	}
}
$.writeln(tagNames);

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2020 0
zeRafio LATEST
Community Beginner ,
Jul 27, 2020

Copy link to clipboard

Copied

you need to define the array before you make the call to the function

That was the solution! 

 

To complete the discussion this is the script that works fine for me

Yep!

But your script returns the tags for each and every element of the file.

I need only the first.

 

Thank you for your help! 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2020 0