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

Extract tags from XML file

Participant ,
Jul 27, 2020 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() );

 

TOPICS
Scripting

Views

343

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 27, 2020 Jul 27, 2020

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

Votes

Translate

Translate
Community Expert ,
Jul 27, 2020 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

Votes

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
community guidelines
Participant ,
Jul 27, 2020 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);

 

 

 

Votes

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
community guidelines
Community Expert ,
Jul 27, 2020 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

Votes

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
community guidelines
Community Expert ,
Jul 27, 2020 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

Votes

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
community guidelines
Participant ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

LATEST
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! 

Votes

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
community guidelines