Reputation: 179
i want to parse country it is present in xml folder
<My_location_country>
<Country_str_code>AW</Country_str_code>
<Country_str_name>Aruba</Country_str_name>
</my_location_country>
....................... i want to display country name in array adapter whn i selecte the country i want the countrycode to be print i am using following method but it is not working
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean countryflag = false;
boolean countryidflag = false;
public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {
System.out.println("Start Element :" + qName);
if (qName.equalsIgnoreCase("Country_str_code")) {
countryidflag = true;
}
if (qName.equalsIgnoreCase("Country_str_name")) {
countryflag = true;
}
}
public void endElement(String uri, String localName,
String qName) throws SAXException {
System.out.println("End Element :" + qName);
}
public void characters(char ch[], int start, int length) throws SAXException {
if (countryidflag) {
System.out.println("Country_str_code" + new String(ch, start, length));
String val=new String(ch, start, length);
countryid.add(val);
countryidflag = false;
}
if (countryflag) {
System.out.println("Country_str_name " + new String(ch, start, length));
String val1=new String(ch, start, length);
countryname.add(val1);
countryflag = false;
}
}
};
File file = new File("res/xml/country.xml");
InputStream inputStream= new FileInputStream(file);
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");
//saxParser.parse();
} catch (Exception e) {
e.printStackTrace();
}
in file location i am getting error any help..............thanx in advance
Upvotes: 0
Views: 1840
Reputation: 179
try
{
//Read myxml.xml file from /res/raw folder
InputStream is=getResources().openRawResource(R.raw.country);
Log.i("TAG","is value==>"+is);
DocumentBuilder builder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc=builder.parse(is, null);
//get the root node
NodeList nodeList=doc.getElementsByTagName("skadate_location_country");
Log.i("TAG","nodeList Length --" + nodeList.getLength()); //output:- -1
Log.i("TAG","nodeList name --" + nodeList.item(0).getNodeName()); //output:- message
for(int j=0; j<nodeList.getLength(); j++) {
Node nodee=nodeList.item(j);
for(int i=0;i<nodee.getChildNodes().getLength();i++)
{
Node node=nodee.getChildNodes().item(i);
if(node.getNodeName().equalsIgnoreCase("Country_str_code")){
Log.i("TAG","from node::" + node.getTextContent());
countryname.add(node.getTextContent());
countrySelector.setAdapter(countryname);
countrySelector.setOnItemSelectedListener(this);
}
else if(node.getNodeName().equalsIgnoreCase("Country_str_name")){
Log.i("TAG","to node::" + node.getTextContent());
}
}
}
is.close();
} catch(Exception e) {
System.out.println("ERROR while parsing xml:----" + e.getMessage());
}
Upvotes: 0
Reputation: 5058
Please use proper Path of raw file
you can put the XML File In raw folder or asset folder
for raw folder you can fetch the file as
int resourceId = context.getResources().getIdentifier("com.your.package:raw/somefile.xml");
File f = new File(context.getResources().openRawResource(resourceId));
or
InputStream in = this.getResources().openRawResource(R.raw.myxml);
from assset
AssetManager assetManager = getAssets();
inputStream = assetManager.open("readme.xml")
from SDCard
FileInputStream in = new FileInputStream("/sdcard/text.txt");
//for assets as well resource ans as well SD CARD storage file its use full link for you This
Upvotes: 1