Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get node name with REXML

Tags:

ruby

rexml

I have an XML, which can be like

<?xml version="1.0" encoding="utf-8"?>
<testnode type="1">123</testnode>

or like

<?xml version="1.0" encoding="utf-8"?>
<othernode attrib="true">other value</othernode>

or the root node can be something completely unexpected. (Theoretically anything.) I'm using REXML to parse it. How can I find out what XML node is the root element?

like image 725
ytg Avatar asked Nov 16 '10 09:11

ytg


1 Answers

xml = REXML::Document.new "<?xml version" #etc (or load from file)
root_node = xml.elements[1]
root_node_name = root_node.name
like image 121
Skilldrick Avatar answered Oct 17 '22 07:10

Skilldrick