IllegalStateException while parsing the XML?

2019-08-03 08:37发布

问题:

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?

回答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>


回答2:

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);               
}