function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
maybemedicmaybemedic 

XmlStreamReader "Constructor not defined"

Hello All,
              I am new to Apex development and I am going through the Apex reference document. I am trying to save a sample code in the doc, but it keeps giving me the Exception "Constructor Not defined". I am using the web interface on my DEV edition. It happens in Eclipse too. I'd appreciate if I get some help in this regard. Thanks.
Code:
public class XmlStreamReader
{
    // Create a class Book for processing
    public class Book 
    {
        String name;
        String author;
    }
    Book[] parseBooks(XmlStreamReader reader) 
    {
        Book[] books = new Book[0];
        while(reader.hasNext()) 
        {
            // Start at the beginning of the book and make sure that it is a book
            if (reader.getEventType() == XmlTag.START_ELEMENT)
            {
                if (reader.getLocalName()=='Book') 
                {
                    // Pass the book to the parseBook method (below)
                    Book book = parseBook(reader);
                    books.add(book);
                }
            }
            reader.next();
        }
    return books;
    }

// Parse through the XML, deterimine the auther and the characters
Book parseBook(XmlStreamReader reader) 
{
    Book book = new Book();
    book.author = reader.getAttributeValue('', 'author');
    while(reader.hasNext()) 
    {
        if (reader.getEventType() == XmlTag.END_ELEMENT) 
        {
            break;
        } 
        else if (reader.getEventType() == XmlTag.CHARACTERS) 
        {
            book.name = reader.getText();
        }
    reader.next();
    }
return book;
}

// Test that the XML string contains specific values
static testMethod void testBookParser() 
{
    XmlStreamReader demo = new XmlStreamReader();
    String str = '<books><book author="Manoj">Foo bar</book>' +
    '<book author="Mysti">Baz</book></books>';
    XmlStreamReader reader = new XmlStreamReader(str);
    Book[] books = demo.parseBooks(reader);
    System.debug(books.size());
    for (Book book : books) 
    {
        System.debug(book);
    }
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae
Don't name your class XMLStreamReader, that is a reserved class name.
If you change it to something like XMLRead, it should work:

Code:
public class XMLRead 
{
    // Create a class Book for processing
    public class Book 
    {
        String name;
        String author;
    }
    Book[] parseBooks(XmlStreamReader reader) 
    {
        Book[] books = new Book[0];
        while(reader.hasNext()) 
        {
            // Start at the beginning of the book and make sure that it is a book
            if (reader.getEventType() == XmlTag.START_ELEMENT)
            {
                if (reader.getLocalName()=='Book') 
                {
                    // Pass the book to the parseBook method (below)
                    Book book = parseBook(reader);
                    books.add(book);
                }
            }
            reader.next();
        }
    return books;
    }

// Parse through the XML, deterimine the auther and the characters
Book parseBook(XmlStreamReader reader) 
{
    Book book = new Book();
    book.author = reader.getAttributeValue('', 'author');
    while(reader.hasNext()) 
    {
        if (reader.getEventType() == XmlTag.END_ELEMENT) 
        {
            break;
        } 
        else if (reader.getEventType() == XmlTag.CHARACTERS) 
        {
            book.name = reader.getText();
        }
    reader.next();
    }
return book;
}

// Test that the XML string contains specific values
static testMethod void testBookParser() 
{
    XMLRead demo = new XMLRead();
    String str = '<books><book author="Manoj">Foo bar</book>' +
    '<book author="Mysti">Baz</book></books>';
    XmlStreamReader reader = new XmlStreamReader(str);
    Book[] books = demo.parseBooks(reader);
    System.debug(books.size());
    for (Book book : books) 
    {
        System.debug(book);
    }
}
}

 

All Answers

JimRaeJimRae
Don't name your class XMLStreamReader, that is a reserved class name.
If you change it to something like XMLRead, it should work:

Code:
public class XMLRead 
{
    // Create a class Book for processing
    public class Book 
    {
        String name;
        String author;
    }
    Book[] parseBooks(XmlStreamReader reader) 
    {
        Book[] books = new Book[0];
        while(reader.hasNext()) 
        {
            // Start at the beginning of the book and make sure that it is a book
            if (reader.getEventType() == XmlTag.START_ELEMENT)
            {
                if (reader.getLocalName()=='Book') 
                {
                    // Pass the book to the parseBook method (below)
                    Book book = parseBook(reader);
                    books.add(book);
                }
            }
            reader.next();
        }
    return books;
    }

// Parse through the XML, deterimine the auther and the characters
Book parseBook(XmlStreamReader reader) 
{
    Book book = new Book();
    book.author = reader.getAttributeValue('', 'author');
    while(reader.hasNext()) 
    {
        if (reader.getEventType() == XmlTag.END_ELEMENT) 
        {
            break;
        } 
        else if (reader.getEventType() == XmlTag.CHARACTERS) 
        {
            book.name = reader.getText();
        }
    reader.next();
    }
return book;
}

// Test that the XML string contains specific values
static testMethod void testBookParser() 
{
    XMLRead demo = new XMLRead();
    String str = '<books><book author="Manoj">Foo bar</book>' +
    '<book author="Mysti">Baz</book></books>';
    XmlStreamReader reader = new XmlStreamReader(str);
    Book[] books = demo.parseBooks(reader);
    System.debug(books.size());
    for (Book book : books) 
    {
        System.debug(book);
    }
}
}

 

This was selected as the best answer
maybemedicmaybemedic
Rae,
         Thanks so much. I should have known that. :smileyhappy: