3

I am quite new in Xml parsing. i am having a XMl

<company>
<account>
    <id>8c98a6be</id>
    <label>4356</label>
    <reference>4356</reference>
</account>
  <date>2012-06-05</date>
  <advertisers>
  <advertiser><id>7</id><name>ABC Telecom</name></advertiser>
  <advertiser><id>106</id><name>ABC_Ozone</name></advertiser>
  <advertiser><id>13</id><name>Acme Corp</name></advertiser>
 </advertisers>
</company>

which i am parsing thru this code.

public class XMLParser {
 public static void parseXML(String xmlValue) {
   Dom.Document doc = new Dom.Document();
   try {
    doc.load(xmlValue);
    DOM.XMLNode root = doc.getRootElement();
    loadChilds(root);
  } catch (System.XMLException e) {
      System.debug( e.getMessage());
  }
 }
 public static void loadChilds(DOM.XMLNode node) {
   if (node.getNodeType() == DOM.XMLNodeType.ELEMENT) {
       System.debug(node.getName()
        '+'
       node.getText());
    }
  for (Dom.XMLNode child: node.getChildElements()) {
     loadChilds(child);
  }
 }
}

i want to create a list of List<advertiser__c>. how do i parse it to achieve a list with all repeated advertiser with all other details i.e data , id , label,reference.

2 Answers 2

2

The Apex DOM parser is the simplest to use. The code would look something like this:

public static Advertiser__c[]  parseXML(String xmlValue) {
    Dom.Document doc = new Dom.Document();
    doc.load(xmlValue);
    Advertiser__c[] advertisers = new Advertiser__c[] {};
    for (Dom.XmlNode child : doc.getRootElement().getChildElement('advertisers', null).getChildElements()) {
        if (child().getNamespace() == null &&  child.getName() == 'advertiser') {
             String id = child.getChildElement('id', null).getText();
             String name = child.getChildElement('name', null).getText();
             advertisers.add(new Advertiser__c(Id = (Id) id, Name = name));
        }
    }
    return advertisers;
}

As your XML does not use namespaces, the namespace arguments are set to null where required.

1
  • I like this approach. +1
    – javanoob
    Commented Mar 23, 2016 at 2:34
1

Here is what I came up with following the sample code given in this link

SimpleXmlParserClass.cls:

public with sharing class SimpleXmlParserClass {

    public class Advertiser{
        String id;
        String name;
    }

    public Advertiser parseAdvertiser(XmlStreamReader reader){

        Advertiser advertiserObj = new Advertiser();
        advertiserObj.id = reader.getAttributeValue(null, 'author');

        boolean isSafeToGetNextXmlElement = true;
        while(isSafeToGetNextXmlElement) {

            if (reader.getEventType() == XmlTag.END_ELEMENT 
                && reader.getLocalName() == 'advertiser') {
                break;
            }

            if(reader.getLocalName() == 'id'){
                reader.next();
                advertiserObj.id = reader.getText();
                while(reader.isStartElement()!= true){ 
                //We need to loop through until 
                //we are pointing to start element of 
                //name tage for that advertiser.
                    reader.nextTag();
                }
            }

            if(reader.getLocalName() == 'name'){
                reader.next();
                advertiserObj.name = reader.getText();
                while(reader.isEndElement()!= true){ 
                //We need to move to the next tag 
                //until we reach end of element name.
                    reader.nextTag();
                }
            }

            // 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 advertiserObj;    
    }      

    public Advertiser[] parseAdvertisers(XmlStreamReader reader){
        Advertiser[] advertisersList = new Advertiser[0];
        boolean isSafeToGetNextXmlElement = true;
        while(isSafeToGetNextXmlElement) {
            // Start at the beginning of the advertiser
            // and make sure that it is a advertiser
            if (reader.getEventType() == XmlTag.START_ELEMENT) {
                if ('advertiser' == reader.getLocalName()) {
                    // Pass the book to the parseBook method (below) 
                    Advertiser advObj = parseAdvertiser(reader);
                    advertisersList.add(advObj);
                }
            }
            // 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 advertisersList;
    }

}

Here is the test class:

SimpleXmlParserClass demo = new SimpleXmlParserClass();

String str = '<company> <account> <id>8c98a6be</id> <label>4356</label> <reference>4356</reference> </account> <date>2012-06-05</date> <advertisers> <advertiser><id>7</id><name>ABC Telecom</name></advertiser> <advertiser><id>106</id><name>ABC_Ozone</name></advertiser> <advertiser><id>13</id><name>Acme Corp</name></advertiser> </advertisers> </company>';

XmlStreamReader reader = new XmlStreamReader(str);
SimpleXmlParserClass.Advertiser[] advertisersList = demo.parseAdvertisers(reader);

System.debug(advertisersList.size());

for (SimpleXmlParserClass.Advertiser adv : advertisersList) {
    System.debug(adv);
}

And here is the output:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .