【问题标题】:IllegalStateException while parsing the XML?解析 XML 时出现 IllegalStateException?
【发布时间】:2015-04-14 14:02:41
【问题描述】:

在解析 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)

如果 XML 格式具有这种结构,那么它可以正常工作。

<Address>
    <Name>Rahul</Name>
    <ID>2345</ID>
    <City>Pune</City>
    <Street>Gandhi Nagar</Street>
</Address>

如果任何字段值为空,则生成标签,如&lt;phone/&gt; 然后在解析时我得到以下异常。

<Address>
    <Name>Rahul</Name>
    <ID>2345</ID>
    <City/>
    <Street>Gandhi Nagar</Street>
</Address>

我尝试在catch 块中添加IllegalStateException 异常,但它仍然抛出异常。

我的代码如下,

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

我们如何处理异常?

【问题讨论】:

    标签: java xml xml-parsing stax


    【解决方案1】:

    我觉得你的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>
    

    【讨论】:

    • 记住格式良好的 XML 应该有一个根标记很有用。
    • 实际上我遇到了问题 这不是父标签的问题。我提交的 xml 文件带有 null 值,例如 因此它抛出了异常。我该如何解决它
    【解决方案2】:

    更改您的代码以执行以下操作以利用 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);               
    } 
    

    【讨论】:

    • 出现解析错误,消息:elementGetText() 函数需要纯文本元素,但遇到了 START_ELEMENT wa。
    • @Precious - 查看使用 hasText() 方法的更新。
    • 现在异常已清除,但所有值都带有空值。
    • 它甚至没有进入 hasText() 循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多