Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trying to fix a YQL statement - Newbie needs help!

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Trying to fix a YQL statement - Newbie needs help!

    Hello I am new to programming so please bear with me as this may be the stupidest question yet!

    I am trying to pull data through JSON from yahoo finance pages with the following code. When I run the code in the YQL console it throws the following error.

    THIS IS THE ERROR:

    <?xml version="1.0" encoding="UTF-8"?>
    <query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
    yahoo:count="0" yahoo:created="2012-04-13T10:28:51Z" yahoo:lang="en-US">
    <diagnostics>
    <publiclyCallable>true</publiclyCallable>
    <url execution-start-time="24" execution-stop-time="27"
    execution-time="3" proxy="DEFAULT"><![CDATA[http://www.datatables.org/yahoo/finance/yahoo.finance.stock.xml]]></url>
    <javascript><![CDATA[Error: double default label in the switch statement<javascript>:180: default:^]]></javascript>
    <javascript><![CDATA[Error: invalid return<javascript>:191:return ^true;]]></javascript>
    <javascript><![CDATA[Error: syntax error<javascript>:192: }^]]></javascript>
    <javascript><![CDATA[Exception: Compilation produced 3 syntax errors. (<javascript>#1)]]></javascript>
    <javascript execution-time="2" instructions-used="0" table-name="yahoo.finance.stock"/>
    <user-time>32</user-time>
    <service-time>3</service-time>
    <build-version>26535</build-version>
    </diagnostics>
    <results/>
    </query>

    THIS IS THE CODE THAT THROWS THE ERROR

    <?xml version="1.0" encoding="UTF-8"?>
    -
    <table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
    -
    <meta>
    <author>Bob Cudmore</author>
    <description>Yahoo Finance Stock Summary Information</description>
    <sampleQuery>select * from {table} where symbol="yhoo"</sampleQuery>
    <documentationURL>To Be Declared</documentationURL>
    </meta>
    -
    <bindings>
    -
    <select produces="XML" itemPath="">
    -
    <urls>
    <url />
    </urls>
    -
    <inputs>
    <key required="true" paramType="variable" type="xs:string" id="symbol" />
    </inputs>
    -
    <execute>
    <![CDATA[

    // pad string with leading char String.prototype.pad = function (padchar, padlen)
    { s = this while (s.length < padlen)
    { s = padchar + s;
    }
    return s;
    }

    String.prototype.trim = function ()
    {
    var str = this.replace(/^\s\s*/, ''), ws = /\s/, i = str.length; while (ws.test(str.charAt(--i))); return str.slice(0, i + 1);

    }

    String.prototype.toInt = function ()
    {
    // remove leading zeros because otherwise str can be interpreted as Octal

    var str = this.replace(/^0+/, ''); return parseInt(str);

    }

    function getQuoteInfo()
    {
    // Get company name & market from Yahoo Quotes Summary page

    var results = quoteQuery.results; elements = results.*.length(); if (elements == 0) return false;
    stock.CompanyName = results.h1.text().toString().trim(); var marketSymbolStr = results.div.span.toString();

    //var match = marketSymbolStr.match(/^[^:]+:\s+([^)]+)\)$/); matches stock symbol

    var match = marketSymbolStr.match(/^\(([^:]+):/); if (match != null)
    {

    stock.CompanyName += <Market>{match[1]}</Market>;

    }

    return true;
    }

    function getHistoricalPrice()
    {
    // Get the Historical Price Range

    var results = historicalQuery.results; elements = results.*.length();
    if (elements < 6)
    {

    y.log("not enough elements");
    return false;
    }

    startMonth = String(results.option[0].@value.toString().toInt() + 1) .pad("0", 2);
    startDay = results.input[0].@value.toString().pad("0", 2);
    startYear = results.input[1].@value.toString();
    endMonth = String(results.option[1].@value.toString().toInt() + 1) .pad("0", 2);
    endDay = results.input[2].@value.toString().pad("0", 2);
    endYear = results.input[3].@value.toString();
    startDate = startYear + "-" + startMonth + "-" + startDay;
    endDate = endYear + "-" + endMonth + "-" + endDay;
    stock.appendChild(<start>{startDate}</start>);
    stock.appendChild(<end>{endDate}</end>);
    stock.appendChild(<StartDate>{startDate}</StartDate>);
    stock.appendChild(<EndDate>{endDate}</EndDate>);
    return true;
    }

    function getExecutive(executives, tr)
    {

    var executive = <Executive></Executive>;
    executive.node += <Name>{tr.td.strong.text()}</Name>;

    var m = tr.td.p[0].text().toString().match(/^,\s*(\d+)/); if (m)
    {

    executive.node += <Age>{m[1]}</Age>;
    }

    var titles = tr.td.p[0].small.toString().split(/,\s*|\s+and\s+/i);
    for (var i=0; i<titles.length; i++) executive.node += <Title>{titles[i]}</Title>;
    var pay = tr.td[1].p.text().toString().replace(/\s+/g, "");
    executive.node += <Pay>{pay}</Pay> var exercised = tr.td[2].p.text().toString().replace(/\s+/g, "");
    executive.node += <Exercised>{exercised}</Exercised> executives.node += executive;

    }

    function getAddressInfo(stock, td)

    {
    var companyName = td.strong.text().toString().trim(); //stock.node += <Name>{companyName}</Name>;
    var mapref = td.p.a[0].@href;
    var m = mapref.match(/addr=([^&]+)&csz=([^&]+)&country=(.*)$/i);

    if (m)
    {
    var address = m[1].replace(/%20/g, " ");
    var country = m[3].replace(/%20/g, " ");
    var csz = m[2].replace(/%20/g, " ");
    var m2 = csz.match(/(.*?)\s+([a-z]+)\s+([-\d]+)$/i);

    if (m2)
    {
    var city = m2[1];
    var state = m2[2];
    var zip = m2[3];
    }
    else
    {
    var city = csz;
    }
    var address = <Address> <Street>{address}</Street> <City>{city}</City> <State>{state}</State> <Zip>{zip}</Zip> <Country>{country}</Country> </Address>;
    stock.node += address;
    }

    var firstPara = td.p[0].toString();
    firstpara = firstPara.replace(/&lt;/, "<");
    firstpara = firstPara.replace(/&gt;/, ">");

    var lines = firstPara.split(/\s*(<p>|<br\s*\/>)\s*/i);
    for (var i=0; i<lines.length; i++)

    {

    var colonMatch = lines[i].match(/^([^:]+)\s*?!\/)\s*(.*?)\s*$/);
    if (colonMatch)

    {

    var property = colonMatch[1].replace(/\s/, "");
    var value = colonMatch[2];
    var aMatch = value.match(/<a\b[^>]+>\s*([^<]+?)\s*<\/a>/i);

    if (aMatch) value = aMatch[1];
    stock.node += <{property}>{value}</{property}>;
    }
    }
    }

    function getProfileInfo()
    {
    // Get the Sector, Industry, Full Time Employees from Profile page

    var results = profileQuery.results;
    elements = results.*.length();
    if (elements == 0) return false;

    getAddressInfo(stock, results.td);
    var executives = <Executives></Executives> for each (var tr in results.tr)
    {

    //Remove trailing colon, and strip whitespace

    var property = tr.td[0].p.text() .toString().slice(0, -1) .replace(/\s+/g, "");
    switch (property.toLowerCase())

    {

    case 'indexmembership':
    var indexes = <Indexes></Indexes> for each (var a in tr.td[1].a)

    {

    var iSymbol = a.@href.match(/=([^&]+)/)[1];
    iSymbol = iSymbol.replace(/%5E/gi, "^");
    indexes.node += <Index symbol={iSymbol}>{a.text()}</Index>;
    }

    if (indexes.length() > 0) stock.appendChild(indexes);
    continue;
    break;
    case 'fulltimeemployees':

    //Strip commas

    value = tr.td[1].*.text().toString().replace(/,/g, "");
    break;

    default: case 'sector': case 'industry':

    //Convert whitespace to single space

    value = tr.td[1].*.text().toString().replace(/\s+/g, " ");
    break;

    default: if (property.match(/^,/)) getExecutive(executives, tr);
    continue;
    break;
    }

    stock.appendChild(<{property}>{value}</{property}>);
    }

    if (executives.length() > 0) stock.appendChild(executives);
    return true;
    }

    // Queue the queries

    var url = "http://finance.yahoo.com/q/pr?s="+symbol;
    var profileQuery = y.query( "select * from html " + "where url=@url and " + "xpath='//table[@class=\"yfnc_datamodoutline1\"]/tr/td/table/tr' "
    + "limit 4", "xpath='" + "//td[@class=\"yfnc_modtitlew2\"] | " + "//table[@class=\"yfnc_datamodoutline1\"]/tr/td/table/tr"
    + "'", {url:url});

    var url = "http://finance.yahoo.com/q?s="+symbol;
    var quoteQuery = y.query( "select * from html " + "where url=@url and " + "xpath='"
    + "//div[@id=\"yfi_investing_head\"]/h1 | " + "//div[@class=\"yfi_quote_summary\"]/div[1]'" , {url:url});
    var url = "http://finance.yahoo.com/q/hp?s="+symbol;
    var historicalQuery = y.query( "select * from html " + "where url=@url and " + "xpath='" + "//option[@selected=\"selected\"] | " + "//input[@maxlength=\"2\"] | "
    + "//input[@maxlength=\"4\"]'", {url:url});
    var stock = <stock symbol={symbol}></stock>; getQuoteInfo(); getHistoricalPrice(); getProfileInfo(); response.object = stock ]]>
    </execute>

    I have attached the source XML file as a RAR file for ease of reading in dreamweaver or some other editor.

    Any help would much appreciated.

    Bob
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trying to fix a YQL statement - Newbie needs help!

    Try a javascript forum. This one is for java.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Trying to fix a YQL statement - Newbie needs help!

    And I don't need help :<
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Newbie here :D
    By maryzark19 in forum Member Introductions
    Replies: 1
    Last Post: October 10th, 2011, 10:43 AM
  2. newbie help
    By willmeister in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2011, 05:30 PM
  3. Newbie help please
    By RIB in forum Member Introductions
    Replies: 3
    Last Post: August 16th, 2011, 02:29 PM
  4. another newbie..
    By xdigg in forum Member Introductions
    Replies: 0
    Last Post: February 15th, 2010, 07:51 AM
  5. newbie here
    By salpoe in forum Member Introductions
    Replies: 0
    Last Post: January 15th, 2010, 01:05 AM