Skip to content

Commit

Permalink
Now parsing all XML attributes (not only the first one)
Browse files Browse the repository at this point in the history
  • Loading branch information
nurkiewicz committed Jun 8, 2011
1 parent e7d0207 commit 9b51833
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ private final int _next() throws XMLStreamException
// fall through
case XML_START_ELEMENT: // attributes to return?
if (_nextAttributeIndex < _attributeCount) {
_localName = _xmlReader.getAttributeLocalName(0);
_namespaceURI = _xmlReader.getAttributeNamespace(0);
_textValue = _xmlReader.getAttributeValue(0);
_localName = _xmlReader.getAttributeLocalName(_nextAttributeIndex);
_namespaceURI = _xmlReader.getAttributeNamespace(_nextAttributeIndex);
_textValue = _xmlReader.getAttributeValue(_nextAttributeIndex);
return (_currentState = XML_ATTRIBUTE_NAME);
}
// otherwise need to find START/END_ELEMENT or text
Expand Down
25 changes: 24 additions & 1 deletion src/test/java/com/fasterxml/jackson/xml/TestXmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,36 @@ public void testForceElementAsArray() throws Exception
assertTrue(xp.getParsingContext().inRoot());
xp.close();
}

/*
/**********************************************************
/* Helper methods
/**********************************************************
*/


public void testXmlAttributes() throws Exception
{
final String XML = "<data max=\"7\" offset=\"9\"/>";

FromXmlParser xp = (FromXmlParser) _xmlFactory.createJsonParser(new StringReader(XML));

// First: verify handling without forcing array handling:
assertToken(JsonToken.START_OBJECT, xp.nextToken()); // <data>
assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // <max>
assertEquals("max", xp.getCurrentName());
assertToken(JsonToken.VALUE_STRING, xp.nextToken());
assertEquals("7", xp.getText());

assertToken(JsonToken.FIELD_NAME, xp.nextToken()); // <offset>
assertEquals("offset", xp.getCurrentName());
assertToken(JsonToken.VALUE_STRING, xp.nextToken());
assertEquals("9", xp.getText());

assertToken(JsonToken.END_OBJECT, xp.nextToken()); // </data>
xp.close();
}

private String _readXmlWriteJson(String xml) throws IOException
{
StringWriter w = new StringWriter();
Expand Down

3 comments on commit 9b51833

@cowtowncoder
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fix looks good, let me try to merge it in.

@cowtowncoder
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a pull request for this fix? I think that's the simplest way for me to merge it in.

@nurkiewicz
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request is already created: https://github.com/FasterXML/jackson-xml-databind/pull/13

Please sign in to comment.