I want to parse xml file from url :
http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=cher&api_key=5d6ce941674603e4bb75cfad6cfa13b7
I want to parse following tags of the file :
<artist>
<name>Cher</name>
<image size="medium">http://userserve-ak.last.fm/serve/64/62286415.png</image>
</artist>
But i don't know how to get the value of these two tags only.
I have tried the example code from
http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/
But it does not showing to parse same tag having different attribute value.
Can anyone guide me how this is done?
Thanx in advance.
from the link you provided, I have just extract a small part :
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName("artist");
// looping through all item nodes <artist>
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
String name = parser.getValue(e, "name"));
String image = parser.getValue(e, "image"));
//if you want the artist 'Cher' sigh ;)
if (name.equals("Cher")){
//do whatever you want
}
}
Thankx. I solved my problem from this url :
Getting element using attribute
if(str.equals("image"))
{
n = item.getElementsByTagName(str);
for (int i = 0; i < n.getLength(); i++) {
Node subNode = n.item(i);
if (subNode.hasAttributes()) {
NamedNodeMap nnm = subNode.getAttributes();
for (int j = 0; j < nnm.getLength(); j++) {
Node attrNode = nnm.item(j);
if (attrNode.getNodeType() == Node.ATTRIBUTE_NODE) {
Attr attribute = (Attr) attrNode;
if( attribute.getValue().equals("medium"))
{
return this.getElementValue(n.item(i));
}
}
}
}
}
}
Related
I am trying to build a app that display the feed of a given twitter account without using Twitter's Oauth, instead I converted the twitter account feed page to an xml format using Twitrss.me the image src is buried inside the img tag under the src attribute
<img xmlns="http://www.w3.org/1999/xhtml" width="250"
src="https://pbs.twimg.com/media/CmEqtEuUoAISIC3.jpg"
xml:base="http://twitrss.me/twitter_user_to_rss/?user=capcomfighters" />
I was able to get all the other tags text content but I have no idea how to access the tag attributes and get the value for specific attribute
also if there is a better way to do this than parsing xml please let me know
I am using this function to get the content of the tags
private void ProcessXml(Document data) {
if (data != null) {
feedItems=new ArrayList<>();
Element root = data.getDocumentElement();
Node channel = root.getChildNodes().item(1);
NodeList items = channel.getChildNodes();
for (int i = 0; i < items.getLength(); i++) {
Node currentchild = items.item(i);
if (currentchild.getNodeName().equalsIgnoreCase("item")) {
FeedItem item=new FeedItem();
NodeList itemchilds = currentchild.getChildNodes();
for (int j = 0; j < itemchilds.getLength(); j++) {
Node current = itemchilds.item(j);
if (current.getNodeName().equalsIgnoreCase("title")){
item.setTitle(current.getTextContent());
item.setDescription(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("description")){
// item.setDescription(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("pubDate")){
item.setPubDate(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("img")){
item.setThumbnailUrl(current.getAttributes().getNamedItem("src").getNodeValue());
Log.d("PIC", "ProcessXml: "+current.getAttributes().getNamedItem("src").getNodeValue());
}
}
feedItems.add(item);
}
}
}
}
I have a String variable containing an XML:
String xml = "<?xml version="1.0" encoding="utf-8" standalone="yes"?><CourtactSioux><ListeContact><IdContactClient>212</IdContactClient><DateContact>25/06/2012 08:09</DateContact><TypeForm>STANDARD</TypeForm><Foyer>2</Foyer><Civilite>M</Civilite><Nom>TEST</Nom><Prenom>JULIEN</Prenom><NomJeuneFille></NomJeuneFille></ListeContact></CourtactSioux>"
And I want to take values from this XML, how to do ?
For exemple: get "Civilite" value.
I tried this:
String xml = cn.getXml();
Integer demandeId = cn.getID();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
idApplication = parser.getValue(e, IdApplication);
idContactClient = parser.getValue(e, IdContactClient);
logement = parser.getValue(e, Logement);
typeForm = parser.getValue(e, TypeForm);
}
id = idApplication + idContactClient;
Product product = new Product()
.setId(id)
.setName(logement)
.setCategory(typeForm)
.setPrice(1)
.setQuantity(1);
ProductAction productAction = new ProductAction(ProductAction.ACTION_PURCHASE)
.setTransactionId(id)
.setTransactionAffiliation("Solutis")
.setTransactionRevenue(1);
HitBuilders.ScreenViewBuilder builder = new HitBuilders.ScreenViewBuilder()
.addProduct(product)
.setProductAction(productAction);
Tracker t = ((App) getApplication()).getTracker();
t.setScreenName("transaction");
t.send(builder.build());
}
It's work, get attention on the aprent node of your xml
There are several ways of parsing XML in android. You could use XMLReader.parse(InputSource) by wrapping the string in a reader, as shown here
Check the Parsing XML Data part of Android Developers site for more information.
I have create news application rss feed on xml parsing for collage project
this is xml http://goo.gl/X8tIr1 so any one guide me for build that application or give code to help for collage project
Can Anyone give me the whole code
Go through Creating-a-simple-rss-application-in-android. This should get you started.
I'm giving you how to XML PArsing works in doInBackground. Try like this :
DOMParser dParser = new DOMParser();
String abc = dParser.getXmlFromUrl(params[0]);
Document doc = dParser.getDocElement(abc);
NodeList n1 = doc.getElementsByTagName("channel");
for (int i = 0; i < n1.getLength(); i++) {
Element e = (Element) n1.item(i);
NodeList nl1 = e.getElementsByTagName("item");
for (int j = 0; j < nl1.getLength(); j++) {
Element e1 = (Element) nl1.item(j);
NodeList ntitle = e1.getElementsByTagName(TITLE);
NodeList ndate = e1.getElementsByTagName(PUBDATE);
NodeList ndesc = e1.getElementsByTagName(DESCRIPTION);
for (int k = 0; k < n1.getLength(); k++) {
Element e2 = (Element) ntitle.item(k);
Element e3 = (Element) ndate.item(k);
Element e4 = (Element) ndesc.item(k);
Pojo pojo = new Pojo();
pojo.setTitle(getCharacterDataFromElement(e2));
pojo.setPubdate(getCharacterDataFromElement(e3));
pojo.setDescription(getCharacterDataFromElement(e4));
itemList.add(pojo);
}
}
}
return null;
Hi i want to parse this
<entry>
<id>http://306721</id>
<title type='text'>MY New</title>
<photo:id>513306721</gphoto:id>
<photo:name>MYNew</gphoto:name>
<photo:numphotos>9</gphoto:numphotos>
<media:group>
<media:content url='http:Ya4MIz9Y/MYNew.jpg' medium='image' type='image/jpeg' />
<media:keywords />
<media:thumbnail url='htt0-c/MYNew.jpg' height='160' width='160' />
<media:title type='plain'>MY New</media:title>
</media:group>
</entry>
i am able to parsing this file, and also able to read some values from the above xml document like this
Document doc = db.parse(is);
NodeList entries = doc.getElementsByTagName("entry");
for (int i = 0; i < entries.getLength(); i++) {
Element element = (Element) entries.item(i);
albumIds.add(getCharacterDataFromElement((Element) element
.getElementsByTagName("photo:id").item(0)));
}
in the above code i am reading gphoto:id like this i am reading photo:name and photo:numphotos.
Now i want to read url from the media:thumbnail those are available in the media:group.. Can any one help me on this how to read this.
Please see below link of my SO Question, it will solve your problem and if you have any query regarding that then tell me.
XML Parsing Using DOM Parser
Add those values in to object(using java pojo) and add that object into ArrayList
try to use Below Code. Hope it will help you.
Document doc = db.parse(is);
NodeList entries = doc.getElementsByTagName("entry");
for (int i = 0; i < entries.getLength(); i++) {
Element element = (Element) entries.item(i);
albumIds.add(getCharacterDataFromElement((Element) element
.getElementsByTagName("gphoto:id").item(0)));
NodeList nodelist_group = doc.getElementsByTagName("media:group");
for (int j = 0; j < nodelist_group.getLength(); j++) {
Element element = (Element) nodelist_group.item(j);
NodeList nodelist_content = doc.getElementsByTagName("media:content");
URLS.add(nodelist_content.getAttribute('url'))); }}
If I parse the tag that contains <p>Some Text</p> tag, I get a null pointer exception.
My RSS feed is as follows:
<quaddeals_conditions><p>Limit one QuadDeal</p></quaddeals_conditions>
My code is:
if (name.equalsIgnoreCase("quaddeals_conditions")) {
property.normalize();
conditions = property.getFirstChild().getNodeValue();
}
You have an element inside an element .
Therefore retrieve all quaddeals and then iterate each one and retrieve from it the p element:
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.inputStream);
Element root = dom.getDocumentElement();
// snip
NodeList items = root.getElementsByTagName("quaddeals_conditions");
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("p")) {
property.getFirstChild().getNodeValue(); // Your paragraph data
}
}
}
Hope this helps.
is "name" not NULL? I dont see you check for that.
It's good coding practice to compare the other way if possible:
if ("quaddeals_conditions".equalsIgnoreCase(name))...
So even if "name" is NULL, you don't get a NullPointerException.
Always check for not null before accessing some object member.