简体   繁体   English

解析XML时出现IllegalStateException吗?

[英]IllegalStateException while parsing the XML?

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. 解析XMl时,如果XML具有一个父标记,则它工作正常;如果XML具有多个父标记,则它引发以下异常。

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. 如果XML格式具有此结构,则可以正常工作。

<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. 如果any字段值为null,则标记将生成为<phone/>然后在解析的时候出现以下异常。

<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. 我尝试在catch块中添加IllegalStateException异常,但仍引发异常。

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? 我们如何处理异常?

I think your xml should be like this 我认为您的xml应该是这样的

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

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: 更改代码以执行以下操作以利用getElementText()方法,而不是尝试前进到可能不存在的文本节点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM