Android parse a very simple XML Array - android

This might sound dumb, but I just need the help with the logic,
I have this very simple XML array:
<plist version="1.0">
<array>
<string>Lisa Jackson</string>
<string>Elisabeth Hartmann</string>
<string>C. J. Sansom</string>
<string>Irmengard Gabler</string>
<string>Oliver Pötzsch</string>
<string>Ulla Illerhaus</string>
<string>Christopher J. Sansom</string>
<string>Nina Blazon</string>
<string>Nicholas Lessing</string>
<string>Johannes Steck</string>
<string>Peter Kaempfe</string>
<string>Dimeter Inkiow</string>
<string>Barbara Sher</string>
<string>Ulrike Hübschmann</string>
<string>Otfried Preußler</string>
<string>Ulla Illerhaus</string>
<string>Annette Kurth</string>
</array>
</plist>
but when I try to parse it this way:
static final String KEY_ITEM = "string";
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_ITEM, parser.getValue(e, KEY_ITEM));
menuItems.add(map);
}
It displays the list based on the number of the given (17 list items) on the XML file, but it doesn't display any of the content, like 'Lisa Jackson', 'Elisabeth Hartmann', etc.
How can I resolve this?

element.getTextContent();
This will get the text value needed.
OR
for (int in = 0; in < numberOfChildren; in++) {
Node node = nl.item(in);
Log.d("skt",node.getNodeName() + " = " + node.getTextContent());
//node.getTextContent() has the text. Add it to map
}

Related

Get values from a XML contained in a String

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.

Get all data and sub data of a complexe xml in organized way

I want to get data in my xml with her associated sub data : for example I want to take all the name3 in categorie2 and all the name5 who are in this name3 and all the name7 who are in this name5 etc...
Currently I only know how to get all the name3, all the name 5 and all the name7 but I can't know who associated with who.
If you need more information, ask me.
Thank for your help.
EDIT
I tried this for take data :
url3= new String("");
xml3 = parser.getXmlFromUrl(url3);
Document doc3 = parser.getDomElement(xml3);
//System.out.println(xml);
menuBase = new ArrayList<HashMap<String, String>>();
NodeList nodes = doc3.getElementsByTagName(KEY_CATEGORY_2);
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element element = (Element) nodes.item(i);
NodeList title = element.getElementsByTagName(KEY_NAME_3);
Element line = (Element) title.item(0);
map.put(KEY_NAME_3, getCharacterDataFromElement(line));
menuBase.add(map);
menu1 = new ArrayList<ArrayList<HashMap<String, String>>>();
ArrayList<HashMap<String, String>> map1 = new ArrayList<HashMap<String, String>>();
NodeList n = doc3.getElementsByTagName(KEY_CATEGORY_4);
for (int y = 0; y < n.getLength(); y++) {
HashMap<String, String> map1bis = new HashMap<String, String>();
Element e = (Element) n.item(y);
NodeList title1 = e.getElementsByTagName(KEY_NAME_5);
Element line1 = (Element) title1.item(0);
map1bis.put(KEY_NAME_5, getCharacterDataFromElement(line1));
map1.add(map1bis);
menu1.add(map1);
}
}
and then for i tried to create menu and sub menu like that:
for(int i=0;i<menuBase.size();i++){
SubMenu x=menu.addSubMenu(menuBase.get(i).get(KEY_NAME_3).toString());
for(int y=0;y<menu1.get(i).size();y++){
SubMenu w=x.addSubMenu(menu1.get(i).get(y).get(KEY_NAME_5).toString());
}
}
but in each menu item i have all my sub menu item
I finally got a solution of mine probleme :
NodeList nodes = doc3.getElementsByTagName(KEY_CATEGORY_2);
int sum0=0;
for(int i=0;i<menuBase.size();i++){
SubMenu x=menu.addSubMenu(changeToRegularForm(menuBase.get(i).get(KEY_NAME_3).toString()));
int count0 = ((Element)(nodes.item(i))).getElementsByTagName(KEY_CATEGORY_4).getLength();
System.out.println(count0);
if(count0==0){
/*x.findItem(i).set*/
}else{
for(int y=sum0;y<sum0+count0;y++){
SubMenu w=x.addSubMenu(changeToRegularForm(menu1.get(y).get(KEY_NAME_5).toString()));
}
}
sum0+=count0;
}
explanation : Finally i take all the child element in a one arryList but i use a fonction to count how each element have child element and had them.
If you need more information/accuracy, just ask for.

