0

I need to parse this xml and get last-name by Id(99999) or by Id(55555) based on what I set in the 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>
        <record name="object" id="55555" acl="DW">
            <field name="last-name" acl="TW">**Jony**</field>
        </record>
    </recordset>
</xmldata>

I am trying this but get only firsr value

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);
0
for (Dom.XmlNode xmlNode : document.getRootElement()
    .getChildElement('recordset', null)
    .getChildElement('record', null).getChildElements()) {

Should be:

for (Dom.XmlNode xmlNode : document.getRootElement()
    .getChildElement('recordset', null)
    .getChildElements() { // These are 'record' elements

And from there, you can parse the fields.

As an aside, you can use my XmlToJSON logic to get a slightly-easier-to-work-with JSON structure instead.

1
  • Pls specify how to get last-name from record 55555 Aug 11 '21 at 11:51

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.