2 added 45 characters in body
source | link

Here is one way to do it (that may contain typos and errors as I haven't tested it):

public class Parser {

    public class MonthlyReport {
        public String month {get; set;}
        public String year {get; set;}
        public Decimal weeklyRate {get; set;}
    }

    public List<MonthlyReport> parse(string toParse) {
        List<MonthlyReport> reports = new List<MonthlyReport>();
        DOM.Document doc = new DOM.Document();
        doc.load(toParse);    
        parseReports(doc.getRootElement(), reports);
        return reports;
    }

    private void parseReports(DOM.XMLNode node, List<MonthlyReport> reports) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'monthlyReport') {
                MonthlyReport r = new MonthlyReport();
                parseReport(child, r);
                reports.add(r);
            }
        }
    }

    private void parseReport(DOM.XMLNode node, MonthlyReport r) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'month') {
                r.month = child.getText().trim();
            } else if (child.getName() == 'year') {
                r.year = child.getText().trim();
            } else if (child.getName() == 'weeklyRate') {
                r.weeklyRate = Decimal.valueOf(child.getText().trim());
            }
        }
    }
}

Using a separate method for each node type keeps each method fairly simple by it dealing with one level of the XML only.

Here is one way to do it (that may contain typos and errors as I haven't tested it):

public class Parser {

    public class MonthlyReport {
        public String month {get; set;}
        public String year {get; set;}
        public Decimal weeklyRate {get; set;}
    }

    public List<MonthlyReport> parse(string toParse) {
        List<MonthlyReport> reports = new List<MonthlyReport>();
        DOM.Document doc = new DOM.Document();
        doc.load(toParse);    
        parseReports(doc.getRootElement(), reports);
        return reports;
    }

    private void parseReports(DOM.XMLNode node, List<MonthlyReport> reports) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'monthlyReport') {
                MonthlyReport r = new MonthlyReport();
                parseReport(child, r);
                reports.add(r);
            }
        }
    }

    private void parseReport(DOM.XMLNode node, MonthlyReport r) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'month') {
                r.month = child.getText().trim();
            } else if (child.getName() == 'year') {
                r.year = child.getText().trim();
            } else if (child.getName() == 'weeklyRate') {
                r.weeklyRate = Decimal.valueOf(child.getText().trim());
            }
        }
    }
}

Using a separate method for each node type keeps each method fairly simple.

Here is one way to do it (that may contain typos and errors as I haven't tested it):

public class Parser {

    public class MonthlyReport {
        public String month {get; set;}
        public String year {get; set;}
        public Decimal weeklyRate {get; set;}
    }

    public List<MonthlyReport> parse(string toParse) {
        List<MonthlyReport> reports = new List<MonthlyReport>();
        DOM.Document doc = new DOM.Document();
        doc.load(toParse);    
        parseReports(doc.getRootElement(), reports);
        return reports;
    }

    private void parseReports(DOM.XMLNode node, List<MonthlyReport> reports) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'monthlyReport') {
                MonthlyReport r = new MonthlyReport();
                parseReport(child, r);
                reports.add(r);
            }
        }
    }

    private void parseReport(DOM.XMLNode node, MonthlyReport r) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'month') {
                r.month = child.getText().trim();
            } else if (child.getName() == 'year') {
                r.year = child.getText().trim();
            } else if (child.getName() == 'weeklyRate') {
                r.weeklyRate = Decimal.valueOf(child.getText().trim());
            }
        }
    }
}

Using a separate method for each node type keeps each method fairly simple by it dealing with one level of the XML only.

1
source | link

Here is one way to do it (that may contain typos and errors as I haven't tested it):

public class Parser {

    public class MonthlyReport {
        public String month {get; set;}
        public String year {get; set;}
        public Decimal weeklyRate {get; set;}
    }

    public List<MonthlyReport> parse(string toParse) {
        List<MonthlyReport> reports = new List<MonthlyReport>();
        DOM.Document doc = new DOM.Document();
        doc.load(toParse);    
        parseReports(doc.getRootElement(), reports);
        return reports;
    }

    private void parseReports(DOM.XMLNode node, List<MonthlyReport> reports) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'monthlyReport') {
                MonthlyReport r = new MonthlyReport();
                parseReport(child, r);
                reports.add(r);
            }
        }
    }

    private void parseReport(DOM.XMLNode node, MonthlyReport r) {
        for (Dom.XMLNode child : node.getChildElements()) {
            if (child.getName() == 'month') {
                r.month = child.getText().trim();
            } else if (child.getName() == 'year') {
                r.year = child.getText().trim();
            } else if (child.getName() == 'weeklyRate') {
                r.weeklyRate = Decimal.valueOf(child.getText().trim());
            }
        }
    }
}

Using a separate method for each node type keeps each method fairly simple.