0

I have the following code to parse an xml script below

class Cell {
    string data;
}

Integer rowCounter = 0;
List<Cell> cells = new List<Cell>();
Map<Integer, List<Cell>> timeEntryMap = new Map<Integer, List<Cell>>();

//See the xmloutput after this apex codes.
XmlStreamReader xsr = new XmlStreamReader(xmlOutput);
while(xsr.hasNext())
{
    if(xsr.getEventType() == XmlTag.START_ELEMENT) {
        if(xsr.getLocalName() == 'Row') {
            rowCounter++;
            timeEntryMap.put(rowCounter, cells);
            cells = new List<Cell>();
        }

        if (xsr.getLocalName() == 'Cell') {
            Cell cell = getCellData(xsr);
            cells.add(cell);
        }
    }
    xsr.next();
}

Cell getCellData(XmlStreamReader reader) {
    string dataContainer = '';
    Cell cell = new Cell();

    while(reader.hasNext()){
        if (reader.getEventType() == XmlTag.END_ELEMENT)
            break;
        else if (reader.getEventType() == XmlTag.CHARACTERS){
            if (!String.isBlank(reader.getText())){
                dataContainer += reader.getText();
            }
        }
        reader.next();
    }

    cell.data = dataContainer;

    return cell;
}

for (Integer key : timeEntryMap.keySet()){
    system.debug('===============ROW Start====================');
    for (Integer i = 0;i < timeEntryMap.get(key).size();i++){
        system.debug(timeEntryMap.get(rowKey)[i].data);
    }
    system.debug('===============ROW End====================');
}

Here is the xmlOutput that I am trying to parse..

<Table>
  <Row>
    <Cell ss:StyleID="Default">
      <Data ss:Type="String">19c46fb7-5314-4f8d-b6b5...</Data>
    </Cell>
    <Cell ss:StyleID="Date">
      <Data ss:Type="DateTime">2016-06-07T00:00:00</Data>
    </Cell>
    <Cell ss:StyleID="Date">
      <Data ss:Type="DateTime">2016-06-05T00:00:00</Data>
    </Cell>
    <Cell ss:StyleID="Date">
      <Data ss:Type="DateTime">2016-06-11T00:00:00</Data>
    </Cell>
    <Cell ss:StyleID="Default">
      <Data ss:Type="String">urn:tenant:user...</Data>
    </Cell>
    <Cell ss:StyleID="Number">
      <Data ss:Type="Number">2.00</Data>
    </Cell>
    <Cell ss:StyleID="Number">
      <Data ss:Type="Number">0.00</Data>
    </Cell>
  </Row>
  <Row>
    <Cell ss:StyleID="Default">
      <Data ss:Type="String">19c46fb7-5314-4f8d-b6b5...</Data>
    </Cell>
    <Cell ss:StyleID="Date">
      <Data ss:Type="DateTime">2016-06-10T00:00:00</Data>
    </Cell>
    <Cell ss:StyleID="Date">
      <Data ss:Type="DateTime">2016-06-05T00:00:00</Data>
    </Cell>
    <Cell ss:StyleID="Date">
      <Data ss:Type="DateTime">2016-06-11T00:00:00</Data>
    </Cell>
    <Cell ss:StyleID="Default">
      <Data ss:Type="String">urn:tenant:user...</Data>
    </Cell>
    <Cell ss:StyleID="Number">
      <Data ss:Type="Number">2.00</Data>
    </Cell>
    <Cell ss:StyleID="Number">
      <Data ss:Type="Number">0.00</Data>
    </Cell>
  </Row>
</Table>

This maybe somehow similar to this issue.

Was this really an issue with XmlStreamReader so the only way to get this thing to work is to use XML Dom Reader? I am taking the XML Dom reader as my last resort if this could not really get to work with XmlStreamReader

1

Not sure why your code isn't working. Most of the time the code is a lot easier to write (and therefore easier to maintain in the future) using the Dom classes as they work at a more abstract level.

For your XML the basic code would look something like this:

Dom.Document doc = new Dom.Document();
doc.load(xml);

Dom.XmlNode table = doc.getRootElement();
for (Dom.XmlNode row : table.getChildElements()) {
   if (row.getName() == 'Row') {
       for (Dom.XmlNode cell : row.getChildElements()) {
           if (cell.getName() == 'Cell') {
               for (Dom.XmlNode data : cell.getChildElements()) {
                   if (data.getName() == 'Data') {
                       String text = data.getText();
                       ...
                   }
               }
           }
       }
   }
}
1
  • This does not really answer my question but I agree with you on "code is a lot easier to write (and therefore easier to maintain in the future) using the Dom classes as they work at a more abstract level." I updated the logic on how do I read the xml response into something like you did in the above code. Thus, it fixes my issue. Thank you!
    – JF-Mechs
    Sep 9 '16 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.