While parsing the XMl, If the XML has one parent tag then it is working fine, If it has multiple parent tags then it is throwing the following exception.
java.lang.IllegalStateException: Current state END_ELEMENT is not among the statesCHARACTERS, COMMENT, CDATA, SPACE, ENTITY_REFERENCE, DTD valid for getText()
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.getText(Unknown Source)
at com.axxonet.queue.xmlParserValues.parse(xmlParserValues.java:37)
at com.axxonet.queue.xmlParserValues.main(xmlParserValues.java:19)
If XML format has this structure then it is working fine.
<Address>
<Name>Rahul</Name>
<ID>2345</ID>
<City>Pune</City>
<Street>Gandhi Nagar</Street>
</Address>
If the any field value is null the tag is generate like <phone/>
Then that time while parsing I am getting the following exception.
<Address>
<Name>Rahul</Name>
<ID>2345</ID>
<City/>
<Street>Gandhi Nagar</Street>
</Address>
I tried adding the IllegalStateException
exception in catch
block still it is throwing the exception.
My code is as follows,
Map<String, String> map = new HashMap<String, String>();
XMLStreamReader xr;
try {
xr = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream("E:/Softwares/eclipse/reqOutputFile.xml"));
while(xr.hasNext()) {
int e = xr.next();
if (e == XMLStreamReader.START_ELEMENT) {
String name = xr.getLocalName();
xr.next();
String value = null;
try{
value = xr.getText();
}
catch(IllegalStateException ex)
{
ex.printStackTrace();
}
map.put(name, value);
}
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (XMLStreamException e1) {
e1.printStackTrace();
} catch (FactoryConfigurationError e1) {
e1.printStackTrace();
}
How do we handle the exception?