Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Is there any way to insert attribute in xml tag? #22

Open
Cr1stal opened this issue Nov 12, 2015 · 6 comments
Open

Is there any way to insert attribute in xml tag? #22

Cr1stal opened this issue Nov 12, 2015 · 6 comments

Comments

@Cr1stal
Copy link

Cr1stal commented Nov 12, 2015

Hello. I need to use this method.
http://developer.ebay.com/devzone/finding/callref/findItemsByProduct.html

<productId type="string"> ProductId (string) </productId>

I don't know how to insert attribute "type" in xml tag. Please, help.

@BDiehr
Copy link

BDiehr commented Dec 3, 2015

If you follow the code to where they actually convert the JSON into XML you'll see that it uses xml (https://github.com/dylang/node-xml).

xml({a: { _attr: { attr:'hi'}, _cdata: "I'm not escaped" }}) === '<a attr="hi"><![CDATA[I\'m not escaped]]></a>'

@urielcastrillon08
Copy link

urielcastrillon08 commented Feb 24, 2017

hello,
i have the same problem

when i pass this Object:

xml({Item: [{BuyItNowPrice: [{ _attr: { currencyID: 'USD'}}, 18]}]}) === "<BuyItNowPrice currencyID="USD">18"

But when I pass as a parameter of this function

ebay.xmlRequest({
serviceName: 'Trading',
opType: 'AddItems',

            // app/environment
            devId: this.devId,
            certId: this.certId,
            appId: this.appId,
            sandbox: true,

            // per user
            authToken: this.authToken,
            params: {Item: [{BuyItNowPrice: [{ _attr: { currencyID: 'USD'}}, 18]}]}
        }, (error, results) => {

          ...
        });

the function deepToArray convert
this object : {Item: [{BuyItNowPrice: [{ _attr: { currencyID: 'USD'}}, 18]}]} in

‌[{"Item":[{"BuyItNowPrice":[{"_attr":[{"currencyID":["USD"]}]}]},{"BuyItNowPrice":[18]}]}]

for which:

xml([{"AddItemsRequest":[{"_attr":{"xmlns":"urn:ebay:apis:eBLBaseComponents"}},{"RequesterCredentials":[{"eBayAuthToken":"Token"}]},{"Item":[{"BuyItNowPrice":[{"_attr":[{"currencyID":["USD"]}]}]},{"BuyItNowPrice":[18]}]}]}
])

image

I really appreciate. It if we could solve the problem

@thehuey
Copy link

thehuey commented Feb 25, 2017 via email

@urielcastrillon08
Copy link

thank you for your answer thehuey.
do you have an example of serviceName: 'Trading', opType: 'AddItems' with BuyItNowPrice?

@urielcastrillon08
Copy link

is possible change the function deepToArray for this:

exports.deepToArray = function deepToArray(obj) {
var key, value, arr = [];

if (.isArray(obj)) {
// not sure about this: arrays within objects are handled below;
// top level should never be an array;
// but seems ok. change/fix if a scenario comes up that this breaks.
return obj.map(function(value) {
return deepToArray(value);
});
}
else if (
.isObject(obj)) {
for (key in obj) {
if (obj.hasOwnProperty(key)) {
value = obj[key];

    // `{foo: [a,b,c]}` => `[{foo:a},{foo:b},{foo:c}]`
    if (_.isArray(value)) {

        if (value[0].hasOwnProperty('_attr')) {
            arr.push(_.set({}, key, value));
        }
        else {
            value.forEach(function (subValue) {
                arr.push(_.set({}, key, deepToArray(subValue)));
            });
        }
    }
    else {
          arr.push(_.set({}, key, deepToArray(value)));
    }
  }
}

}
else {
arr = [obj];
}
return arr;
};

@alexdrans
Copy link

alexdrans commented Mar 28, 2017

@thehuey Are you sure you're meant to manually convert to XML before passing the params? Doing so produces the correct (escaped) XML, however, when this is passed to here, the manually converted xml param is escaped when converted again (and therefore 500s eBay's API).

i.e. it's sending;


<?xml version="1.0" encoding="UTF-8"?>
<findItemsByProductRequest xmlns="http://www.ebay.com/marketplace/search/v1/services">&amp;lt;productId type=&amp;quot;EAN&amp;quot;&amp;gt;5055297800890&amp;lt;/productId&amp;gt;</findItemsByProductRequest>

If I don't run the params through xml first, i.e. params = { .. }, then it produces the same output as @urielcastrillon08 above.

Any ideas..?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants