0

How to get value session-id from this xml.

<?xml version="1.0"?>
<xmldata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" status="SUCCESS">
<recordset name="login" check="1">
        <record name="login" id="12">
            <field name="session-identifier">77777777</field>
        </record>
</recordset>
</xmldata>

I use this but get only session-identifier and not 77777777

String XMLString = '<?xml version="1.0"?><xmldata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" status="SUCCESS"><recordset name="login" check="1"><record name="login" id="12"><field name="session-identifier">77777777</field></record></recordset></xmldata>';

DOM.Document document = new DOM.Document();
document.load(XMLString);
String taskId = document.getRootElement()
        .getChildElement('recordset', null)
        .getChildElement('record', null)
        .getChildElement('field', null)
        .getAttributeValue('name', null);
system.debug(taskId);
0

in field child element, the value is text not an attribute of XML child. Thus your code should look for text rather than attribute.

String XMLString = '<?xml version="1.0"?><xmldata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" status="SUCCESS"><recordset name="login" check="1"><record name="login" id="12"><field name="session-identifier">77777777</field></record></recordset></xmldata>';

DOM.Document document = new DOM.Document();
document.load(XMLString);
String taskId = document.getRootElement()
        .getChildElement('recordset', null)
        .getChildElement('record', null)
        .getChildElement('field', null).getText();
system.debug(taskId);
3
  • Thx it works - do we need specify session-identifier because in this section i have user-identifier <record name="login" id="12"> <field name="session-identifier">77777777</field> <field name="user-identifier">8888888</field> </record> Jul 23 at 10:07
  • tiny question - i have one more <field name="user-identifier">8888888</field> block Jul 23 at 10:12
  • how to specify and get only from session-identifier Jul 23 at 10:14

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.