SAX Parser: How to fetch root element of XML in java without explicitly giving root name?

Upz :

I'm using SAX Parser to convert XML into CSV format. Here, I need to fetch the root element of any given XML file. I know that I can do the task by using the following snippet.

if (!"book".equalsIgnoreCase(qName)) {
   .......
}

But I want to fetch the root element name from any given XML file automatically instead of explicitly defining it as "book". Because my intention is to generate CSV from any input XML file but with only using the SAX Parser. Can anyone help me with my problem? Thanks in advance!

kjhughes :

There can only be a single root element in an XML document, and it will necessarily be the first element encountered, so simply save the element name (localName or QName – both are provided) the first time your startElement() callback is called.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=308206&siteId=1