1

I am hitting a web service method. This Web service method returns values in the XML format. enter image description here

When I am trying to get this element. I am getting a single ElementName with the following code in debug mode.

    givenwsdl.periodictableSoap   proxyClass = new givenwsdl.periodictableSoap  ();
String retVal = proxyClass.GetAtoms();
String GetAtomicNumber = proxyClass.GetAtomicNumber('Nitrogen');
System.debug('retVal'+retVal);
System.debug('GetAtomicNumber'+GetAtomicNumber);
 conversionclass parser=new conversionclass(retVal);
for(integer i=0;i<(retVal.length());i++)
{
    String sName=getNodeValue(parser,'ElementName');
    System.debug('test'+sName);
}


 //To get Node value based on tag name from parser
    public String getNodeValue(conversionclass parser1,String tagName)
    {
        if(parser!=null && tagname!=null)
        {
            conversionclass.Element element=parser.getElementByTagName(tagName);
            if(element!=null)
                return element.nodeValue;
        }
        return '';
    }

    //To get Node value based on tag name from Element
    public String getNodeValue(conversionclass.Element parser1,String tagName)
    {
        if(parser!=null && tagname!=null)
        {
            conversionclass.Element element=parser.getElementByTagName(tagName);
            if(element!=null)
                return element.nodeValue;

        }
        return '';
    }    

enter image description here

Problem: How to get all atomic number in the single list, So that i can use it in Vf page to display it using <apex:data-list> or <apex:repeat>

Thanks in advance.

2

Although you do not link to it, I presume that the web service you are calling is http://www.webservicex.net/periodictable.asmx.

The string returned by the GetAtoms operation is itself XML with many ElementName elements:

<string xmlns="http://www.webserviceX.NET">
    <NewDataSet>
        <Table> <ElementName>Actinium</ElementName> </Table>
        <Table> <ElementName>Aluminium</ElementName> </Table>
        ...

(By the way this seems like a poor design choice to me as the structure being returned is not modelled in the WSDL and so has to be separately assumed/implemented.)

I suggest you use a DOM parser e.g.:

private String[] parseElementNames(String retVal) {

    String[] elements = new String[] {};

    Dom.Document doc = new Dom.Document();
    doc.load(retVal);
    Dom.XmlNode root = doc.getRootElement();
    Dom.XmlNode ds = root.getChildElement('NewDataSet', 'http://www.webserviceX.NET');

    for (Dom.XmlNode t : ds.getChildElements()) {
        Dom.XmlNode en = t.getChildElement('ElementName', 'http://www.webserviceX.NET');
        elements.add(en.getText());
    }

    return elements;
}

Once you have those names, you can loop over them and invoke the GetAtomicNumber operation on each. But as only 100 web service callouts are allowed per transaction in Apex you will not be able to do that for all of them.

Put the results in a public array array field of:

public class Item {
    public String elementName {get; set;}
    public Decimal atomicNumber {get; set;}
}

that you can then iterate over in your Visualforce.

PS

I don't know why your code is not working. Make sure that the XML coming from the service matches what I have listed above. This test passes:

@IsTest
private class MyTest {

    @IsTest
    static void test() {
        String xml = ''
                + '<string xmlns="http://www.webserviceX.NET"><NewDataSet>'
                + '<Table> <ElementName>Actinium</ElementName> </Table>'
                + '<Table> <ElementName>Aluminium</ElementName> </Table>'
                + '</NewDataSet></string>'
                ;
        System.assertEquals(new String[] {'Actinium', 'Aluminium'}, parseElementNames(xml));
    }

    private static String[] parseElementNames(String retVal) {

        String[] elements = new String[] {};

        Dom.Document doc = new Dom.Document();
        doc.load(retVal);
        Dom.XmlNode root = doc.getRootElement();
        Dom.XmlNode ds = root.getChildElement('NewDataSet', 'http://www.webserviceX.NET');

        for (Dom.XmlNode t : ds.getChildElements()) {
            Dom.XmlNode en = t.getChildElement('ElementName', 'http://www.webserviceX.NET');
            elements.add(en.getText());
        }

        return elements;
    }
}

This (from your comment) is definitely wrong:

Dom.XmlNode ds = root.getChildElement('NewDataSet', 'null');

If there is no namespace the second argument should be the literal null not the string 'null'.

4
  • Thanks for your response. your presume is correct.I am using same WSDL. The Code which given is showing System.NullPointerException: Attempt to de-reference a null object at this line.` for (Dom.XmlNode t : ds.getChildElements())`.Please help me.
    – Sathish
    Jun 27 '16 at 15:31
  • @Sathish You'll have to debug; looks like ds is null for some reason.
    – Keith C
    Jun 27 '16 at 15:46
  • Its returning null for ds .I am not able to get child element of NewDataSet from the root element(root). Please help me solve this issue. Dom.XmlNode root = doc.getRootElement(); //system.debug('hello'+root); Dom.XmlNode ds = root.getChildElement('NewDataSet', 'null'); system.debug('hello'+ds);
    – Sathish
    Jun 28 '16 at 15:15
  • @Sathish See the PS in the answer.
    – Keith C
    Jun 28 '16 at 18:06

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.