soapxpathgoogle-ads-apimsxml6adwords-budgetservice

How to get budgetId out of an Adwords response XML?


Context: Windows 7, JScript, Adwords v201309

Given the following (doctored) XML, how do I traverse to the budgetId using XPath?

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
        <ResponseHeader xmlns="https://adwords.google.com/api/adwords/cm/v201309">
            <requestId>feeddcaa866554433222211000000000</requestId>
            <serviceName>CampaignService</serviceName>
            <methodName>get</methodName>
            <operations>1</operations>
            <responseTime>29</responseTime>
        </ResponseHeader>
    </soap:Header>
    <soap:Body>
        <getResponse xmlns="https://adwords.google.com/api/adwords/cm/v201309">
            <rval>
                <totalNumEntries>1</totalNumEntries>
                <Page.Type>CampaignPage</Page.Type>
                <entries>
                    <id>134557899</id>
                    <name>Test Search</name>
                    <budget>
                        <budgetId>123346677</budgetId>
                        <name>Test Search</name>
                        <period>DAILY</period>
                        <amount>
                            <ComparableValue.Type>Money</ComparableValue.Type>
                            <microAmount>1000000</microAmount>
                        </amount>
                    </budget>
                </entries>
            </rval>
        </getResponse>
    </soap:Body>
</soap:Envelope>

So far I've got the following script

var oXML = new ActiveXObject('Msxml2.DOMDocument.6.0');
oXML.async = false;
oXML.validateOnParse = false;
oXML.setProperty("SelectionLanguage", "XPath");
var ns = 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"';
oXML.setProperty( "SelectionNamespaces", ns );

oXML.load("response.xml");
var x = oXML.selectSingleNode("/soap:Envelope/soap:Body");
//var x = oXML.selectSingleNode("//soap:Envelope/soap:Body/getResponse/rval/entries/budget/budgetId");
WScript.Echo(x.nodeName, x.nodeValue);

This works fine as far as traversing to /soap:Envelope/soap:Body but if I try to go further, like /soap:Envelope/soap:Body/getResponse, (or even /soap:Envelope/soap:Body/getResponse/rval/entries/budget/budgetId as suggested below) I get null.


Solution

  • You should traverse through <soap:Body> instead of <soap:Header> to get <budgetId> :

    /soap:Envelope/soap:Body/getResponse/.......
    

    This is the exact path to <budgetId> given sample XML in this question :

    /soap:Envelope/soap:Body/getResponse/rval/entries/budget/budgetId
    

    UPDATE :

    Your xml has default namespace (xmlns="....") besides soap namespace. In XML all elements without prefix considered in default namespace. And in XPath, all element without prefix considered has no namespace. So you'll need to define another prefix pointing to default namespace url (not sure if this is the correct syntax) :

    var ns = 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:d="https://adwords.google.com/api/adwords/cm/v201309"';
    

    and use it in XPath statement :

    /soap:Envelope/soap:Body/d:getResponse/d:rval/d:entries/d:budget/d:budgetId