XML 파일 (소스)을 파싱하고 새 파일 (결과)을 동시에 작성하고 있습니다.XML을 파싱하는 동안 ArrayIndexOutOfBoundsException이 발생했습니다.
<?xml version="1.0"?>
<poll>
<pollHead>
<code>1</code>
<title>Here's the title</title>
<description>Desc</description>
<author>Hilbert Von Neumann</author>
<date>2013-09-15T11:53:00</date>
</pollHead>
<pollBody>
<topic code="T1">
<topicHead>
<title>Topic title bla</title>
<description>description bla bla</description>
</topicHead>
<topicBody>
<question code="T1Q1" type="unique">How do you blabla?</question>
<option code="T1Q1_1">bla bla</option>
<option code="T1Q1_2">ok bla</option>
<option code="T1Q1_3">strabla</option>
<question code="T1Q2" type="unique">Are they bli bli?</question>
<option code="T1Q2_1">Lorem Ipsum</option>
<option code="T1Q2_2">Lorem Ipsum2</option>
<option code="T1Q2_3">Lorem Ipsum3</option>
</topicBody>
</topic>
</pollBody>
</poll>
나는이 소스 XML 코드를 분석하고 <answer>
요소의 <option>
요소를 변환하는 새로운 XML을 작성하려는 :
여기에 소스 XML입니다. 그러나 결과 XML에는 code
속성을 가지고있는 요소 인 answer
요소 만 나열되어야하며 그 값은 특정 HashMap에 포함되어 있습니다. 따라서 <option>
에 내 HashMap에 포함 된 코드가있는 경우 <option>
은 <answer>
이됩니다. 그렇지 않으면 결과 XML에 기록되지 않고 삭제되어야합니다. N.B : HashMap의는이 코드를 작성했습니다
아래의 코드에 questionAnswers
라고하지만, 나에게는 ArrayIndexOutOfBoundsException를 제공한다. 여기에 코드입니다 :
while (xer.hasNext()) {
XMLEvent event = xer.nextEvent();
if (event.getEventType() == XMLEvent.START_ELEMENT && event.asStartElement().getName().getLocalPart().equals("option")) {
Iterator ite = event.asStartElement().getAttributes();
while (ite.hasNext()) {
Attribute attr = (Attribute) ite.next();
if (attr.getName().getLocalPart().equals("code") && questionAnswers.containsValue(attr.getValue())) {
String optionCodeValue = attr.getValue();
writer.add(eventFactory.createStartElement("", null, "answer"));
writer.add(eventFactory.createAttribute("code", optionCodeValue));
}
}
event = xer.nextEvent();
} else {
writer.add(event);
}
}
writer.close();
그리고 여기에 오류 발생 :
javax.xml.stream.XMLStreamException: No element was found to write: java.lang.ArrayIndexOutOfBoundsException: -1
at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeEndElement(XMLStreamWriterImpl.java:930)
at com.sun.xml.internal.stream.writers.XMLEventWriterImpl.add(XMLEventWriterImpl.java:186)
...
내가 여기에 놓친 거지 무슨 일이? 뭐가 문제 야?
굉장 대답은, 내가 당신 같은 발견되는 사용자 기뻐요. 우수, 감사합니다 :) –
감사. 기쁜 데 도움이되었습니다. –