How to parse this xml file? - android

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'))); }}

Related

Parse XML data from url using DOM Parser

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));
}
}
}
}
}
}

How to get all the values from the child nodes in android 4.0?

I would like to list all the values of the tag inside in android 4.0 as of now I am able to get only one value I have added the snippet which I am using right now & also the xml below.Please help with the this with snippet or example.Thanks a lot.
NodeList nodes = doc.getElementsByTagName("month");
for (int i = 0; i < nodes.getLength(); i++)
{
Element e = (Element) nodes.item(i);
stock_list.add(getValue(e, "month"));
}
Here is my xml
The nodeValue of an XML Element is null by definition. Thus, you can either
query the element's children for the Text node containing the actual contents, or
simply use e.getTextContent() to retrieve the text content of the XML element.
Try this code i was having the same problem and solved it this way..
ArrayList<HashMap<String, String>> stock_list = new ArrayList<HashMap<String, String>>();
NodeList nodes = doc.getElementsByTagName("month");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Node name = nodes.item(i);
NodeList month = name.getChildNodes();
map.put("months", ((Node) month.item(0)).getNodeValue());
stock_list.add(map);
}

Problem in parsing nested tags in xml

I have an xml that i am parsing through DOM parser. the xml is of somewhat this sequence
<root>
<item1> abc </item1>
<item2> def </item2>
<item3> ghi </item3>
<item4>
<subItem4>
<name> xyz </name>
<id> 1 </id>
</subItem4>
<subItem4>
<name> asd </name>
<id> 2 </id>
</subItem4>
</item4>
</root>
According to this dummy xml i am reaching till subItem 4 but not to the childern of it. what i am trying is as follows to get innermost Items is:
NodeList slide = theElement.getElementsByTagName("item4").item(0).getChildNodes();
for(int i = 0; i<slide.getLength(); i++)
{
NodeList subSlides = theElement.getElementsByTagName("subItem4").item(0).getChildNodes();
for (int j=0; j<subSlides.getLength(); j++)
{
String subSlide_title = subSlides.item(i).getFirstChild().getNodeValue();
}
}
its not working. please can someone identify where am i doing the mistake in parsing. Any help is appreciated.
You are not using valid XML - you can't have spaces in tag names.
XML Names cannot contain white space, see here for valid values.
Update (following comment that the posted sample is representative of the actual XML):
Your access via the indexer of the node list is incorrect:
String subSlide_title = subSlides.item(i).getFirstChild().getNodeValue();
Try this instead (using j instead of i, as the inner loop variable is called):
String subSlide_title = subSlides.item(j).getFirstChild().getNodeValue();
NodeList nodes = doc.getElementsByTagName("item");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList nodesimg = element.getElementsByTagName("name");
for (int j = 0; j < nodesimg.getLength(); j++) {
Element line = (Element) nodesimg.item(j);
String value=getCharacterDataFromElement(line);
}
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cd = (CharacterData) child;
return cd.getData();
}
return "?";
}
I think above code will help you in parsing xml file.
The XML elements are all messed up.
There are literally 2 lines that don't have mistakes in them.
For instance
<subItem 4>
is syntactically wrong and I don't see what logical sense you could make out of it.
Do you mean
<subItem4>
as in the fourth sub item or
<subItem someAttribute="4">
I'd recommend learning XML, it's very simple... http://www.w3schools.com/xml/default.asp

Dom parsing in Android

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.

Android DOM parsing problem

i have a xml file and i am parsing it with DOM. `
<media:group>
<media:category label='hi'scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>hello</media:category>
<media:content
url='http://www.youtube.com/1'
expression='full' duration='37' yt:format='5' />
<media:content
url='rtsp://v8.cache8.c.youtube.com.2.gp'
type='video/3gpp' medium='video' expression='full' duration='37'
yt:format='1' />
</media:group>
(Not original links)
like this it has many media:group tag...
my code is giving bellow:
nodeList = doc.getElementsByTagName("media:group");
for (int i = 0; i < nodeList.getLength(); i++) {
try {
currentNode = nodeList.item(i);
Element fstElmnt = (Element) currentNode;
NodeList media_list = fstElmnt
.getElementsByTagName("media:content");
Element mediaElement = (Element) media_list.item(0);
media_list = mediaElement.getChildNodes();
String urlString = mediaElement.getAttribute("url");
Now my problem is this i want all url of all media:content tag,, but getting only 1st url of every media:content tag.
so where i am doing mistake please help me...
You only ask for the first media element in the list by using:
Element mediaElement = (Element) media_list.item(0);
Of course this will crash if the media list is empty. Solution to your problem: Just loop over media_list. ;-)
Your problem lies here (you're getting only the first item on media_list):
Element mediaElement = (Element) media_list.item(0);
In your sample ATOM there's 2 media:content elements. I would iterate through media_list and get the item(i) and do String urlString = mediaElement.getAttribute("url"); and add the urlString into a list. That way, you have all the url in a list.
Hope this helps.

Categories

Resources