Get node name with REXML
Asked Answered
S

1

5

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?

Sicklebill answered 16/11, 2010 at 9:30 Comment(0)
T
11
xml = REXML::Document.new "<?xml version" #etc (or load from file)
root_node = xml.elements[1]
root_node_name = root_node.name
Turnedon answered 16/11, 2010 at 9:37 Comment(2)
Thanks. In my defense it is not in the documentation. Or at least I wasn't able to find it.Sicklebill
No, I wasn't able to find it in the docs either - I had to Google in the end.Turnedon

© 2022 - 2024 — McMap. All rights reserved.