0

I am wondering how can I search a node once I parse an XML, I need to go to the node (attached at the end of the question) "data-block" and loop through all the children to extract the value " <vs:value ", at the moment I am doing something that looks really bad as I havent figure it out how to go straight to data-block to start my loop, or even straight to the partiicular node that i need to extract the value, for example if i want to get th evalue of NA8900_xr_cur

public static void testing () {
    Attachment att = new Attachment();
    att = [SELECT Id, Body FROM Attachment WHERE Id='00P8s0000001czJ'];
    String s = att.Body.toString();

    DOM.Document doc = new DOM.Document();
    DOM.XmlNode rootNode;
    DOM.XmlNode body;        
    DOM.XmlNode response;
    DOM.XmlNode product_data;
    DOM.XmlNode summary_data;
    DOM.XmlNode data_block;
    
    try{
        doc.load(s);
        rootNode = doc.getRootElement();
        // System.debug(rootNode);
    }catch(exception e){
        system.debug(e.getMessage());
    }
    for(Dom.XmlNode node : rootNode.getChildElements()){
        if (node.getName() == 'body') {
            body = node;
        }
    }

    if (body == null) {
        System.debug('Body is null');
        return;
    }
    for(Dom.XmlNode node : body.getChildElements()){
        if (node.getName() == 'response') {
            response = node;
        }
    }
    
    if (response == null) {
        System.debug('response is null');
        return;
    }
    for(Dom.XmlNode node : response.getChildElements()){
        System.debug(node.getName());
        if (node.getName() == 'product-data') {
            product_data = node;
        }
    }
    
    if (product_data == null) {
        System.debug('product_data is null');
        return;
    }
    for(Dom.XmlNode node : product_data.getChildElements()){
        if (node.getName() == 'summary-data') {
            summary_data = node;
        }
    }
    
    if (summary_data == null) {
        System.debug('summary_data is null');
        return;
    }
    for(Dom.XmlNode node : summary_data.getChildElements()){
        System.debug(node.getName());
        if (node.getName() == 'data-block') {
            data_block = node;
        }
    }
    
    if (data_block == null) {
        System.debug('data_block is null');
        return;
    }
    for(Dom.XmlNode node : data_block.getChildElements()){
        System.debug(node.getName());
    }
}

XML Sample:

