Issue Details (XML | Word | Printable)

Key: XMLBEANS-267
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Dmitri Colebatch
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
XMLBeans

toFirstAttribute/toNextAttribute doesn't work

Created: 18/Apr/06 01:06 PM   Updated: 19/Apr/06 06:14 AM
Component/s: XmlObject
Affects Version/s: Version 2.1
Fix Version/s: None

Time Tracking:
Not Specified

Environment: Windows XP, Intel, Sun JRE 1.5.0_04

Resolution Date: 19/Apr/06 12:32 AM


 Description  « Hide
Either I'm misinterpreting the expected usage of toNextAttribute, or it simply doesn't work. Here's my test code:

public static void main(String[] args) throws Exception
{
XmlObject xml = XmlObject.Factory.parse(new StringReader("<foo a=\"x\"><bar>test</bar></foo>"));
XmlCursor cursor = xml.newCursor();
boolean found = cursor.toFirstAttribute();
System.out.println("found attribute: " + found);
}

And it outputs:

found attribute: false

I'm assuming this is a bug as the javadoc implies this should work.


 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Cezar Andrei added a comment - 19/Apr/06 12:32 AM
The method XmlCuresor.toFirstAttribute() works from two states:
1. on START - goes to the first attribute of that element if one exists
2. on STARTDOC - goes to the first top level attribute of a document fragment - if STARTDOC is a docfrag and a top level attribute exists.

I'll add this comment to the java doc.

Dmitri Colebatch added a comment - 19/Apr/06 06:14 AM
Thanks Cezar - while you're in there, I might add another comment. I was after a method to extract the attributes from a cursor and have this:

private static Map extractAttributes(XmlCursor cursor)
{
Bookmark bookmark = new Bookmark();
cursor.setBookmark(bookmark);
if (cursor.isStartdoc())
cursor.toNextToken();

Map attributes = new HashMap();
if (cursor.toFirstAttribute())
{
do
attributes.put(cursor.getName(), cursor.getTextValue());
while (cursor.toNextAttribute());
}

cursor.toBookmark(bookmark);
return attributes;
}

The part I found curious was that I had to call toFirstAttribute before calling toNextAttribute, which I can understand, but it doesn't really provide for what I would imagine would be the typical usage pattern. It might be worth adding a comment on this in the javadoc as well.