1

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?

2 Answers 2

1

I think your xml should be like this

<Root>
    <Address>
       <Name>Rahul</Name>
       <ID>2345</ID>
       <City>Pune</City>
       <Street>Gandhi Nagar</Street>
    </Address>
    <ContactAddress>
      <phone>223363</phone>
      <mobile>9988776655</mobile>
    </ContactAddress>
</Root>
Sign up to request clarification or add additional context in comments.

2 Comments

Is useful to remember that a well-formed XML should have a root tag.
Actually I got the issue It is not the issue with parent tags. I my xml one filed is coming with null value like, <phone/> So for that reason it is throwing the exception. How can I resolve it
1

Change your code to do the following to leverage the getElementText() method instead of attempting to advance to a text node that may not exist instead:

int e = xr.next();
if (e == XMLStreamReader.START_ELEMENT) {
    String name = xr.getLocalName();
    // xr.next();
    String value = null;
    try{
        if(xr.hasText()) {
            value = xr.getElementText(); // was xr.getText();
        }
    }
    catch(IllegalStateException ex)
    {
        ex.printStackTrace();
    }
    map.put(name, value);               
} 

4 Comments

Getting parse error, Message: elementGetText() function expects text only element but START_ELEMENT wa encountered.
@Precious - See update that uses hasText() method.
Now the exception is cleared but all the values are coming with null value.
It is not even entered in to the hasText() loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.