Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Won't Fix
-
Labels:None
-
Number of attachments :
Description
(from mailing list, by Fred Bricon)
—
Using code below, it seems I can't read both contents.
I would get the following exception when trying to create the "files" cursor:
java.lang.IllegalStateException: Child cursor already requested.
at org.codehaus.staxmate.in.SMInputCursor.descendantCursor(SMInputCursor.java:1715)
at org.codehaus.staxmate.in.SMInputCursor.descendantElementCursor(SMInputCursor.java:1816)
at foo.bar.StaxMateTest.testParsing(StaxMateTest.java:61)
How can I achieve the reading of these separate nodes? Is mixing childElementCursor and descendantElementCursor appropriate?
I guess iterating on the same cursor -while(null != branch.childCursor().next()) -
and detecting the value of the current node (if getLocalName == deep_children => start looking for files) would do it,
but I'm looking for a more elegant solution, if it exists.
—
package foo.bar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.io.StringReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.codehaus.staxmate.SMInputFactory;
import org.codehaus.staxmate.in.SMEvent;
import org.codehaus.staxmate.in.SMInputCursor;
import org.junit.Test;
public class StaxMateTest {
@Test
public void testParsing() throws XMLStreamException {
String XML = "<?xml version='1.0' encoding='UTF-8'?>\n"
+ "<root>\n"
+ "<branch>"
+ "<branch_name>branch name</branch_name>"
+ "<branch_directory>//home//dir</branch_directory>"
+ "<branch_version>1.0</branch_version>"
+ "<noise name_count=\"1\">"
+ "<ignored id='toto'/>"
+ "</noise_stuff>"
+ "<deep_children deep_child_count=\"1\">"
+ "<deep_child date='2010-10-15'>"
+ "<files file_count='3'>"
+ "<file file_name='file1.txt' />"
+ "<file file_name='file2.txt' />"
+ "<file file_name='file3.txt' />"
+ "</files>"
+ "</deep_child>"
+ "</deep_children>"
+ "</branch>\n"
+ "</root>";
XMLStreamReader sr = XMLInputFactory.newInstance()
.createXMLStreamReader(new StringReader(XML));
SMInputCursor root = SMInputFactory.rootElementCursor(sr);
assertEquals(SMEvent.START_ELEMENT, root.getNext());
assertEquals("root", root.getLocalName());
SMInputCursor branch = root.childElementCursor("branch");
assertEquals(SMEvent.START_ELEMENT, branch.getNext());
assertEquals("branch", branch.getLocalName());
assertEquals(1, branch.getParentCount());
SMInputCursor directory = branch.childElementCursor("branch_directory");
assertEquals(SMEvent.START_ELEMENT, directory.getNext());
assertEquals(2, directory.getParentCount());
assertEquals("branch_directory", directory.getLocalName());
assertEquals(0, directory.getAttrCount());
assertEquals("//home//dir", directory.collectDescendantText(false));
SMInputCursor files = branch.descendantElementCursor("files").advance(); //Fails here : "Child cursor already requested."
assertEquals("files", files.getLocalName());
int nbFiles = files.getAttrIntValue(0);
assertEquals(3, nbFiles);
SMInputCursor fileCursor = files.childCursor();
int i = 0;
while (null != fileCursor.getNext())
assertEquals(nbFiles, i);
assertNull(root.getNext());
sr.close();
}
}
You are correct: you can not create more than one active child/descendant cursor.
So code will not work as is.