I have searched the internet quite a lot about this, but I am still struggling to get the following simple code working. Can anybody give me a hand?
The target platform is Android 2.2.
When evaluating, 1 node is returned, but the value is null - I would like the filename.
XML Reading code:
public String GetValue(String data, String xpath) {
String value = "";
XPath path = XPathFactory.newInstance().newXPath();
try {
XPathExpression exp = path.compile(xpath);
Document doc = this.GetDocument(data);
NodeList nodes = (NodeList)exp.evaluate(doc, XPathConstants.NODESET);
if(nodes.getLength() > 0) {
value = nodes.item(0).getNodeValue();
}
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
Calling Code:
XMLHelper xhelp = new XMLHelper();
this.FileName = xhelp.GetValue(fileData, "tilesheet/filename");
The XML:
<?xml version="1.0" encoding="utf-8"?>
<tilesheet>
<filename>sheet.png</filename>
<cellwidth>32</cellwidth>
<cellheight>32</cellheight>
<columns>9</columns>
<cellcount>9</cellcount>
</tilesheet>
Thank you very much for your help,
Richard Hughes
OK. Just looking at this: How to do XPath or XML parsing in Android It seems this works: /tilesheet/filename/text()
Related
I'm writing an Android app that connects to a SOAP webservice using kSOAP2, and I have a kXML element where I would like to inject a child based on an XML string I got from elsewhere (a REST API). I have the following code:
Element samlHeader = new Element().createElement("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security");
samlHeader.setPrefix("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
samlHeader.setPrefix("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
String samlTokenString = ...; //I got this from elsewhere
Element samlTokenElement = ...; //I don't know how to build this
samlHeader.addChild(Node.ELEMENT, samlTokenElement);
So I'm trying to figure out how to build my Element based on the XML string I'm getting from elsewhere.
This is the solution that we ended up implementing:
try {
KXmlParser parser = new KXmlParser();
parser.setInput(new StringReader(samlTokenString));
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
Document samlTokenDocument = new Document();
samlTokenDocument.parse(parser);
samlHeader.addChild(Node.ELEMENT, samlTokenDocument.getRootElement());
} catch (XmlPullParserException e) {
Log.e(TAG,"Could not parse SAML assertion", e);
} catch (IOException e) {
Log.e(TAG,"Could not parse SAML assertion", e);
}
We're still validating if it produces the right result but it seems to work.
I'm thinking about making my first android app, It'd be about movies, I found an excellent data source, it is "http://www.google.com/movies?" but I wanted to know how could I extract this information and put it in my app,
I've searched but I don't know which is the optimal way to do this? does google have an API for this? is that what I want? is it better with the source code?what could I read or see to learn to do this?
thanks a lot guys, Is my first time as well programming retrieving information from the cloud,
cheers
Yup. Here is one way to do it.
First, you need to find the source of the SQL. The Yahoo Developer Console is a great place to look for this sort of stuff. It has EVERYTHING. The way these resources work is that you have a long link, like this....
developer.yahoo.com/blah/this . . . &q=KEYWORD_HERE+blah/ . . .
To access the information you are looking for, you stick whatever the correct keyword is where "KEYWORD_HERE" is, and the link will give you info in SQL format. I'll be doing the example as a stocks app.
First you create an Activity and define both sides of your link as strings. It'll look a bit like this:
public class InfoActivity extends Activity {
String firstHalf = "http://query.yahooapis.com/v1/public/blahblahblah&q=";
String secondHalf = "+blah/blah&blah . . . ";
Then, in your onCreate, you'll need to start an aSync task to do the actual pulling and parsing:
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.id.layout_name);
final String yqlURL = firstHalf + KEYWORD_HERE + secondHalf;
new MyAsyncTask().execute(yqlURL);
}
Then to define our MrAsyncTask:
private class MyAsyncTask extends AsyncTask<String, String, String>{
protected String doInBackground(String... args) {
try {
URL url = new URL(args[0]);
URLConnection connection;
connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
// Tests if responseCode == 200 Good Connection
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("nodeName1");
if (nl != null && nl.getLength() > 0) {
for (int i = 0 ; i < nl.getLength(); i++) {
//Parse the node here with getTextValue(n1, "Name of element")
//ex: String movieName = getTextValue(n1, "MovieName");
}
}
}
} catch (MalformedURLException e) {
Log.d(TAG, "MalformedURLException", e);
} catch (IOException e) {
Log.d(TAG, "IOException", e);
} catch (ParserConfigurationException e) {
Log.d(TAG, "Parser Configuration Exception", e);
} catch (SAXException e) {
Log.d(TAG, "SAX Exception", e);
}
finally {
}
return null;
}
I hope that gives you some idea of how to do this sort of thing. I'll go see if I can quickly spot a good resource on the yahoo apis to get the movie times at a certain location.
Good luck :) Let me know if you need anything clarified.
EDIT:
Looks like this is EXACTLY what you need (resource wise):
https://developer.yahoo.com/yql/console/?q=show%20tables&env=store://datatables.org/alltableswithkeys#h=select+*+from+google.igoogle.movies+where+movies%3D'68105'%3B
Check that out. Using that, your two halves of the link would be:
String firstHalf = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20google.igoogle.movies%20where%20movies%3D'"
String secondHalf = "'%3B&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
And then to get your final link, you would just do
String yqlURL = firstHalf + "ZIP CODE OF YOUR LOCATION" + secondHalf;
And you would have all of the movies playing near you returned!
Make your life a lot easier and choose the api that is right for you. Choose one of these:
http://www.programmableweb.com/news/52-movies-apis-rovi-rotten-tomatoes-and-internet-video-archive/2013/01/22
Make your decision not only based on the content, but also ease of use and documentation. Documentation is a biggy.
Good luck!
well i would rather advice you to use an TheMovieDB.com API it is simple and provides every info of movies.
I may be misunderstanding what String.contains does. I am now trying to pull a specific link using Jsoup in Android. I'm trying to just get the faceBook one as an example. Ive tried a few things. this one It Seems to be outputting got it on the ones that do not contain the facebook url and leaving the facebook ones blank. How do I just get the FaceBook ones and stop the loop.
protected Void doInBackground(Void... params) {
Document doc = null;
try {
doc = Jsoup.connect("http://www.homedepot.com").get();
Elements link = doc.select("a[href]");
String stringLink = null;
for (int i = 0; i < link.size(); i++) {
stringLink = link.toString();
if (stringLink.contains("https://www.facebook.com/")){
System.out.println(stringLink+"got it");
}
else{
//System.out.println(stringLink+"not it");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
The following line is causing the problem:
stringLink = link.toString();
The link variable is a collection of Elements (in this case every link on the page), so by calling link.toString() you're getting the String representation of every single link on the page all at once! That means stringLink will always contain the facebook link!
Change the line to:
stringLink = link.get(i).toString();
This line gets only the link at index i on each iteration and checks whether or not it contains the facebook link.
I have:
<description><![CDATA[<div><b>Details:</b> <div class=ExternalClassCDAAC64F989B48B1AE79489DFBF8C27C><div><span style="font-size:8pt;font-weight:700"><font color="#008080" face=Verdana><span style="text-decoration:none"><a style="text-decoration:none" href="http://unipune.ac.in/other_academic_and_service_units/board_students_welfare/pdf/Annual_Essay_Competition_Covering_26-5-12.pdf" target="_blank"><font color="#008080">Letter Regarding Annual Essay Prize Competition</font></a></span></font></span></div></div></div>
<div><b>Expires:</b> 8/14/2012</div>
]]></description>
I need a regular expression which will give me only the href contents. I tried with this:
String link1 = a.substring(a.indexOf("href=\""), a.indexOf("\""));
But its giving me a force close error.
The output that I want is somewhat like this:
link = http://unipune.ac.in/other_academic_and_service_units/board_students_welfare/pdf/Annual_Essay_Competition_Covering_26-5-12.pdf
Can anybody help me?
try this:::: but only if this original string will be in the same format.
String[] separated = a.spilt("href=\"");
String[] first = separated[1].spilt("\" target");
String link1 = first[0];
try this
\bhref="([^"<>]+)"
Code
try {
String resultString = subjectString.replaceAll("\\bhref=\"([^\"<>]+)\"", "link=$1");
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
} catch (IllegalArgumentException ex) {
// Syntax error in the replacement text (unescaped $ signs?)
} catch (IndexOutOfBoundsException ex) {
// Non-existent backreference used the replacement text
}
Test it here.
Hope this helps.
Try this.
String link1 = a.substring(a.indexOf("href=\""), a.indexOf("target=")-1);
I want to extract information from the web and show that value in my Android app. When I try to write the following code, nothing gets initialized to my textView. I can't see the data I wanted. Can you please tell me whats wrong?
EDIT: Android is now not even going past the line:
Document doc = Jsoup.connect("http://movies.ign.com/articles/100/1002569p1.html").get();
When I run the emulator, it just exits the App. Why is this happening??
Here is my code:
public class Search extends Activity {
private static final String TAG = "TVGuide";
String outputtext;
Parser parser;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
TextView outputTextView = (TextView) findViewById(R.id.outputTextView);
String id = "main-article-content";
try {
Document doc = Jsoup.connect("http://movies.ign.com/articles/100/1002569p1.html").get();
Elements elementsHtml = doc.getElementsByAttributeValue("id", "main-article-content");
for (Element element : elementsHtml) {
Log.i("PARSED ELEMENTS:", URLDecoder.decode(element.text(), HTTP.UTF_8));
outputTextView.setText(element.text());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I think you imported the class org.w3c.dom.Document instead of the required one, org.jsoup.nodes.Document by mistake.
EDIT: Android is now not even going past the line:
Jsoup cant connect to the site? Try to add timeout on the connect:
Document doc = Jsoup.connect("http://movies.ign.com/articles/100/1002569p1.html").timeout(10000).get();