0

I am trying parse the xml but result return null.

Here is the xml:

<feed>
    <title type="text">neymar</title>
    <subtitle type="text">Bing Image Search</subtitle>
    <id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='neymar'&$top=2</id>
    <rights type="text"/>
    <updated>2013-05-13T08:45:02Z</updated>
    <link rel="next" href="https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='neymar'&$skip=2&$top=2"/>
    <entry>
        <id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='neymar'&$skip=0&$top=1</id>
        <title type="text">ImageResult</title>
        <updated>2013-05-13T08:45:02Z</updated>
        <content type="application/xml">
            <m:properties>
                <d:ID m:type="Edm.Guid">99cb00e9-c9bb-45ca-9776-1f51e30be398</d:ID>
                <d:Title m:type="Edm.String">neymaer wallpaper neymar brazil wonder kid neymar wallpaper hd</d:Title>
                <d:MediaUrl m:type="Edm.String">http://3.bp.blogspot.com/-uzJS8HW4j24/Tz3g6bNII_I/AAAAAAAAB1o/ExYxctnybUo/s1600/neymar-wallpaper-5.jpg</d:MediaUrl>
                <d:SourceUrl m:type="Edm.String">http://insidefootballworld.blogspot.com/2012/02/neymar-wallpapers.html</d:SourceUrl>
                <d:DisplayUrl m:type="Edm.String">insidefootballworld.blogspot.com/2012/02/neymar-wallpapers.html</d:DisplayUrl>
                <d:Width m:type="Edm.Int32">1280</d:Width>
                <d:Height m:type="Edm.Int32">800</d:Height>
                <d:FileSize m:type="Edm.Int64">354173</d:FileSize>
                <d:ContentType m:type="Edm.String">image/jpeg</d:ContentType>
                <d:Thumbnail m:type="Bing.Thumbnail">
                    <d:MediaUrl m:type="Edm.String">http://ts3.mm.bing.net/th?id=H.5042206689331494&pid=15.1</d:MediaUrl>
                    <d:ContentType m:type="Edm.String">image/jpg</d:ContentType>
                    <d:Width m:type="Edm.Int32">300</d:Width>
                    <d:Height m:type="Edm.Int32">187</d:Height>
                    <d:FileSize m:type="Edm.Int64">12990</d:FileSize>
                </d:Thumbnail>
            </m:properties>
        </content>
    </entry>
    <entry>
        <id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/Image?Query='neymar'&$skip=1&$top=1</id>
        <title type="text">ImageResult</title>
        <updated>2013-05-13T08:45:02Z</updated>
        <content type="application/xml">
            <m:properties>
                <d:ID m:type="Edm.Guid">9a6b7476-643e-4844-a8da-a4b640a78339</d:ID>
                <d:Title m:type="Edm.String">neymar jr 485x272 Neymar Show 2012 Hd</d:Title>
                <d:MediaUrl m:type="Edm.String">http://www.sontransferler.com/wp-content/uploads/2012/07/neymar_jr.jpg</d:MediaUrl>
                <d:SourceUrl m:type="Edm.String">http://www.sontransferler.com/neymar-show-2012-hd</d:SourceUrl>
                <d:DisplayUrl m:type="Edm.String">www.sontransferler.com/neymar-show-2012-hd</d:DisplayUrl>
                <d:Width m:type="Edm.Int32">1366</d:Width>
                <d:Height m:type="Edm.Int32">768</d:Height>
                <d:FileSize m:type="Edm.Int64">59707</d:FileSize>
                <d:ContentType m:type="Edm.String">image/jpeg</d:ContentType>
                <d:Thumbnail m:type="Bing.Thumbnail">
                    <d:MediaUrl m:type="Edm.String">http://ts1.mm.bing.net/th?id=H.4796985557255960&pid=15.1</d:MediaUrl>
                    <d:ContentType m:type="Edm.String">image/jpg</d:ContentType>
                    <d:Width m:type="Edm.Int32">300</d:Width>
                    <d:Height m:type="Edm.Int32">168</d:Height>
                    <d:FileSize m:type="Edm.Int64">4718</d:FileSize>
                </d:Thumbnail>
            </m:properties>
        </content>
    </entry>
</feed>

and here is the code:

var response = UrlFetchApp.fetch('https://api.datamarket.azure.com/Bing/Search/Image?Query=%27neymar%27&$top=2',options)
var resp = response.getContentText();
var ggg = Xml.parse(resp,false).getElement().getElement('entry').getElement('content').getElement('m:properties');
Logger.log(ggg);

How do I get element <d:MediaUrl m:type="Edm.String">?

update: but still not work

var response = UrlFetchApp.fetch('https://api.datamarket.azure.com/Bing/Search/Image?Query=%27neymar%27&$top=2',options)
var text = response.getContentText();
var eleCont = Xml.parse(text,true).getElement().getElement('entry').getElement('content');
var eleProp = eleCont.getElement('hxxp://schemas.microsoft.com/ado/2007/08/dataservices/metadata','properties')
var medUrl= eleProp.getElement('hxxp://schemas.microsoft.com/ado/2007/08/dataservices','MediaUrl').getText()

 Logger.log(medUrl)
0

1 Answer 1

1

While the provider is using multiple namespaces (signified by m: and d: in front of element names), you can ignore them for retrieving the data you're interested in.

Once you've called getElement() to get the root of the XML doc, you can navigate through the rest using attribute names. (Stop after var feed = ... in the debugger, and explore feed, you'll find you have the entire XML document there

Debugger view

Try this:

  var text = Xml.parse(resp,true);
  var feed = text.getElement();
  var urls = [];
  for (var i in feed.entry) {
    urls.push(feed.entry[0].content.properties.MediaUrl.Text);
  }
  Logger.log(urls);

This also works. Note that you have multiple entries in your response, and this example is going after the second of them:

var ggg = Xml.parse(resp,true)
             .getElement()
             .getElements('entry')[1]
             .getElement('content')
             .getElement('properties')
             .getElement('MediaUrl')
             .getText();

References

Sign up to request clarification or add additional context in comments.

1 Comment

thats work thx for help me out.. i am just know method getelement can you tell me reference for learn this.. once again thx..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.