IllegalStateException while parsing the XML?

纵然是瞬间 提交于 2019-12-11 09:32:53

问题


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


来源:https://stackoverflow.com/questions/28501128/illegalstateexception-while-parsing-the-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!