XmlStreamReader非法状态异常

时间:2015-06-28 19:38:31

标签: java xml illegalstateexception xmlstreamreader

我正在尝试解析一个xml文件,其中包含的行如下所示:

<z:row TapID="4501" GesprekID="1358900" Onderzoek="123456789" NummerBeller="316987654321" TenaamstellingBeller="Pieter de Klerck, geboren 28-10-1980" NummerGebelde="316123456789" TenaamstellingGebelde="de Klerck B.V" Aard="Spraak" IMSIGebelde="098765432112345" Sessienummer="46" Wanneer="2014-11-09T10:49:25" Duur="39" ProductID="1000134498162" PaalGebeldeStartUniek="204-16-60076-33924" PaalGebeldeEindUniek="204-16-60076-33924"/>

我使用以下代码来解析“行”记录:

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(file);
while (reader.hasNext()) {
  int event = reader.next();
  switch (event) {
      case XMLStreamConstants.START_ELEMENT:
      if ("row".equals(reader.getLocalName())) {
          for(int x = 0; x < reader.getAttributeCount(); x++){
             System.out.println(reader.getAttributeName(x) +": " + reader.getAttributeValue(x);)
          } 
      }
  }
}

但我一直收到错误:

java.lang.IllegalStateException: Current state is not among the states START_ELEMENT , ATTRIBUTEvalid for getAttributeName()

有人可以向我解释为什么有可能获得AttributeValue但是在被问到AttributeName时我一直都会遇到错误吗?

0 个答案:

没有答案