1

I'm trying to parse the below XML file:

<articles>
    <article>
        <source>EN-US</source>
        <target>SL</target>
        <id>ka1800000000eCYAAY</id>
        <publishstatus>Draft</publishstatus>
        <fields>
            <field name="BU_Type_SMB__c" xml:space="preserve" translate="false">false</field>
            <field name="BU_Type__c" xml:space="preserve" translate="false">RBU</field>
            <field name="BU_Type_text__c" xml:space="preserve" translate="false">HCP</field>
            <field name="Auto_Assignment_Rule_Enabled__c" xml:space="preserve"translate="false">false</field>
        </fields>
    </article>
</articles>

Code:

public void XMLClass() {        
    string xml = '<articles><article><source>EN-US</source><target>SL</target><id>ka1800000000eCYAAY</id><publishstatus>Draft</publishstatus><fields><field name="BU_Type_SMB__c" translate="false">false</field><field name="BU_Type__c" translate="false">RBU</field><field name="BU_Type_text__c" translate="false">HCP</field><field name="Auto_Assignment_Rule_Enabled__c" translate="false">false</field></fields></article></articles>';
    Dom.Document doc = new Dom.Document();
    doc.load(xml);
    //Retrieve the root element for this document.
    Dom.XMLNode soapWeather = doc.getRootElement();
    system.debug('------soapWeather------'+soapWeather);
    Dom.XMLNode OrderDetailId = soapWeather.getChildElement('article', null);
    string source = OrderDetailId.getChildElement('source', null).getText();
    system.debug('------source--------'+source);
    String articleSource = OrderDetailId.getChildElement('target', null).getText();
    String articleId = OrderDetailId.getChildElement('id', null).getText();
    String articlePublishstatus = OrderDetailId.getChildElement('publishstatus', null).getText();
    Dom.XMLNode fieldsList = OrderDetailId.getChildElement('fields', null).getChildElement('field',null);
    system.debug('------fieldsList--------'+fieldsList);
    Integer fieldCount = fieldsList.getAttributeCount();
    system.debug('------fieldCount-----'+fieldCount);
    String fieldName = fieldsList.getAttributeValue('name','');
    String fieldTranslate = fieldsList.getAttributeValue('translate','');
    string fieldvalue = fieldsList.getChildElement('field', null).getText();

    /*   list<Dom.XMLNode> fieldsNodeLst;
        for(Dom.XMLNode fields : fieldsList.getChildElements()){
            String fieldName = fields.getAttributeValue('name','');
            system.debug('------fieldName--------'+fieldName);
            String fieldTranslate = fields.getAttributeValue('translate','');
            fieldsNodeLst.add(new list<Dom.XMLNode> (fieldName,fieldTranslate));
        } */              
}

I'm getting null pointer exception in the below line-

string fieldvalue = fieldsList.getChildElement('field', null).getText();

How can I fix this error?

  • 1
    you code is not complying at all Duplicate variable: fieldName (attempt to re-create the variable with type: String) (Line: 25, Column: 20) – Oleksandr Berehovskyi Jun 6 '17 at 11:45
  • I commented those lines now. Actually I want to get the attributes as well as value inside the field tag which I can't able to fetch. How to do that? – ani Jun 6 '17 at 11:52
  • //Iterate Over Columns Node **** for(Dom.XMLNode child : yourList){ for (dom.XmlNode subChild : child.getchildren() ) { subChild.getAttributeValue(NODE_NAME,''); } } – Nachiket Deshpande Jun 6 '17 at 12:11
1

I have created class to store field values:

public class Field {
    public String text {get; set;}
    public String name {get; set;}
    public String translate {get; set;}
}

result of parsed fields would be stored in fields variable:

public void XMLClass(){
    String xml = '<articles><article><source>EN-US</source><target>SL</target><id>ka1800000000eCYAAY</id><publishstatus>Draft</publishstatus><fields><field name="BU_Type_SMB__c" translate="false">false</field><field name="BU_Type__c" translate="false">RBU</field><field name="BU_Type_text__c" translate="false">HCP</field><field name="Auto_Assignment_Rule_Enabled__c" translate="false">false</field></fields></article></articles>';
    Dom.Document doc = new Dom.Document();
    doc.load(xml);
    Dom.XMLNode soapWeather = doc.getRootElement();
    Dom.XMLNode orderDetailId = soapWeather.getChildElement('article', null);
    String source = OrderDetailId.getChildElement('source', null).getText();
    String articleSource = OrderDetailId.getChildElement('target', null).getText();
    String articleId = OrderDetailId.getChildElement('id', null).getText();
    String articlePublishstatus = OrderDetailId.getChildElement('publishstatus', null).getText();
    List<Dom.XMLNode> fieldsList = OrderDetailId.getChildElement('fields', null).getChildElements();
    List<Field> fields = parseFields(fieldsList);
    System.debug('fields:'+fields);
}

private List<Field> parseFields(List<Dom.XMLNode> fieldsList){
    List<Field> result = new List<Field>();
    for(Dom.XMLNode field :fieldsList){
        Field parsedField = new Field();
        parsedField.text = field.getText();
        parsedField.name = field.getAttribute('name', null);
        parsedField.translate = field.getAttribute('translate', null);
        result.add(parsedField);
    }
    return result;
}

Debug result:

DEBUG|fields:(Field:[name=BU_Type_SMB__c, text=false, translate=false], Field:[name=BU_Type__c, text=RBU, translate=false], Field:[name=BU_Type_text__c, text=HCP, translate=false], Field:[name=Auto_Assignment_Rule_Enabled__c, text=false, translate=false])

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.