Web service with NFC

I have a problem when I want to make a webservice stops the app is not that any ideas?
private String dumpTagData(Parcelable p) {
StringBuilder sb = new StringBuilder();
Tag tag = (Tag) p;
byte[] id = tag.getId();
sb.append("ID Producto:").append(getDec(id)).append("\n");
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(0);
// adding each child node to HashMap key => value
sb.append("Nombre:").append(parser.getValue(e, KEY_NAME)).append("\n");
sb.append("Costo:").append(parser.getValue(e, KEY_COST)).append("\n");
sb.append("Descripción:").append(parser.getValue(e, KEY_DESC)).append("\n");
sb.delete(sb.length() - 2, sb.length());
return sb.toString();
}
usually you don't want to let a server stop a client app, when it comes to webservices. It is much like a TV-station shutting down your television.

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

How to parse XML (especific structure) in Android?

I need to parse a specific part of an XML obtained from a http request but I do not know how to do it!
I have the following XML structure being returned:
<categorias type="array">
<categoria>
<nome>
Alimentação
</nome>
<idcategoria>
5
</idcategoria>
<subcategorias>
<subcategoria>
<nome>
Todos
</nome>
<id>
5
</id>
</subcategoria>
</subcategorias>
</categoria>
</categorias>
I need to parse the data inside the subcategorias
tag because with the code I have now, I get only the upper tags, like
nome and idcategoria from the root tag categoria.
I've created a NodeList inside the for loop but it return all the subcategoria tags in the Document. And I need to get only the ones inside a unique categoria tag.
Here's the code I have now:
menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
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++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
// adding HashList to ArrayList
menuItems.add(map);
}
Can someone help me on this!?
Thanks in advance!
Have a look at this official document of Android which describe XMLPullParser.
You have to implement your logic according to your XML structure.
Aside from XMLPullParser, another option for parsing XML is to use the Simple XML library. It will allow you to easily deserialize that XML into an object which you can manipulate however you wish. It would be possible for you to ignore all other child elements in categoria and just capture subcategorias by using loose object mapping. Check the Simple Framework documentation for more information.
#Android Coader is right, "You have to implement your logic according to your XML structure"
Why do you have a KEY_ITEM, when there are no "item" tags in your XML? You need to specify in the code which tags you want to retrieve. For example, if you define a KEY_SUBCATEGORIA = "subcategoria", then to get all subcategory nodes you can do
NodeList nl = doc.getElementsByTagName(KEY_SUBCATEGORIA);
Then you should navigate through each node, pulling out the name and id's as you are doing now.
I got it!
I used only the same kind of structure I had before!
I added this code inside the for loop I had:
NodeList nlSubcategorias = e.getElementsByTagName("subcategoria");
ArrayList<HashMap<String, String>> subcategorias = new ArrayList<HashMap<String, String>>();
for (int j = 0; j < nlSubcategorias.getLength(); j++) {
HashMap<String, String> mapSub = new HashMap<String, String>();
Element eSub = (Element)nlSubcategorias.item(j);
mapSub.put(KEY_ID_SUB, parser.getValue(eSub, KEY_ID_SUB));
mapSub.put(KEY_NAME_SUB, parser.getValue(eSub, KEY_NAME_SUB));
subcategorias.add(mapSub);
System.out.println("subcategorias: " + subcategorias.get(j).get(KEY_NAME_SUB));
}
subcategoriasItems.add(subcategorias);

Categories

Resources