<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header>
        <wsa:MessageID
            xmlns:wsa="http://www.w3.org/2005/08/addressing">211009-CF16D-994B7
        </wsa:MessageID>
        <wsa:RelatesTo
            xmlns:wsa="http://www.w3.org/2005/08/addressing"/>
            <wsa:To
                xmlns:wsa="http://www.w3.org/2005/08/addressing">http://www.w3.org/2005/08/addressing/anonymous
            </wsa:To>
            <wsa:From
                xmlns:wsa="http://www.w3.org/2005/08/addressing">
                <wsa:Address>https://cteau.vedaxml.com/sys2/soap11/vedascore-apply-v2-0</wsa:Address>
            </wsa:From>
            <wsa:Action
                xmlns:wsa="http://www.w3.org/2005/08/addressing">http://vedaxml.com/vedascore-apply/EnquiryResponse
            </wsa:Action>
        </soapenv:Header>
        <soapenv:Body>
            <vs:response
                xmlns:vs="http://vedaxml.com/vxml2/vedascore-apply-v2-0.xsd" response-type="report">
                <vs:product-header>
                    <vs:enquiry-id>xxxxxxxxx</vs:enquiry-id>
                    <vs:datetime-requested>2021-10-09T16:10:28.499+11:00</vs:datetime-requested>
                    <vs:datetime-generated>2021-10-09T16:10:31.872+11:00</vs:datetime-generated>
                    <vs:client-reference>xxxxxx</vs:client-reference>
                    <vs:operator-id>xxxxx</vs:operator-id>
                    <vs:operator-name>xxxxx xxxxxx</vs:operator-name>
                    <vs:product-name>VedaScore Apply®</vs:product-name>
                    <vs:permission-type code="X">CONSUMER</vs:permission-type>
                    <vs:product-data-level code="N">Negative</vs:product-data-level>
                    <vs:product-version>2.0</vs:product-version>
                </vs:product-header>
                <vs:product-data>
                    <vs:enquiry-report>
                        <vs:primary-match id-index="1" type="create">
                            <vs:bureau-reference>30YYXXXX12</vs:bureau-reference>
                            <vs:individual-name first-reported-date="2021-10-09">
                                <vs:family-name>YYYYY</vs:family-name>
                                <vs:first-given-name>ZZZZZZ</vs:first-given-name>
                            </vs:individual-name>
                            <vs:gender code="U">Unknown</vs:gender>
                            <vs:date-of-birth first-reported-date="2021-10-09" last-reported-date="2021-10-09">1980-12-01</vs:date-of-birth>
                            <vs:is-deceased>false</vs:is-deceased>
                        </vs:primary-match>
                        <vs:addresses>
                            <vs:address first-reported-date="2021-10-09" last-reported-date="2021-10-09" type="C">
                                <vs:id-references>
                                    <vs:id-reference>1</vs:id-reference>
                                </vs:id-references>
                                <vs:street-number>937</vs:street-number>
                                <vs:street-name>KENT</vs:street-name>
                                <vs:street-type>ST</vs:street-type>
                                <vs:suburb>SYDNEY</vs:suburb>
                                <vs:state>NSW</vs:state>
                                <vs:postcode>2000</vs:postcode>
                                <vs:country-code>AUS</vs:country-code>
                            </vs:address>
                        </vs:addresses>
                        <vs:consumer-credit-file>
                            <vs:credit-enquiries>
                                <vs:credit-enquiry enquiry-date="2021-10-09" type="credit-application">
                                    <vs:id-references>
                                        <vs:id-reference>1</vs:id-reference>
                                    </vs:id-references>
                                    <vs:account-type code="PF">Personal Loan (fixed term)</vs:account-type>
                                    <vs:enquiry-amount currency-code="AUD">650</vs:enquiry-amount>
                                    <vs:relationship code="1">Principal's Account (sole)</vs:relationship>
                                    <vs:credit-enquirer>
                                        <vs:name>XXXXXXXX.</vs:name>
                                        <vs:classification>SLND</vs:classification>
                                    </vs:credit-enquirer>
                                    <vs:enquiry-client-reference>0068S000000AAQD</vs:enquiry-client-reference>
                                </vs:credit-enquiry>
                            </vs:credit-enquiries>
                        </vs:consumer-credit-file>
                    </vs:enquiry-report>
                    <vs:summary-data>
                        <vs:data-block name="genesis" type="standard">
                            <vs:characteristic id="NA8900" key-characteristic="true">
                                <vs:variable-name>NA8900_xr_cur</vs:variable-name>
                                <vs:description>Number of cross references</vs:description>
                                <vs:value unit="count">0</vs:value>
                                <vs:data-level code="N">Negative</vs:data-level>
                            </vs:characteristic>
                            <vs:characteristic id="NA8904" key-characteristic="false">
                                <vs:variable-name>NA8904_age_fle</vs:variable-name>
                                <vs:description>Age of primary file</vs:description>
                                <vs:value unit="months">0.0</vs:value>
                                <vs:data-level code="N">Negative</vs:data-level>
                            </vs:characteristic>
                        </vs:data-block>
                    </vs:summary-data>
                    <vs:score-data>
                        <vs:score>
                            <vs:scorecard id="VSA_2.0_X_NR">
                                <vs:name>VedaScore Apply</vs:name>
                                <vs:version>2.0</vs:version>
                                <vs:type>Consumer</vs:type>
                                <vs:data-level code="N">Negative</vs:data-level>
                            </vs:scorecard>
                            <vs:risk-odds>9.38</vs:risk-odds>
                            <vs:key-contributing-factors>
                                <vs:contributing-factor id="KCF-001">
                                    <vs:name>Length of Credit History</vs:name>
                                    <vs:description>The time since credit activity is first recorded on file can be an indicator of risk.</vs:description>
                                    <vs:score-impactor>Moderately Increases Risk</vs:score-impactor>
                                </vs:contributing-factor>
                                <vs:contributing-factor id="KCF-004">
                                    <vs:name>Current Consumer Credit Application Information</vs:name>
                                    <vs:description>The type and amount of credit being applied for can have an impact on risk.</vs:description>
                                    <vs:score-impactor>Marginally Increases Risk</vs:score-impactor>
                                </vs:contributing-factor>
                            </vs:key-contributing-factors>
                            <vs:score-masterscale>523</vs:score-masterscale>
                        </vs:score>
                    </vs:score-data>
                </vs:product-data>
            </vs:response>
        </soapenv:Body>
    </soapenv:Envelope>
2
  • 1
    I don't know what your technology constraints are but the obvious answer is "XPath". Commented Oct 9, 2021 at 7:41
  • 2
    Specifically consider apex-xpath.
    – Phil W
    Commented Oct 9, 2021 at 8:05

1 Answer 1

1

This is a SOAP call, so your obvious best bet is to get a WSDL, use the SOAP Services feature of Apex, then you can call the method like a normal method call. If your particular API call isn't supported as this way, then you'll want to use something like XPath to avoid complicated parsing. However, I would strongly advise that you try importing the WSDL first, it's so much easier, as you don't have to write any explicit parsing code yourself.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .