void EarthquakeApp::parseEarthquakes( const string &url ) { XmlDocument doc( loadUrlStream( url ) ); vector<XmlElement> items( doc.rootNode().children() ); for( vector<XmlElement>::iterator itemIter = items.begin(); itemIter != items.end(); ++itemIter ) { if( itemIter->name() == "entry" ) { // Title is in the form "M 3.3, Puerto Rico region" const XmlElement &titleEl = itemIter->findChild( "title" ); istringstream titleString( titleEl.value() ); string dummy, title; float magnitude; titleString.ignore( 2, ' ' ); titleString >> magnitude; titleString.ignore( 2, ' ' ); char lineBuffer[512]; titleString.getline( lineBuffer, 511 ); title = string( lineBuffer ); const XmlElement &locationEl = itemIter->findChild( "georss:point" ); istringstream locationString( locationEl.value() ); Vec2f locationVector; locationString >> locationVector.x >> locationVector.y; mEarth.addQuake( locationVector.x, locationVector.y, magnitude, title ); } } }
void EarthquakeApp::parseEarthquakes( const string &url ) { try { const JsonTree json( loadUrl( url ) ); for( auto &feature : json["features"].getChildren() ) { auto &coords = feature["geometry"]["coordinates"]; float mag = feature["properties"]["mag"].getValue<float>(); string title = feature["properties"]["title"].getValue(); mEarth.addQuake( coords[0].getValue<float>(), coords[1].getValue<float>(), mag, title ); } } catch( ci::Exception &exc ) { console() << "Failed to parse json, what: " << exc.what() << std::endl; } //mEarth.addQuake( 37.7f, -122.0f, 8.6f, "San Francisco" ); }
void EarthquakeApp::parseEarthquakes( const string &url ) { const XmlTree xml( loadUrl( Url( url ) ) ); for( XmlTree::ConstIter itemIter = xml.begin( "feed/entry" ); itemIter != xml.end(); ++itemIter ) { string titleLine( itemIter->getChild( "title" ).getValue() ); size_t firstComma = titleLine.find( ',' ); float magnitude = fromString<float>( titleLine.substr( titleLine.find( ' ' ) + 1, firstComma - 2 ) ); string title = titleLine.substr( firstComma + 2 ); istringstream locationString( itemIter->getChild( "georss:point" ).getValue() ); Vec2f locationVector; locationString >> locationVector.x >> locationVector.y; mEarth.addQuake( locationVector.x, locationVector.y, magnitude, title ); } console() << xml << std::endl; //mEarth.addQuake( 37.7f, -122.0f, 8.6f, "San Francisco" ); }