tags:

views:

29

answers:

2

Suppose I have downloaded a valid XML document via Ajax (var docum = request.responseXML;). Now how do I find out the name of the root element?

+3  A: 
docum.documentElement.tagName
KennyTM
A: 

If you have a string with xml then this will give you the first tag

var xml = "<foo><bar>......";
var tag = /<(.*?)>/.exec(xml)[1];
alert(tag) // foo

Alternatively you just access the node and retrieves the tagName

Sean Kinsey
`<?xml version="1.0" encoding="UTF-8" ?><foo><bar/></foo>`
KennyTM
Are you referring to what 'valid' XML is? If so then be aware that the `<?xml...?>` block is optional..
Sean Kinsey
@Sean: No, I mean your regex will show `?xml version="1.0" encoding="UTF-8" ?` as the tag name.
KennyTM