String xmlString = '<books><book>My Book</book><book>Your Book</book></books>'; XmlStreamReader xsr = new XmlStreamReader(xmlString);
These methods work on the following XML events:
Use the next and hasNext methods to iterate over XML data. Access data in XML using get methods such as the getNamespace method.
When iterating over the XML data, always check that stream data is available using hasNext before calling next to avoid attempting to read past the end of the XML data.
The following example processes an XML string.
public class XmlStreamReaderDemo { // Create a class Book for processing public class Book { String name; String author; } public Book[] parseBooks(XmlStreamReader reader) { Book[] books = new Book[0]; boolean isSafeToGetNextXmlElement = true; while(isSafeToGetNextXmlElement) { // Start at the beginning of the book and make sure that it is a book if (reader.getEventType() == XmlTag.START_ELEMENT) { if ('Book' == reader.getLocalName()) { // Pass the book to the parseBook method (below) Book book = parseBook(reader); books.add(book); } } // Always use hasNext() before calling next() to confirm // that we have not reached the end of the stream if (reader.hasNext()) { reader.next(); } else { isSafeToGetNextXmlElement = false; break; } } return books; } // Parse through the XML, determine the author and the characters Book parseBook(XmlStreamReader reader) { Book book = new Book(); book.author = reader.getAttributeValue(null, 'author'); boolean isSafeToGetNextXmlElement = true; while(isSafeToGetNextXmlElement) { if (reader.getEventType() == XmlTag.END_ELEMENT) { break; } else if (reader.getEventType() == XmlTag.CHARACTERS) { book.name = reader.getText(); } // Always use hasNext() before calling next() to confirm // that we have not reached the end of the stream if (reader.hasNext()) { reader.next(); } else { isSafeToGetNextXmlElement = false; break; } } return book; } }
@isTest private class XmlStreamReaderDemoTest { // Test that the XML string contains specific values static testMethod void testBookParser() { XmlStreamReaderDemo demo = new XmlStreamReaderDemo(); String str = '<books><book author="Chatty">Foo bar</book>' + '<book author="Sassy">Baz</book></books>'; XmlStreamReader reader = new XmlStreamReader(str); XmlStreamReaderDemo.Book[] books = demo.parseBooks(reader); System.debug(books.size()); for (XmlStreamReaderDemo.Book book : books) { System.debug(book); } } }