[SalesForce] Using DOM to parse API Response. How to get subchild elements

I know there are some questions on this topic already. I have tried to implement them myself, but I've been stuck and need to try and get some help. I am a new graduate on my first project at a new job and I was told to try and utilize all resources and ask for help and it is Xmas so here I go.
I need to parse this API response. I want to learn how to get all the text values but what I really need is the "resultCode" and the "text" values.
This is my code that I have written. I can get the "refId" and the "merchantId", but I cant get the subchild elements (resultCode, code, text). I have been getting a error as well "attempt to dereference a null object", so I checked for null values before getting the text. Any help will be helpful and appreciated.

           <?xml version="1.0" encoding="utf-8"?>
           <resellerCreateMerchantResponse>
               <refId>Sample</refId>
               <messages>
                    <resultCode>Ok</resultCode>
                    <message>
                          <code>I00001</code>
                          <text>Successful.</text>
                    </message>
               </messages>
              <merchantId>600894</merchantId>
           </resellerCreateMerchantResponse>


           try{
                 Dom.Document doc = res.getBodyDocument();    
                 //Retrieve the root element for this document.
                 Dom.XMLNode xmlResponse = doc.getRootElement();
                 System.debug(xmlResponse.getName()); // prints out the name of the root

                // check for missing elements before trying to get the text of the element 
                Dom.XmlNode refid  = xmlResponse.getChildElement('refId', null);
                String referenceId = refid != null ? xmlResponse.getText() : null;
                System.debug('Ref Id: ' + referenceId);

                Dom.XMLNode merchid = xmlResponse.getChildElement('merchantId', null);
                String merchantId   = merchid != null ? xmlResponse.getText() : null;
                System.debug('Merchant Id: ' + merchantId);

                Dom.XmlNode msgs = xmlResponse.getChildElement('messages', null);
                // .getChildElement('resultCode', null);//.getChildElement('text', null);
                String messages = msgs != null ? xmlResponse.getText() : null;

               // get the child elements.. BUT THIS DOESN'T GET ME ALL THAT I NEED.
                for(Dom.XMLNode child : xmlResponse.getChildElements())
                {
                   System.debug(child.getText());
                }

           }catch(System.XMLException e){      
                  System.debug(e.getMessage());
            }

Best Answer

After some sleep and a new approach I have solved my problem. I was able to traverse through each node to get the value I need to proceed. It took a couple days for me to figure it out, but I learned different techniques I could use for future reference. Here is my code in case another rookie runs into the same problem.

           try{
                Dom.Document doc = res.getBodyDocument();    // retrieves the body of this request as a DOM document

                //Retrieve the root element for this document.
                Dom.XMLNode xmlResponse = doc.getRootElement();
                System.debug(xmlResponse.getName()); // prints out the name of the root

                Boolean status = false; // true means API response was processed with no errors

                for(Dom.XMLNode child : xmlResponse.getChildElements())
                {
                    //System.debug(child.getName());
                    //System.debug(child.getText());

                   for(Dom.XMLNode childField : child.getChildren() )
                   {
                        //System.debug(childField.getText());
                        //System.debug(child.getName());

                        for(Dom.XMLNode subchild : childField.getChildren())
                        {                               
                            if(subchild.getName() == 'code' && subchild.getText() == 'I00001')
                            {
                                status = true;
                                System.debug(subchild.getName());
                                System.debug(subchild.getText());
                                System.debug(status);                                   
                            }                  
                        }
                   }
                }
            }
            catch(System.XMLException e)
            {
                System.debug(e.getMessage());
            }
Related Topic