0

I need to parse this xml and get last-name by Id(99999) or by Id(55555) based on what I set in method.

<?xml version="1.0"?>
<xmldata
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" status="SUCCESS">
    <recordset name="object" nritems="1" acl="AW">
        <record name="object" id="99999" acl="DW">
            <field name="last-name" acl="TW">**SS**</field>
        </record>
    </recordset>
</xmldata>

I am trying this for Id

DOM.Document document = new DOM.Document();
document.load(responseBody);
String subjectID = document.getRootElement()
            .getChildElement('recordset', null)
            .getChildElement('recordname', null).getText();
0
-2

Use this.

DOM.Document document = new DOM.Document();
document.load(responseBody);
String subjectID = document.getRootElement()
        .getChildElement('recordset', null)
        .getChildElement('record', null).getAttribute('id', '');
String name;
for (Dom.XmlNode xmlNode : document.getRootElement()
        .getChildElement('recordset', null)
        .getChildElement('record', null).getChildElements()) {
    if (xmlNode.getAttribute('name', '') == 'name') {
        name = xmlNode.getText();
    }
}
System.debug(
        subjectID
);
System.debug(name);
2
  • 2
    Please edit your answer to eplain what the difference is and why this code works (but the code OP is using didn't)
    – Derek F
    Aug 4 at 16:44
  • Yes it works perfect! Aug 5 at 15:46

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.