I'm looking for help to get an XML to an arraylist.
here is the XML :
<campagne>
<sms><texte>
Vente a Drouot
</texte>
<list>
<id> 1 </id>
<nom> TOTO </nom>
<id> 2 </id>
<nom> TATA </nom>
<id> 3 </id>
<nom> Mr.Gerard </nom>
</list>
</sms>
</campagne>
I want to have TOTO,TATA,Mr.Gerard to a StringArray[] exactly like if I put manually : String[] Customers = {"TOTO","TATA","Mr.Gerard"}
for now my XMlPullParser (I have written " ArrayList clientslist = null; " before and I want to put the difference name in this Array )
:
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text = null;
ArrayList<Client> clientslist = null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if (name.equals("texte"))
message = text.trim();
else if (name.equals("nom"))
clientslist = text.trim(); // error is here
else
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
With this code I have Mr.Gerard ONLY...
I suppose clients is a String - it should be a List<String> instead and clients = text.trim(); should be replaced with clients.add(text.trim());
Then you can call clients.toArray(new String[clients.size()]) to get a String array you wanted.
What is the clients member variable? Without seeing the rest of the code it appears that you are simply over-writing it with whatever happens to be the last <nom> element.
Use a List and add each <nom> to that and then convert the list to a string array.
Use index for the string array to update the values.
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
int counter = 0;
String text = null;
String[] clients = {};
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if (name.equals("texte"))
message = text.trim();
else if (name.equals("nom"))
clients[counter++] = text.trim();
else
break;
}
event = myParser.next();
}
parsingComplete = false;
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<String> parseXMLAndStoreIt(XmlPullParser myParser) {
ArrayList<String> clientslist = new ArrayList<String>; // 1
...
else if (name.equals("nom")){
clients = text.trim();
clientslist.add(text.trim()); //2
}
...
return clientslist; //3
}
at the end , you can get the list
Related
I am not full aware of XMLParsing so i am beginner in XMLParsing. I am using XMLPull parser.
i wanna to store player one and player two in below xml. How can i identify playertag??
<scores sport="tennis">
<category name="Atp - Doubles: Us Open (Usa), Hard" city="Us Open" id="11987">
<match date="08.09.2016" time="19:00" status="Not Started" id="396388">
<player name="Herbert/ Mahut" s1="" s2="" s3="" s4="" s5="" totalscore="" id="22079"/>
<player name="Murray/ Soares" s1="" s2="" s3="" s4="" s5="" totalscore="" id="33098"/>
</match>
<match date="08.09.2016" time="20:30" status="Not Started" id="396400">
<player name="Lopez/ Lopez" s1="" s2="" s3="" s4="" s5="" totalscore="" id="9632"/>
<player name="Carreno-Busta/ Garcia-Lopez" s1="" s2="" s3="" s4="" s5="" totalscore="" id="22770"/>
</match>
</category>
</scores>
Here is my code
public List<LiveScore> parse(InputStream is) {
try {
String tournament = null,tournament_place = null,date = null,time = null,status = null,player_one = null,player_two;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(is, null);
liveScoreArrayList = new ArrayList<LiveScore>();
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if ("category".equals(tagname)){
tournament = parser.getAttributeValue(null,"name");
tournament_place = parser.getAttributeValue(null,"city");
}
if ("match".equals(tagname)){
date = parser.getAttributeValue(null,"date");
time = parser.getAttributeValue(null,"time");
status = parser.getAttributeValue(null,"status");
}
break;
case XmlPullParser.TEXT:
// text = parser.getText();
// Log.i(TAG, "parse: text: "+text);
break;
case XmlPullParser.END_TAG:
if ("player".equals(tagname)){
player_one = parser.getAttributeValue(null,"name");
}
Log.d("LiveMatch", "parse: category : "+tournament+" city = "+tournament_place+" date = "+date+ " time = "+time+" status = "+status+" player_one = "+player_one);
// rankList.add(new Rank(rank,player_name+" \n"+country,points ));
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
return liveScoreArrayList;
}
This is Complete Xml Pull Parsing example.. It may Help you.
https://androidgarden.wordpress.com/2016/09/02/android-xml-pull-parsing-from-sd-card-assets/
I'm trying to make an app for a podcast. My end goal is for a user to be able to click on an episode in a list (which is generated from an RSS feed) and then have the audio play (which is also retrieved from the RSS feed) My problem is with parsing the XML from the RSS feed. I can't seem to figure out how to get the URL out of the enclosure tag for the mp3. Any help would be appreciated. Here is my parsing class
private String data;
private ArrayList<Episodes> mEpisodes;
public ParseEpisodes(String xmlData) {
this.data = xmlData;
mEpisodes = new ArrayList<>();
}
public ArrayList<Episodes> getEpisodes() {
return mEpisodes;
}
public boolean process(){
boolean status = true;
Episodes currentRecord = null;
boolean inEntry = false;
String textValue = "";
String urlValue = "";
try{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader(this.data));
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT){
String tagName = xpp.getName();
switch(eventType){
case XmlPullParser.START_TAG:
//Log.d("ParseEpisodes", "Starting tag for " + tagName);
if(tagName.equalsIgnoreCase("item")){
inEntry = true;
currentRecord = new Episodes();
}
break;
case XmlPullParser.TEXT:
textValue = xpp.getText();
urlValue = xpp.getText();
break;
case XmlPullParser.END_TAG:
//Log.d("ParseEpisodes", "Ending tag for " + tagName);
if(inEntry){
if(tagName.equalsIgnoreCase("item")){
mEpisodes.add(currentRecord);
inEntry = false;
}else if(tagName.equalsIgnoreCase("title")){
currentRecord.setTitle(textValue);
}else if(tagName.equalsIgnoreCase("enclosure")){
currentRecord.setLink(urlValue);
}
}
break;
}
eventType = xpp.next();
}
}catch (Exception e){
status = false;
e.printStackTrace();
}
return true;
}
here is the xml
<item>
<title>Episode 52 - Facebook Nukes The Show</title>
<link>https://greynoi.se/podcasts/episode-52-facebook-nukes-the-show</link>
<enclosure url="http://greynoi.se/episodes/ep79_m.mp3" length="43312404" type="audio/mpeg"/>
<pubDate>Fri, 15 Jul 2016 15:22:24 -0700</pubDate>
<category>1</category>
<source url="https://greynoi.se">The GR3YNOISE Podcast</source>
<itunes:subtitle>Episode 52 - Facebook Nukes The Show</itunes:subtitle>
<itunes:summary>Episode 52 - Facebook Nukes The Show</itunes:summary>
<content:encoded><![CDATA[Episode 52 - Facebook Nukes The Show"}]]></content:encoded>
<guid>https://greynoi.se/podcasts/episode-52-facebook-nukes-the-show</guid>
</item>
So you want the url after the enclosure url= tag?
If that is the case it is very easy. Just use substrings to find and get the url.
Something like this should work.
int urlTagStart = text.indexOf("<enclosure url="");
int urlTagEnd = text.indexOf(".mp3");
String url = text.substring(urlTagStart + 16, urlTagEnd + 4)
Try this:
...
YOUR CODE
...
while(eventType != XmlPullParser.END_DOCUMENT){
String tagName = xpp.getName();
if (tagName.equalsIgnoreCase("enclosure")) {
int attributeCount = xpp.getAttributeCount();
for(int i = 0; i<attributeCount; i++){
String attrib = xpp.getAttributeName(i);
if(attrib && attrib.equalsIgnoreCase("url")){
String url = xpp.getAttributeValue(i);
}
}
}
if (url != null && !url.isEmpty() && !url.equals("null")) {
Log.d("ParseEpisodes", "URL to mp3: " + url);
}
...
THE REST OF YOUR CODE
...
}
The solution to this ended up being pretty simple. All I needed to do was delete
urlValue = xpp.getText();
from the
case XmlPullParser.TEXT:
Then change
else if(tagName.equalsIgnoreCase("enclosure")){
urlValue = xpp.getAttributeValue(0);
currentRecord.setLink(urlValue);
}
in the
case XmlPullParser.END_TAG:
How can I get element Id in android
<EditText
android:id="#+id/etCustomerNo"
I need access to id of editText in my activity (for example get "etCustomerNo" as a String).
Thank you.
I need to know id of all editText on layout
for (int i = 0; i < rl.getChildCount(); i++) {
if (rl.getChildAt(i) instanceof EditText) {
String id = String.valueOf(rl.getChildAt(i).getId());
}
}
getId() returns an int value instead of "etCustomerNo"
String s = getResources().getResourceEntryName(et.getId());
Et is your EditText object.
s is your id name.
by looking at View's source code inside toString() method, we can see how you can get the id name as string:
final int id = getId();
if (id != NO_ID) {
out.append(" #");
out.append(Integer.toHexString(id));
final Resources r = mResources;
if (Resources.resourceHasPackage(id) && r != null) {
try {
String pkgname;
switch (id&0xff000000) {
case 0x7f000000:
pkgname="app";
break;
case 0x01000000:
pkgname="android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
}
}
}
the entryname string is what you're looking for.
Simple.
EditText et=(EditText)FindViewById(R.id.etCustomerNo);
EditText edittext = (EditText) this.findViewById(R.id.etCustomerNo);
If you already have the editText, then try calling getId(). If you need to do anything further you'd like need to put together a switch statement formed of the various options:
private String getIdAsString(EditText editText) {
String value = null;
int id = editText.getId();
switch (id) {
case R.id.etCustomerNo:
value = "etCustomerNo";
break;
case R.id.edit_text_two:
value = "edit_text_two";
break;
}
return value;
}
I made a class to parse indoorGML information. The indoorGML has 5 layers.
All the parts work.
In fifth layer, I need to call a tag called posList:
<indoorCore:CellSpace gml:id='L5C1'>
<indoorCore:Geometry2D>
<gml:Polygon >
<gml:exterior>
<gml:LinearRing>
<gml:posList>529.0 840.25 529.0 803.51 540.0 803.51</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</indoorCore:Geometry2D>
<indoorCore:PartialboundedBy>
<indoorCore:CellSpaceBoundary gml:id='L5B1'>
<indoorCore:geometry2D>
<gml:LineString>
<gml:posList>540.0 803.51 529.0 803.51 529.0 840.25</gml:posList>
</gml:LineString>
</indoorCore:geometry2D>
<indoorCore:duality>
<indoorCore:Transition xlink:href='#L5T1'/>
</indoorCore:duality>
</indoorCore:CellSpaceBoundary>
I need the points inside tag posList which are inside the tag polygon. I wrote the parsing like this:
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
currentTagName = parser.getName();
if (currentTagName.equals("gml:Polygon")) {
inPolygon = true;
} else if (currentTagName.equals("indoorCore:SpaceLayer")) {
String layer = parser.getAttributeValue(0);
currentLayer = layer;
if (layer.equals("L5")) {
startOfFifthLayer = true;
}
} else if(currentTagName.equals("gml:name")) {
inName = true;
// inNodes=true;
// Log.d("name", "true");
}
if(currentTagName.equals("gml:posList")) {
inLayerFivePolygon = true;
Log.d("Polygon5", "true");
}
break;
case XmlPullParser.END_TAG:
if (currentTagName.equals("indoorCore:SpaceLayer")) {
String layer = parser.getAttributeValue(0);
currentLayer = layer;
currentTagName = parser.getName();
if(currentLayer.equals("L5")) {
endOfFifthLayer = true;
}
}
break;
case XmlPullParser.TEXT:
if (inName) {
// put posList points in an array
String points = parser.getText();
Log.d("PlaceName MapPlace", points);
String[] split = points.split(" ");
inName=false;
} else if(inLayerFivePolygon){
String points = parser.getText();
Log.d("Area MapPlace", points);
String[] split = points.split(" ");
inLayerFivePolygon=false;
}
break;
}
if (endOfFifthLayer) {
break;
}
eventType = parser.next();
}
}
When I just parse tag posList, my program can read the points. But when I want to filter it and put it inside the polygon tag, it does not show the points to me in the Log.
Do you know what is the trick?
thanks a lot!
I solved it by changing the condition of testing the layer in the case XmlPullParser.START_TAG :
else if (currentTagName.equals("indoorCore:SpaceLayer")) {
String layer = parser.getAttributeValue(0);
currentLayer = layer;
if (!layer.equals("L5")) {
inFifthLayer = false;
}else {
inFifthLayer = true;
startOfFifthLayer = true;
}
Then to control the tag, moreover testing the tag, I control the layer again, like this:
if(currentTagName.equals("gml:posList") && inFifthLayer) {
inLayerFivePolygon = true;
Log.d("Polygon5", "true");
}
Finally in the XmlPullParser.TEXT, I parse the points I need like this:
XmlPullParser.TEXT:
if(inLayerFivePolygon){
String points = parser.getText();
String[] split = points.split(" ");
//ArrayList<String> finalPoints = convertToPolygon(points);
// Log.d("Area MapPlace", finalPoints.toString());
Log.d("Area MapPlace2", points);
inLayerFivePolygon=false;
}
I've done XML parsing via XmlPullParser and it works fine until I add special characters to my xml file such as š,č,ř,ž and so on. It replaces the character with "?" sign. Is there any way how to get rid of that?
Here is my Xml parser class:
public class ClubsXmlPullParser {
static final String CLUB = "club";
static final String NAME = "name";
static final String ABOUT = "about";
static final String STADIUM = "stadium";
static final String LOGO = "logo";
static final String MOREABOUT = "moreAbout";
public static List<LeagueClub> getItemsFromFile(Context ctx) {
List<LeagueClub> clubs;
clubs = new ArrayList<LeagueClub>();
LeagueClub curItem = null;
String curText = "";
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
FileInputStream fis = ctx.openFileInput("clubs.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
xpp.setInput(reader);
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = xpp.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase(CLUB)) {
curItem = new LeagueClub();
}
break;
case XmlPullParser.TEXT:
curText = xpp.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase(CLUB)) {
clubs.add(curItem);
} else if (tagname.equalsIgnoreCase(NAME)) {
curItem.setName(curText);
} else if (tagname.equalsIgnoreCase(ABOUT)) {
curItem.setAbout(curText);
} else if (tagname.equalsIgnoreCase(STADIUM)) {
curItem.setStadium(curText);
} else if (tagname.equalsIgnoreCase(LOGO)) {
curItem.setLogo(curText);
} else if (tagname.equalsIgnoreCase(MOREABOUT)) {
curItem.setName(curText);
}
break;
default:
break;
}
eventType = xpp.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return clubs;
}
}
Assuming that you know how to obtain the encoding of the file, you can use the alternative version of setInput - setInput(InputStream inputStream, String inputEncoding)